0

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.

cameck
  • 2,058
  • 20
  • 32

1 Answers1

1

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

Marco Boerner
  • 1,243
  • 1
  • 11
  • 34
  • As a note, https://developer.apple.com/documentation/safariservices/sfextensionmessagekey says it is available only in MacOS 11, but I was able to access the variable by upgrading to Xcode 12.2 and MacOS 10.15. Xcode 12.1 does not have this variable – cameck Jan 11 '21 at 22:48
  • Strange! Even the messaging test project has its target set to 10.16 – Marco Boerner Jan 12 '21 at 11:22
  • now it's available for iOS 15.0+ – Serge Nov 15 '21 at 00:34