Sharing code between original iOS App and App Extension

不想你离开。 提交于 2020-01-09 07:30:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!