iOS共享扩展无法从Chrome获取共享URL [英] ios share extension unable to get shared URL from chrome

查看:161
本文介绍了iOS共享扩展无法从Chrome获取共享URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用程序实现共享扩展.当我从Safari浏览器和youtube应用(例如,我从这些应用共享)中运行时,它会获得public.url,即要共享的url.

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.

当我在chrome中尝试相同的功能时,没有显示扩展名.当我将NSExtensionActivationRule下的NSExtensionActivationSupportsText添加为true时,它开始显示.但是,当我尝试共享内容时,我无法获取要共享的URL字符串.我只有contentText.

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.

我已遵循此链接 https://stackoverflow.com/a/31942744/6199038 中显示的方法.

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

我还添加了demoProcessor.js

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;

我正在使用 SLComposeServiceViewController

在我的ShareViewController.m中,我试图获取如下所示的数据,

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);
    }];
}

然后我对此进行了修改,以从chrome获取URL,该URL无法正常工作.

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;
        }
    }

}

请咨询

推荐答案

我自己已经解决了.由于它没有进入"loadItemForTypeIdentifier"方法内部.所以我已经将我的方法修改为以下代码

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;
                }
            }
        }
    }

我也删除了demoProcessor.js. 现在,我能够从几乎所有新闻应用程序,浏览器(例如Chrome,firefox,safari)中获取URL,在某些应用程序中,我会以纯文本形式获取共享的url,然后我必须删除该URL.空格并将其转换为NSString.

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.

根据我的理解,如果用户想访问ex(title,baseURL,og:image,og:description等)的url页面属性,则使用demoprocessor.js,该属性仅适用于Safari浏览器.

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.

这篇关于iOS共享扩展无法从Chrome获取共享URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆