Sharing class between iOS app and extension app
I want to use a custom class in both my app and the extension app. If I just have the class in the main app folder, the extension doesn't pick up on it. I tried making a copy of it and putting it into the extension folder, but I of course get a invalid redeclaration of [my class]
error. What's the best solution for this? It's just one class, not multiple resources.
Nothing should be copied. Just add required files to compile sources for your extension:
Just make sure that the files you want to share gets a tick in the section Target Membership .
Select your file, open the utility panel on the right and tick the appropriate target under Target Membership .
Embedded frameworks are a great way to reuse code between your application and your extension. Having moved some of the code to embedded frameworks will provide us with a great opportunity to write some unit tests around it.
See section Using an Embedded Framework to Share Code https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1
App Extension(Widget, Today)
App Extension
- from iOS v8 - is a packaged bundle
which uses different process than Containing App
and can communicate with Host App
. It has .appex
file extension and is located in PlugIns
folder. Something similar to CXTests [About]
Application packaged bundle
SpeedWidgetExtension.appex - Application Extension packaged bundle
App Group
[About]Target Membership
- the simplest way or for pre iOS v8 which allows you to add files into every target. It increases a footprint of package because this file will be duplicated for every target
Framework
- module for grouping/reusing code
Embedded framework
aka Dynamic framework
- the most appropriate way to share common code base. Please note that for using Embedded framework
you should embed it only in Containing App
(and don't add this framework in App Extension
. If you include framework and for App Extension
it will have the same framework in App Extension
bundle - framework will be duplicated). It is possible because of @prath
[About] //App Extension LD_RUNPATH_SEARCH_PATHS = @executable_path/../../Frameworks //It means that App Extension can uses Dynamic framework from Containing app
Static Framework
- link the code to executable binary. You can link library only for Containing App
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.