How to share lib between phone app & WatchOS2 extension?

Has anyone been able to share their Static Library or Framework between both the phone app & the watch os2 extension? If so, please let me know.


Under new target (iOS target > Framework & Library), I've tried to use either the Cocoa Touch Framework or the Cocoa Touch Static Library. I think the latter is more appropriate since the description indicates that the former uses UIKit (and you don't get UIKit in the watch extension). I'm able to import & use my static library in my phone app (via the main view controller). I am unable to use my static library in my watch InterfaceController since the import gives me "file not found" error. I'm getting this "file not found" error, even though I've added my static library in the Target Dependencies, Link Binary With Libraries, and Other Linker Flags.


Is there a way to share libs/code between the Watch OS 2 extension & the phone app? If so, how & what is the best way? Thanks!


P.S. Not sure what's the difference between the 'Framework & Library' targets under iOS versus under watchOS.

We got this running. Xcode Beta 6 under El cap.


We are using a dynamic lib. Added a iOS Framework and a Watch Framework.

Files have to be added to each target.

We were even able to add external frameworks from carthage.


In the Lister example you will find a configuration of how to use a framework in a watchOS 2 app.

All we needed to do is to add the Framework in "Embedded Libraries" in the extensions build config.

And we changed the product name of the watch lib to be the same as the ios framework.

So basically the framework can use e.g. Alamofire and will automatically use Alamofire for watchOS in the watch target and Alamofire for iOS in the iOS target.

(As they have the same product name for both targets)

Remember, on watchOS 1 a framework could be shared between your iPhone app, the watchOS 1 extension and other extensions (today widgets etc). On watchOS 2, the extension runs on the watch, which has a different ARM architecture so you have to create a separate framework specifically built for watchOS 2.


If you don't have any resources in the framework, it might be simpler to just include the source files you need to share in the watchOS 2 extension and don't even bother with a watchOS 2 framework. Other than resources, there's nothing else you can share the watchOS 2 framework with (unlike the framework on the iPhone), so creating a framework might be overkill.

Have you tried validating that? You can't embed a library in an app extension no?

That's what I'm leaning towards, less DRY code by having different code in the phone & watch areas.


I also don't like the mystery meat (copy & paste) nature of the shell script required in Build Phase > Run Script to create binaries for different architectures.

Thanks, I'll look into the OS2 Lister, but there is a difference between looking at a codebase & going through the steps yourself with Xcode. I know I had some issues with being able to find "Embedded Libraries" in the targets.

You can embed a library to an extension.

you need to set the flag for "Allow App Extension API only"

How to share lib between phone app & WatchOS2 extension?
 
 
Q