ios share extension unable to get shared URL from chrome

丶灬走出姿态 提交于 2019-12-06 14:17:29

问题


I am trying to implement share extension for my app. Its working good in safari browser and youtube app (i.e) when i share from these apps i get the public.url which is the url to be shared.

When i tried the same in chrome it was not showing the extension. When i added the NSExtensionActivationSupportsText under the NSExtensionActivationRule to true its started showing. But when i try to share the contents i am unable to fetch the URL String which is to be shared. I get only contentText.

I have followed the approach shown in this link https://stackoverflow.com/a/31942744/6199038.

I have also added the demoProcessor.js

var MyPreprocessor = function() {};
MyPreprocessor.prototype = {
run: function(arguments) {
    arguments.completionFunction({"URL": document.URL, "pageSource": document.documentElement.outerHTML, "title": document.title, "selection": window.getSelection().toString()});
}
};
var ExtensionPreprocessingJS = new MyPreprocessor;

I am using SLComposeServiceViewController

In my ShareViewController.m i am trying to get the data as shown below,

for safari i used this which is working fine

NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = [[item.userInfo valueForKey:NSExtensionItemAttachmentsKey] objectAtIndex:0];
if ([itemProvider hasItemConformingToTypeIdentifier:@"public.url"]) {
    [itemProvider loadItemForTypeIdentifier:@"public.url" options:nil completionHandler:^(NSURL *url, NSError *error) {
        urlString = url.absoluteString;
        NSLog(@"urlString %@",urlString);
    }];
}

Then i modified my code to this to get the URL from chrome, Which is not working.

 for (NSExtensionItem *item in self.extensionContext.inputItems) {
    for (NSItemProvider *itemProvider in item.attachments) {
        if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypePropertyList]) {
            [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypePropertyList options:nil completionHandler:^(NSDictionary *jsDict, NSError *error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSDictionary *jsPreprocessingResults = jsDict[NSExtensionJavaScriptPreprocessingResultsKey];

                    NSString *selectedText = jsPreprocessingResults[@"selection"];
                    NSString *pageTitle = jsPreprocessingResults[@"title"];
                    NSString *URL = jsPreprocessingResults[@"URL"];

                    NSLog(@"selectedText %@",selectedText);
                    NSLog(@"pageTitle %@",pageTitle);
                    NSLog(@"URL %@",URL);
                });
            }];

            break;
        }
    }

}

PLEASE ADVICE


回答1:


I have solved it myself. As it was not entering inside the "loadItemForTypeIdentifier" method. So i had modified my method to the below code

for (NSExtensionItem *item in self.extensionContext.inputItems) {
        for (NSItemProvider *itemProvider in item.attachments) {

            NSString *URLinPlainText = [item.attributedContentText string];
            if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) {
                [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error) {

                    urlString = url.absoluteString;
                    NSLog(@"<< URL >> %@",urlString);
                    return;
                }];
            }
            else if (URLinPlainText)  {
                // In Some app i got the URL in a Plain text mode
                if([URLinPlainText containsString:@"http"]){
                    urlString = [sharedPlainText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                    NSLog(@"<< URL >> %@",urlString);
                    return;
                }
            }
        }
    }

And also i have removed the demoProcessor.js. Now i am able to get the URL from almost all the News Apps, Browsers like (Chrome, firefox, safari) and in some of the apps i am getting the shared url in the form of plain text which is then i had to remove the empty spaces and convert it to NSString.

According to my understanding the demoprocessor.js is used if user wants to access the url page properties for ex(title, baseURL, og:image, og:description etc...) which works only for safari browser.



来源:https://stackoverflow.com/questions/37454200/ios-share-extension-unable-to-get-shared-url-from-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!