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
Post a Comment