iOS Share Extension: get URL of page when sharing via context menu in Safari

后端 未结 2 1764
感情败类
感情败类 2021-02-01 22:07

What I want

I\'m trying to achieve the following user flow:

  1. User is browsing a webpage in iOS Safari.
  2. User selects some content (
2条回答
  •  天涯浪人
    2021-02-01 22:57

    Swift 3

    Try something along the lines of:

    override func didSelectPost() {
        if let item = extensionContext?.inputItems.first as? NSExtensionItem,
            let itemProvider = item.attachments?.first as? NSItemProvider,
            itemProvider.hasItemConformingToTypeIdentifier("public.url") {
            itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { (url, error) in
                if let shareURL = url as? URL {
                    // do what you want to do with shareURL
                }
                self.extensionContext?.completeRequest(returningItems: [], completionHandler:nil)
            }
        }
    }
    

    "public.url" can be replaced with the kUTTypeURL String imported from MobileCoreServices

提交回复
热议问题