WatchKit:'sharedApplication'不可用:iOS上不可用(App Extension) - 在适当的地方使用基于视图控制器的解决方案 [英] WatchKit : 'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead
问题描述
我在监视工具包扩展的构建阶段添加了Manager.m(iOS类)。但是,我收到以下错误
I've added Manager.m (iOS class) in my build phase in the watch kit extension. However, I got the following error
'sharedApplication'不可用:iOS上不可用(App
Extension) - 使用视图适当的基于控制器的解决方案
而不是
'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead
这是我的Manager.m中导致问题的行。
Here is the line in my Manager.m that is causing the problem.
AppDelegate *appDelegate =
(AppDelegate *)[[UIApplication sharedApplication] delegate];
我想在Manager.m中使用代码,但这些代码行导致问题并赢了让我运行手表App。有没有解决的办法?我不想重写整个类只是为了容纳watchkit。
I want to use code in Manager.m, but lines of code like these are causing issues and won't let me run the watch App. Is there a way around this? I don't want to rewrite the whole class just to accommodate watchkit.
推荐答案
定义/添加一个宏(例如WATCH_KIT_EXTENSION_TARGET)您的手表套件扩展程序的目标构建设置,并使用它来有选择地构建代码。例如。由于sharedApplication在iOS扩展程序上不可用,因此您可以像下面这样编码
Define/Add a macro (e.g. WATCH_KIT_EXTENSION_TARGET) in your watch kit extension's target build settings and use it to selectively build code. E.g. as sharedApplication is not available on iOS extension, so you can code like below
#ifndef WATCH_KIT_EXTENSION_TARGET
AppDelegate *appDelegate =
(AppDelegate *)[[UIApplication sharedApplication] delegate];
#endif
这篇关于WatchKit:'sharedApplication'不可用:iOS上不可用(App Extension) - 在适当的地方使用基于视图控制器的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!