问题
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.
回答1:
Nothing should be copied. Just add required files to compile sources for your extension:
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/28797242/sharing-code-between-original-ios-app-and-app-extension