How sharing the model of the main app with WatchOS2? - ios

I need to share my entities (CoreData) of the main app with WatchOS2.
There are two ways to share code between targets - frameworks, and just including the .m or .swift file in multiple targets using Xcode's inspector.
WatchOS2 can create a dynamic framework and the main app too.
I have tried to create a dynamic framework in the main app and share code with WatchOS 2 : `Not such module TestFrameworkKit'.
Framework isn't going to work with a watchOS 2 extension because that extension runs on a completely different device.
Any idea ?

I just add the .swift and .m files to both targets and this works great. Since the iOS and watch app need to be in the same project anyways, this is a very appropriate solution. I would only look into adding frameworks if you need to share code between projects.

Related

Objective C generated interface header file Not found in WatchKit

I am currently working to add Apple Watch app in my current App. I added the required profiles and got it to working without any issues.
Now, I want to use some of the class files from iphone app in my apple watch app. I did add the .m file in the target membership to the watchkit extension target.
Since the file which I want to use is in Objective C class, I went ahead and created the bridging header file for apple watch extension target.
Each time when I run the application; I get $(SWIFT_MODULE_NAME)-Swift.h file not found. I did try to follow steps from this link but to no avail. I did clean build folder; removed the derived data but it still gives me the same error.
Am I missing anything in particular which others have tried and can share with me ?
I am using Xcode 8 and Swift 3.
So I figured out the answer and thought of posting it here.
There are couple of ways in which you can fix this but it depends on what you want to do.
1) Using #IF TARGET_OS_IOS will allow you to use the class from iphone to watch.
2) You can also use WatchConnectivity framework to send and/or receive messages, send data also. This is the new framework Apple has recommended to use for connectivity between the devices.
Hope this will help someone else trying to figure out this problem.

iOS Today Widget share source file with main application

I've added a Today Extention Widget Project in my Xcode workspace and I want to use some source file from the main app, so I've flagged the "target membership" on this files. The problem is that the Xcode linker fail with duplicate symbol error when compiling the main app. So what is the correct way to share source file between main app and widget?
You can use Frameworks to share code/files between your Host App and App Extension.
You can create an embedded framework to share code between your app
extension and its containing app. For example, if you develop an image
filter for use in your Photo Editing extension as well as in its
containing app, put the filter’s code in a framework and embed the
framework in both targets.
For more on sharing scenarios between Host App and App Extension refer to: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

How to connect Realm with watchOS and iOS app at the same time?

So, there simple instructions on Realm website:
Installation (Swift 2.1.1):
...
2) Go to your Xcode project’s “General”
settings. Drag RealmSwift.framework and Realm.framework from the
ios/swift-2.1.1/, watchos/, tvos/ or osx/swift-2.1.1/ directory to the
“Embedded Binaries” section. Make sure Copy items if needed is
selected and click Finish.
Basically, stating that we need to use different Realm libraries for developing apps for Watch and for iPhone.
Problem here, is that if I want to create watchOS app I need to import library specific to it. When at the same time I'm trying to import library for iOS, I get error from xCode (version 7.2.1):
Multiple errors occurred while copying the files
Making it impossible to add both libraries at the same time.
My goal is to share data between Watch and iPhone like instructed in this article, but even there author is missing information on how to setup 2 different libraries.
Appreciate any help or advice. Thank you.
The problem you are having adding both the iOS Frameworks and the WatchKit's frameworks is the exact same one I had
I solved this by copying both folders [iOS] [watchkit] into my Xcode project's folder, then adding the respective libraries to their target's Embedded Binaries section.
In this way, you will not be literally copying the same-named items to the same location and it will not throw the error!

iOS Sharing .h file between main app and share extension

I have all constant key in .h file. After that, I share that one between my main app and my share extension. However, my share extension doesn't seem to read that file and I can't use constant key from there. How shall I do?
You need to make an embedded framework that is imported by both the app target and the share extension target. That is how to share code of any kind between targets.

How can I reference a Swift class in my WatchKit extension from the core iOS code?

I am trying to make my iOS application send the WatchKit Extension a set of data each time that data is updated from the server.
This is what the code in my iOS app looks like (names changed)
[WatchKitDataModel loadDataFromSource: currentData]
However, the iOS application does not recognize the WatchKitDataModel.
The error is "use of undeclared modifier."
Please help me make my apps talk to one another!
Ensure that your offending class is added to both the App target and the WatchKit target in the Document panel
The picture shows a TodayExtension but the principle is the same.
To share code between your WatchKit Extension and its containing app, create an embedded Framework in your app. Frameworks are the recommended tool by Apple to share code, within an app, and between apps.
To create an embedded Framework in Xcode 6, select your project and go to File > New > Target... Then in the dialog that opens select iOS > Framework & Library > Cocoa Touch Framework. Click Next. Give it a name, SomethingKit, similar to what Apple uses, e.g. UIKit, HealthKit, WatchKit. Now create your new classes and other common code in files inside your new Framework. When you want to use it in any of your other targets, i.e. the WatchKit Extension or its containing app, don't forget to import SomethingKit.
Ah, and don't forget to give public access level to the public-facing classes and functions in your Framework.
For more information about Frameworks to share code, for example, what sort of things cannot be included in Frameworks, see Apple's article: Using an Embedded Framework to Share Code.
There is also an interesting WWDC 2014 session video about this: Building Modern Frameworks.

Resources