如何在共享扩展视图控制器中获取标题? [英] 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;
}
}];
}
我在我的应用程序中使用共享扩展名.我将其用作UIViewController而不是SLcomposer的子类.在viewDidLoad中,我试图从NSItemProvider
访问页面中的文本,但是它只有一个键是public url,有人可以告诉我如何以实现NSItemProvider
中的kUTTypePlainText
.我还设置了NSExtensionActivationSupportsText,其类型为Boolean,值为YES
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
. I also set NSExtensionActivationSupportsText, its type to Boolean, and the value to YES
推荐答案
您可以使用ExtensionPreprocessingJS执行此操作.
You can use the ExtensionPreprocessingJS to do this.
首先,将NSExtensionJavaScriptPreprocessingFile
键添加到Info.plist中的NSExtensionAttributes
词典中.该值应为不带扩展名的JavaScript文件的名称,即MyExtensionJavaScriptClass
.
First, add the the NSExtensionJavaScriptPreprocessingFile
key to the NSExtensionAttributes
dictionary in your Info.plist. The value should be the name of your JavaScript file without the extension, i.e. MyExtensionJavaScriptClass
.
然后,在Info.plist的NSExtensionActivationRule
词典中,将NSExtensionActivationSupportsWebPageWithMaxCount
键设置为1(数字类型).
Then, in the NSExtensionActivationRule
dictionary in your Info.plist, set the NSExtensionActivationSupportsWebPageWithMaxCount
key with a value of 1 (type Number).
接下来,使用以下代码将MyExtensionJavaScriptClass.js
添加到您的扩展程序中:
Next, add MyExtensionJavaScriptClass.js
to your extension with the following code:
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;
然后在ShareViewController viewDidLoad
和import MobileCoreServices
Then include the following function in your ShareViewController viewDidLoad
and 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")
}
这篇关于如何在共享扩展视图控制器中获取标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!