3

I'm trying to figure out how to get my share extension to access a web page and manipulate its contents. I've followed the documentation but I must be missing something because I am not seeing any items conforming to kUTTypePropertyList as I thought I was supposed to.

My plist looks like this:

<?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>

My ExtensionPreProcessing Looks like this:

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

Finally, my ShareViewController looks like this:

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?")
            }
        }
    }

}

}

Notice my println statements. They never execute. I am basically just looking for some sort of confirmation that things are working. My share extension shows up in Safari but I don't think my custom javascript is running. Hopefully someone can spot what I'm doing wrong. Thanks in advance

Bret
  • 883
  • 1
  • 9
  • 18

1 Answers1

3

You're correct that the JS is not running. It's because of your Info.plist configuration. NSExtensionJavaScriptPreprocessingFile needs to be a sub-key of NSExtensionAttributes, not of NSExtensionActivationRule. Something like

<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>
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170