分享所选文字-iOS分享扩展

时间:2018-06-27 00:15:41

标签: ios swift xcode ios8-share-extension share-extension

我希望能够共享选定的文本,但是我的扩展名仅在单击共享图标时出现(然后它会在页面标题中填充该字段)。我希望我的扩展名在用户选择文本并单击“共享...”(如下图所示)时出现,然后我希望它使用所选文本填充文本区域。

I want extension to be accessible here

Share View Controller:

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

1 个答案:

答案 0 :(得分:3)

目前,您的NSExtensionActivationRule明确要求提供1个Web 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>

您可以在这篇文章中了解更多信息:IOS Share extension: how to read from notes posts