iOS Share Extension Grabbing URL in Swift

8.5k views Asked by At

I'm trying to create an iOS share extension in swift. When the user is in safari and opens the share extension, I want to be able to grab the URL and use it in my app. I know I can put the code below in the didSelectPost() function in the ShareViewController to get the text that the user enters in the share extension, but how do I get the URL of the web page the user is on when they click the share extension? I'm fairly new to iOS extensions, so any help would be much appreciated.

 let shareDefaults = NSUserDefaults(suiteName: "groupName")
 shareDefaults?.setObject(self.contentText, forKey: "stringKey")
 shareDefaults?.synchronize()
2

There are 2 answers

0
joern On

This is how you get the URL:

- (void)didSelectPost {
    NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
    NSItemProvider *itemProvider = item.attachments.firstObject;
    if ([itemProvider hasItemConformingToTypeIdentifier:@"public.url"]) {
        [itemProvider loadItemForTypeIdentifier:@"public.url"
                                        options:nil
                              completionHandler:^(NSURL *url, NSError *error) {
                                  NSString *urlString = url.absoluteString;
                                  // send url to server to share the link
                                  [self.extensionContext completeRequestReturningItems:@[]         
                                                                     completionHandler:nil];
                              }];
    }
}

Or in Swift:

override func didSelectPost() {
    if let item = extensionContext?.inputItems.first as? NSExtensionItem {
        if let itemProvider = item.attachments?.first as? NSItemProvider {
            if itemProvider.hasItemConformingToTypeIdentifier("public.url") {
                itemProvider.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: { (url, error) -> Void in
                    if let shareURL = url as? NSURL {
                        // send url to server to share the link
                    }
                    self.extensionContext?.completeRequestReturningItems([], completionHandler:nil)
                })
            }
        }
    }
}
2
Sam Napolitano On

There is one small change to this. In Chrome the public.url is in item 1, not item 0 of the attachements. Looping through to find it is better and will work on both chrome and safari.

if let item = extensionContext?.inputItems.first as? NSExtensionItem {
    if let attachments = item.attachments as? [NSItemProvider] {
        for attachment: NSItemProvider in attachments {
            if attachment.hasItemConformingToTypeIdentifier("public.url") {
                attachment.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: { (url, error) in
                    if let shareURL = url as? NSURL {
                        // Do stuff with your URL now. 
                    }
                    self.extensionContext?.completeRequestReturningItems([], completionHandler:nil)
                })
            }
        }
    }
}