Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I build a Safari Extension Companion in Xcode 8.2.1? the template is missing

I want to create a Safari Extension Companion, but the option to create a Safari extension doesn't appear.

What is the right way to create an extension?

enter image description here

like image 586
makerofthings7 Avatar asked Feb 12 '17 17:02

makerofthings7


2 Answers

To create a Safari app extension, you add a new target to an existing project in Xcode. — mentioned in the documentation.

  1. Launch Xcode and open an existing project containing an OS X application, or create a new one.

  2. Create a new target by choosing File > New > Target.

  3. In the New Target sheet, look in the sidebar on the left and select OS X Application Extension.

  4. From the list of templates on the right, select Safari Extension and click Next.

enter image description here

  1. Enter a Product Name for your extension, such as “My Extension.”

  2. Make sure that your application project is selected in the Project menu, and that your OS X application target is selected in the Embed in Application menu.

  3. Click Finish.

  4. When Xcode asks you if you want to activate a new scheme for your new extension, click the Cancel button.


Xcode adds a new group into your project, which contains several new files, including an Info.plist file, a variety of supporting source files, an Interface Builder file, a JavaScript file, and a toolbar image PDF.

* There might be a few more steps, although I'm pretty sure you can handle it...

↳ Add a Safari App Extension Target in Xcode

like image 199
l'L'l Avatar answered Sep 29 '22 16:09

l'L'l


The easiest way is to just install Xcode 7 side-by-side with Xcode 8 - this works fine - and load your project in 7. Create the extension companion target, but don't do anything with it yet. Close Xcode 7 and open Xcode 8 to the same project/workspace, and you'll see the companion. If you use Swift you'll need to modernize the language (Xcode 7 uses Swift 2, Xcode 8 uses Swift 3); the IDE will suggest some of the changes directly when you try to compile but others you may need to change by hand.

Alternatively / more awkwardly, you can create a new target (such as a Safari Extension) in Xcode 8, and then delete the extraneous files (such as JavaScript for content injection) and edit/replace the plist in the extension to the plist of an extension companion. The key property is <string>com.apple.Safari.extension-companion</string> for the NSExtensionPointIdentifier; also make sure that the NSExtensionPrincipalClass implements the extension companion protocol (both of these keys are under NSExtension).

Please be aware that I've had mixed results with this approach. Sometimes it works, sometimes Safari pretends it can't see the extension companion at all, sometimes the extension can't see the companion until you re-load it and then it can... If you can see the extension companion, it should work (but note the warning here about if the companion crashes) and I haven't found any logging that helps troubleshoot these issues. Attempting to debug the extension companion using Xcode 8 doesn't work for me either.

I really wish Apple didn't insist so hard on "thinking different" and just supported Native Messaging like every single other modern browser.

like image 28
CBHacking Avatar answered Sep 29 '22 15:09

CBHacking