Sharing Extension in IOS8 beta

后端 未结 7 1946
天命终不由人
天命终不由人 2020-12-24 08:40

I\'m trying to create a sharing extension using the new iOS 8 app extensions. I tried to get the current URL of a Safari site to show it in a UILabel. Simple enough.

7条回答
  •  失恋的感觉
    2020-12-24 09:16

    The other answers are all complicated and incomplete. They only work in Safari and do not work in Google Chrome. This works both in Google Chrome and Safari:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        for item in extensionContext!.inputItems {
            if let attachments = item.attachments {
                for itemProvider in attachments! {
                    itemProvider.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: { (object, error) -> Void in
                        if object != nil {
                            if let url = object as? NSURL {
                            print(url.absoluteString) //This is your URL
                            }
                        }
                    })
                }
            }
        }
    }
    

提交回复
热议问题