iOS 8扩展如何检测运行 [英] iOS 8 Extension How To Detect Running
问题描述
如果您在iOS 8扩展程序中运行,是否有人知道您是如何在代码中检测到的。
Does anyone know how you detect from within your code if you're running inside an iOS 8 Extension.
我有一个应用程序,它在应用程序和延期。应用程序代码使用 [UIApplication sharedApplication]
,但这在扩展程序中不可用,因此它不会编译说:
I have an app which shares classes between an app and an extension. The app code uses [UIApplication sharedApplication]
but this isn't available from within an extension, so it won't compile saying:
'sharedApplication'不可用:不可用iOS(App Extension)
所以我需要一个如果是这种情况,检测我是否在扩展中并使用 sharedApplication
的方法。
So I need a way to detect that I'm in the extension and use an to sharedApplication
if that's the case.
推荐答案
您可以使用预处理器宏:
You can use a preprocessor macro:
在项目设置中,使用顶部栏中的下拉列表选择您的扩展目标:
In the project settings use the dropdown in the topbar to select your extension target:
然后:
- 点击
构建设置
- 在
Apple LLVM 6.0 - 预处理
- 在debu中添加
TARGET_IS_EXTENSION
或您选择的任何其他名称g和发布部分。
下查找(或搜索)
预处理器宏
- Click
Build Settings
- Find (or search)
Preprocessor Macros
underApple LLVM 6.0 - Preprocessing
- Add
TARGET_IS_EXTENSION
or any other name of your choice in both the debug and release sections.
然后在你的代码中:
#ifndef TARGET_IS_EXTENSION // if it's not defined
// Do your calls to UIApplication
#endif
这篇关于iOS 8扩展如何检测运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!