[英]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 am using share extension in my app.我在我的应用程序中使用共享扩展。 I am using it as a subclass of a UIViewController not SLcomposer.In the viewDidLoad i am trying to access the text in the page from
NSItemProvider
,But it has only one key which is public url , Can anybody give me an idea of how to achieve the kUTTypePlainText
in NSItemProvider
.我将它用作 UIViewController 而不是 SLcomposer 的子类。在 viewDidLoad 我试图从
NSItemProvider
访问页面中的文本,但它只有一个键是 public url,任何人都可以给我一个如何实现的想法kUTTypePlainText
中的NSItemProvider
。 I also set NSExtensionActivationSupportsText, its type to Boolean, and the value to YES我还设置了 NSExtensionActivationSupportsText,其类型为布尔值,值为 YES
You can use the ExtensionPreprocessingJS to do this.您可以使用 ExtensionPreprocessingJS 来执行此操作。
First, add the the NSExtensionJavaScriptPreprocessingFile
key to the NSExtensionAttributes
dictionary in your Info.plist.首先,将
NSExtensionJavaScriptPreprocessingFile
键添加到 Info.plist 中的NSExtensionAttributes
字典中。 The value should be the name of your JavaScript file without the extension, ie MyExtensionJavaScriptClass
.该值应该是不带扩展名的 JavaScript 文件的名称,即
MyExtensionJavaScriptClass
。
Then, in the NSExtensionActivationRule
dictionary in your Info.plist, set the NSExtensionActivationSupportsWebPageWithMaxCount
key with a value of 1 (type Number).然后,在 Info.plist 的
NSExtensionActivationRule
字典中,将NSExtensionActivationSupportsWebPageWithMaxCount
键设置为 1(类型为 Number)。
Next, add MyExtensionJavaScriptClass.js
to your extension with the following code:接下来,使用以下代码将
MyExtensionJavaScriptClass.js
添加到您的扩展程序中:
var MyExtensionJavaScriptClass = function() {};
MyExtensionJavaScriptClass.prototype = {
run: function(arguments) {
arguments.completionFunction({"title": document.title});
}
};
// The JavaScript file must contain a global object named "ExtensionPreprocessingJS".
var ExtensionPreprocessingJS = new MyExtensionJavaScriptClass;
Then include the following function in your ShareViewController viewDidLoad
and import MobileCoreServices
然后在您的 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 = results["title"] as! String
//yay, you got the title now
}
})
} else {
print("error")
}
I guess you've to request the site using the provided URL, parse the content and get the title from that string.我猜您必须使用提供的 URL 请求站点,解析内容并从该字符串中获取标题。
BTW: Apple Documentation say:顺便说一句:Apple 文档说:
If your Share or iOS Action extension needs to access a webpage, you must include the NSExtensionActivationSupportsWebPageWithMaxCount key with a nonzero value
如果你的 Share 或 iOS Action 扩展需要访问一个网页,你必须包含一个非零值的 NSExtensionActivationSupportsWebPageWithMaxCount 键
See the section " Accessing a Webpage "请参阅“ 访问网页”部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.