3

我试图弄清楚如何让我的共享扩展程序访问网页并操纵其内容。我已经按照文档进行了操作, 但我一定遗漏了一些东西,因为我没有看到任何符合kUTTypePropertyList我认为应该的项目。

我的 plist 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleDisplayName</key>
    <string>WebOutLoudExtension</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>com.bretlester.WebsiteReader.$(PRODUCT_NAME:rfc1034identifier)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>XPC!</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>NSExtension</key>
    <dict>
        <key>NSExtensionAttributes</key>
        <dict>
            <key>NSExtensionActivationRule</key>
            <dict>
                <key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
                <integer>1</integer>
                <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
                <integer>1</integer>
                <key>NSExtensionJavaScriptPreprocessingFile</key>
                <string>ExtensionPreProcessing</string>
            </dict>
        </dict>
        <key>NSExtensionMainStoryboard</key>
        <string>MainInterface</string>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.share-services</string>
    </dict>
</dict>
</plist>

我的 ExtensionPreProcessing 看起来像这样:

ExtensionPreprocessingJS = {
run : function(arguments) {
    arguments.completionFunction({"hello":"world"});
},
finalize : function(arguments) {
    document.body.style.backgroundColor = 'red';
}
};

最后,我的 ShareViewController 看起来像这样:

class ShareViewController: UIViewController, NSExtensionRequestHandling {


override func viewDidLoad() {

    let identifierType = NSString(format: kUTTypePropertyList, NSUTF8StringEncoding)

    for (item: NSExtensionItem) in self.extensionContext?.inputItems as [NSExtensionItem] {
        for (itemProvider: NSItemProvider) in item.attachments as [NSItemProvider] {
            if itemProvider.hasItemConformingToTypeIdentifier(identifierType) {
                println("why doesn't this happen?")
            }
        }
    }

}

override func viewDidAppear(animated: Bool) {


}

override func beginRequestWithExtensionContext(context: NSExtensionContext) {
    super.beginRequestWithExtensionContext(context)

    let identifierType = NSString(format: kUTTypePropertyList, NSUTF8StringEncoding)

    //println(self.extensionContext?.inputItems)
    for (item: NSExtensionItem) in context.inputItems as [NSExtensionItem] {
        for (itemProvider: NSItemProvider) in item.attachments as [NSItemProvider] {
            if itemProvider.hasItemConformingToTypeIdentifier(identifierType) {
                println("why doesn't this happen?")
            }
        }
    }

}

}

注意我的 println 语句。他们从不执行。我基本上只是在寻找某种确认事情正在运行的确认。我的共享扩展显示在 Safari 中,但我认为我的自定义 javascript 没有运行。希望有人能发现我做错了什么。提前致谢

4

1 回答 1

3

你是正确的,JS 没有运行。这是因为您的 Info.plist 配置。NSExtensionJavaScriptPreprocessingFile需要是 的子键NSExtensionAttributes,而不是 的子键NSExtensionActivationRule。就像是

<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionJavaScriptPreprocessingFile</key>
        <string>ExtensionPreProcessing</string>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
            <integer>1</integer>
            <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
            <integer>1</integer>
        </dict>
    </dict>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
</dict>
于 2015-02-23T18:50:21.937 回答