分享所选文本 - iOS 分享扩展
Share Selected Text - iOS Share Extension
我希望能够共享选定的文本,但我的扩展仅在单击共享图标时出现(然后它会使用页面标题填充该字段)。我希望我的扩展程序在用户选择文本并单击 "Share..." 时出现(如下图所示),然后我希望它用所选文本填充文本区域。
共享视图控制器:
override func viewDidLoad() {
super.viewDidLoad()
customPopup()
let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
let itemProvider = extensionItem.attachments?.first as! NSItemProvider
let propertyList = String(kUTTypePropertyList)
if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
guard let dictionary = item as? NSDictionary else { return }
OperationQueue.main.addOperation {
if let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary {
print("RESULTS: \n", results)
}
}
})
} else {
print("error")
}
}
Action.js(JS预处理)
var MyPreprocessor = function() {};
MyPreprocessor.prototype = {
run: function(arguments) {
arguments.completionFunction({"URL": document.URL, "title": document.title, "selection": window.getSelection().toString()});
}
};
var ExtensionPreprocessingJS = new MyPreprocessor;
Info.plist
就目前而言,您的 NSExtensionActivationRule
明确要求 1 个网络 URL,因此这就是您得到的结果。
相反,请尝试将您的 NSExtensionActivationRule
(在 info.plist
中)更改为:
<dict>
<key>NSExtensionActivationSupportsText</key>
<true/>
</dict>
如果这不起作用,请尝试更长的时间:
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationUsesStrictMatching</key>
<integer>2</integer>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationDictionaryVersion</key>
<integer>2</integer>
<key>NSExtensionActivationSupportsText</key>
<true/>
</dict>
</dict>
您可以在 post 中阅读更多内容:。
我希望能够共享选定的文本,但我的扩展仅在单击共享图标时出现(然后它会使用页面标题填充该字段)。我希望我的扩展程序在用户选择文本并单击 "Share..." 时出现(如下图所示),然后我希望它用所选文本填充文本区域。
共享视图控制器:
override func viewDidLoad() {
super.viewDidLoad()
customPopup()
let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
let itemProvider = extensionItem.attachments?.first as! NSItemProvider
let propertyList = String(kUTTypePropertyList)
if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
guard let dictionary = item as? NSDictionary else { return }
OperationQueue.main.addOperation {
if let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary {
print("RESULTS: \n", results)
}
}
})
} else {
print("error")
}
}
Action.js(JS预处理)
var MyPreprocessor = function() {};
MyPreprocessor.prototype = {
run: function(arguments) {
arguments.completionFunction({"URL": document.URL, "title": document.title, "selection": window.getSelection().toString()});
}
};
var ExtensionPreprocessingJS = new MyPreprocessor;
Info.plist
就目前而言,您的 NSExtensionActivationRule
明确要求 1 个网络 URL,因此这就是您得到的结果。
相反,请尝试将您的 NSExtensionActivationRule
(在 info.plist
中)更改为:
<dict>
<key>NSExtensionActivationSupportsText</key>
<true/>
</dict>
如果这不起作用,请尝试更长的时间:
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationUsesStrictMatching</key>
<integer>2</integer>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationDictionaryVersion</key>
<integer>2</integer>
<key>NSExtensionActivationSupportsText</key>
<true/>
</dict>
</dict>
您可以在 post 中阅读更多内容: