Search code examples
swiftxcodesafari-web-extension

Cannot find 'SFExtensionMessageKey' in scope


I'm trying to build the Native Messaging Test Project and it keeps failing to build with Cannot find 'SFExtensionMessageKey' in scope. How do I fix this?

The error comes from this bit of code:

SafariWebExtensionHandler.swift

/*
See LICENSE folder for this sample’s licensing information.

Abstract:
Communicates with the extension running in Safari.
*/
import SafariServices

class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {

    // Receive a message from the JavaScript component of the Web Extension.
    // Unpack the message, and then wrap in an object to send as the response.
    func beginRequest(with context: NSExtensionContext) {
        let item = context.inputItems[0] as? NSExtensionItem
        let message = item?.userInfo?[SFExtensionMessageKey] // ERROR HERE

        let response = NSExtensionItem()
        response.userInfo = [ SFExtensionMessageKey: [ "Response to": message ] ] // AND ERROR HERE
        
        context.completeRequest(returningItems: [response], completionHandler: nil)
    }
    
}

It fails because it cannot find SFExtensionMessageKey in scope, but when I look it up I see that it is supposed to be included in Safari Services.

When I inspect the Safari Services file directly in Xcode, I don't see any mention of SFExtensionMessageKey -- this could just be my confusion. But it seems like this was renamed, maybe?

Another thought was that I needed to add the SafariServices framework to the Build phases (under the section "Link Binary With Libraries") for the various app targets. This also did not solve the issue.

I'm on Xcode 12.1 & Swift 5.


Solution

  • As discussed in the comments, SFExtensionMessageKey is only available for macOS 11+ and not iOS or earlier macOS versions.