Watchkit - shared code - framework, app extension, app groups, add target or just copy files?

荒凉一梦 提交于 2019-12-12 06:16:35

问题


I have an existing app and I'm adding a Watch app to it. I have several .swift files with code from my main app that I'll need to use for the watchkit app (functions and some constants, etc). I'm confused by the options for sharing this code - how do I know which one to try and use?

Create an app group, my own framework, app extension? I actually started to just copy the .swift files into my watchkit app folder and noticed when you add a file to Xcode on the dialog that comes up you get the check boxes of "Add to targets" so that got me thinking - is there a way to just change the properties of my .swift files in the main app section and check off the WatchKit App on the "Add to Targets" there?

That seems like it would be the way to go, right?

thanks for any help


回答1:


I solved that Just clicking on the files and add them in both targets. (iphone app target and apple watch extension target).

To fix the sharedapplication problem I used this:

#if (SL_TARGET_APP_EXTENSION == NO)
    [[UIApplication sharedApplication] .....
#endif 

SL_TARGET_APP_EXTENSION is a preprocessor macro added in the apple watch extension target.

Hope it helps.




回答2:


To share code between an apple watch app and your main app, you'll need to create a framework with that shared code. You said it yourself, an apple watch app is an entirely new target - it's not going to know squat about your existing code.

Creating an app group, you can also share NSUserDefaults: using initWithSuiteName and for Coredata, you can go the shared container route.



来源:https://stackoverflow.com/questions/29494040/watchkit-shared-code-framework-app-extension-app-groups-add-target-or-jus

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