iOS Sharing .h file between main app and share extension - ios

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.

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

Accessing a class of iPhone app in Watch App

I want to use my already developed NSObject Class in watch app (Watch App target is added to same project).
My iPhone App is written in Objective-C and now I am using Swift for Watch Extension, so I created a Bridge-Header to use this class in Watch App and add the class to Watch Extension target. When I build the app, it gives me many errors from other classes with this reason:
Cannot find interface declaration for UIControllerView, SuperClass of xxxxClass
And all these error files seem that they have a target of WatchKit Extension, but I didn't include them. On these files, Target Membership Watch Extension is also unchecked (means not a target of extension). Here are the Classes which I didn't added to Watch Extension, but they are running as a Watch Extension class.
You will need to select that class and check Target membership for WatchKit extension From file inspector.
For Swift class :
For Objective-C class you make it for .m file
You have the check the targeting files are added or not.
For that:
1.select your project file -->Build Phase-->Compile Sources.
-Check the file present or not.
2.if not just remove and add the files that are all missing in compile source.
3.Clean and Build your source..

How sharing the model of the main app with WatchOS2?

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.

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