2

I'm developing share extension which is used on safari. I could get url on share extension. but I cant get page title.

let puclicURL = String(kUTTypeURL)
if itemProvider.hasItemConformingToTypeIdentifier(puclicURL) {
    itemProvider.loadItem(forTypeIdentifier: puclicURL, options: nil, completionHandler: { 
        (item, error) in
            if let url: NSURL = item as? NSURL {
                print("url", url)
                // I want page title also
            }
        }
   }

And, I tried below code.https://stackoverflow.com/a/33139355/5060282 I think below code can run only in Action Extension. not Share Extension.

let propertyList = String(kUTTypePropertyList)
        if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
            itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
                let dictionary = item as! NSDictionary
                OperationQueue.main.addOperation {
                    let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as! NSDictionary
                    let title = NSURL(string: (results["title"] as! String))
                    //yay, you got the title now
                    print(title)
                }
            })
        } else {
            print("error")
        }
// But, error...
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
oxygen
  • 165
  • 1
  • 1
  • 8
  • Have you made a print of your results dictionary? – Francesco Deliro Oct 30 '17 at 17:05
  • Oh... thanks! >> let title = NSURL(string: (results["title"] as! String)) this code is miss. below code is correct let title = results["title"] as! String – oxygen Oct 30 '17 at 17:44
  • the second code snippet can run in a ShareExtension. Did you register NSExtensionActivationSupportsWebURLWithMaxCount or NSExtensionActivationSupportsWebPageMaxCount in your plist? – Hilmar Demant Nov 10 '17 at 12:53

0 Answers0