ios - How to Get the title in share extension view controller? -


-(void)viewdidload {      [super viewdidload];      nsextensionitem *item = self.extensioncontext.inputitems[0];     nsitemprovider *itemprovider = item.attachments[0];      nslog(@"%@",itemprovider);      if ([itemprovider hasitemconformingtotypeidentifier:(nsstring *)kuttypeplaintext])     {             [itemprovider loaditemfortypeidentifier:(nsstring *)kuttypeplaintext options:nil completionhandler:^(nsstring *item, nserror *error)         {                    if (item)             {                 textstring=item;                 }         }];     } 

i using share extension in app. using subclass of uiviewcontroller not slcomposer.in viewdidload trying access text in page nsitemprovider,but has 1 key public url , can give me idea of how achieve kuttypeplaintext in nsitemprovider. set nsextensionactivationsupportstext, type boolean, , value yes

you can use extensionpreprocessingjs this. first, add the nsextensionjavascriptpreprocessingfile key nsextensionattributes dictionary in info.plist.

next, add myextensionjavascriptclass.js extension following codes

var myextensionjavascriptclass = function() {};     myextensionjavascriptclass.prototype = {       run: function(arguments) {       // pass baseuri of webpage extension.       arguments.completionfunction({"title": document.title});    }  };  // javascript file must contain global object named "extensionpreprocessingjs". var extensionpreprocessingjs = new myextensionjavascriptclass; 

then include following function in shareviewcontroller viewdidload , import mobilecoreservices

    let extensionitem = extensioncontext?.inputitems.first as! nsextensionitem     let itemprovider = extensionitem.attachments?.first as! nsitemprovider      let propertylist = string(kuttypepropertylist)     if itemprovider.hasitemconformingtotypeidentifier(propertylist) {         itemprovider.loaditemfortypeidentifier(propertylist, options: nil, completionhandler: { (item, error) -> void in             let dictionary = item as! nsdictionary             nsoperationqueue.mainqueue().addoperationwithblock {                 let results = dictionary[nsextensionjavascriptpreprocessingresultskey] as! nsdictionary                 let title = nsurl(string: (results["title"] as! string))                                                         //yay, got title             }         })     } else {         print("error")     } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -