iOS Today Widget share source file with main application - ios

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

Related

iMessage app, "disallowed nested bundles" error trying to archive/upload with binary framework

I have an iMessage app (not an app with an iMessage extension) in which I have successfully added a binary framework (the project runs just fine on device and simulator)
However, I cannot successfully upload the project to App Store Connect - upload from the archive build returns the following errors:
The relevant text of the error is:
The bundle ... contains disallowed nested bundles. Refer to https://developer.apple.com/go/id=framework-imessage
That link (if you follow the instructions for using an newer Xcode version) leads you back to the link below I used to add the framework to begin with... to run you will need Xcode 11 as I am using an XCFramework.
I added the framework to the iMessage app as instructed by Apple here:
https://developer.apple.com/library/archive/technotes/tn2435/_index.html
(see Embedding a Framework in an iMessage App section)
What do I need to change to the settings for the project or extension in order for the archive/upload process to succeed, while actually including the framework I need? I have searched on StackOverflow, and found a variety of posts related to cocoapods, or around various build settings of "Always Embed Swift Standard Libraries" that do not help.
I have reduced the problem down to a simple sample app you can see here, which builds and runs just fine but cannot be archived and uploaded:
https://www.dropbox.com/sh/jpa4oe7zlnb21wl/AACXkLbxIbayZUtJr3VDwO07a?dl=0
That directory contains a zip file of the project, and an image showing the error encountered.
You have .xcframework in your project. May be you haven't enable Build Libraries for Distribution in Build setting when you have build .xcframework. You can refer this link for this.
Edit:
Error message is Invalid Bundle. So check bundle name of message extension and frameworks which are in the .xcframework.
I've made a few changes regarding the stub app, it seems to work and validate ok.
Remove the Embed Framework from the extension target.
Add the Embed Framework in the app target, set the Destination to 'Frameworks'

Firebase IOS Crash Reporting with Embedded Framework

I have a project which contains 3 targets
The application target
The Embedded Framework
The today extension
Which target should I include the Firebase Crash Framework into ?
Currently I have included it into the embedded framework so that both the app and extension might use it.
The problem is that symbol files related to the application are not being uploaded automatically.
How do I get about this ?
Using the Firebase Crash SDK inside an Embedded framework and making the framework included in the App and the Today extension should work.
If you want to symbolicate the app related symbols, use the steps mentioned in https://firebase.google.com/docs/crash/ios#upload_symbol_files. This will enable the app/extension to have their symbols de-symbolicated.

Adding Framework and App Extension targets to IOS App project, am I doing it correctly?

I'm studying IOS/Swift development and I'd like to build an IOS app that in the future will have its own Today Extension. I've read that it is better to put the common logic (for example the logic that accesses Internet resources, performs some job on the result etc) between the App and its extension into a Custom Framework.. Since I think I'll need to change both Framework and App code during the development of the App, I'd like to avoid building the framework in a separate project, than use Show In Finder function to locate the framework bundle and then drag-and-dropping it within the App project.. So I'd like to know if this is the correct way to set up a Workspace with an App, the framework it uses and the Today Extension of the App:
Create a new Project with an IOS App target
Select the project in the Project Navigator and then Editor->Add Target... and specify a Cocoa Touch Framework
Editor->Add Target... and specify Today Extension
Add the framework in Linked Frameworks and Libraries
In this way I've seen that changes to the Framework sources do not need a Framework target recompilation, maybe because the project knows about the need to recompile framework sources even if I only build & run the IOS App target that uses that framework? Am I doing it right? I've seen that the Today Extension created in such way does not lists the framework in the Build Phases -> Target Dependencies while the IOS App does, but both of them correctly use the updated code of the Framework when I update it.
Then, I've noticed the following warning when I compile the project, but I don't know if it is related to the way I've setup the project:
ld: warning: linking against dylib not safe for use in application extensions: /Users/gianni/Library/Developer/Xcode/DerivedData/TestFrameworkApp-dshihhfiuepeqzddbnpgnfwilhem/Build/Products/Debug-iphonesimulator/TestFramework.framework/TestFramework
BTW is it normal that the entries within the Products folder are always red? Is it due to the fact that I build only for IOS Simulator since I don't have a Paid developer account that allows me to build for an IOS Device? Does this prevent me to build a framework that can be exported for some other Project since I can't find the Framework bundle with a right clicking on the framework product and selecting Show In Finder (and so I can't drag-and-drop it to another project)?
I hope I've been clear enough and that you'll point me in the right direction to understand what is the suggested way of starting my new project :)
It sounds to me like you're headed down the exact path I would take.
Regarding the warning you're seeing... I believe that checking the "Allow app extension API only" for your shared framework will supress that and cause build failures when you attempt to use a API that is not allowed in extensions.

Issue with nested Framework for Watchkit Extension

My Watchkit app does not work in production (TestFlight && rejected from AppStore).
I think the issue come from a nested framework. Here the architecture :
I've made a framework "ModelLayer" (Cocoa Touch Framework) with the app's model, so it can be shared across all targets. This framework use another third party framework (UICKeyChainStore.framework).
And the apple watch extension use "ModelLayer".
When debugging everything works fine. But when I uploaded the archive on iTunes connect I had an error saying that nested frameworks where not allowed. So I removed the copy file phase that I had in the Build Phase of "ModelLayer"'s target. It did not worked well as the app has been rejected...
I found some threads on internet that says that the copy file phase should have as Destination 'Shared Frameworks' and not 'Frameworks' (as I've done before). So I tried this, I've uploaded a pre-release of my app on itunes connect and tried the app with testflight. The app is now on my Apple Watch, but impossible to load the app. The loader stay on screen.
Did I miss something ? How can I use UICKeyChainStore.framework in my 'ModelLayer' framework ??
Thanks in advance !
So I found the solution.
The third party framework in my custom framework was not the problem. But It was my custom framework in the watch kit extension. So it's not possible to embed directly a framework in an extension. The framework should be embedded by the main app. And the extension will be able to use it.
Add the framework to the main app target
In Build Phases Tab
Use the framework in extension target
In General Tab
And in Build Settings Tab -> Linking
Add #executable_path/../../Frameworks to Runpath Search Paths

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