iOS 8扩展如何检测运行 [英] iOS 8 Extension How To Detect Running

查看:189
本文介绍了iOS 8扩展如何检测运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在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:

然后:



  1. 点击构建设置

  2. Apple LLVM 6.0 - 预处理

  3. 下查找(或搜索)预处理器宏
  4. 在debu中添加 TARGET_IS_EXTENSION 或您选择的任何其他名称g和发布部分。

  1. Click Build Settings
  2. Find (or search) Preprocessor Macros under Apple LLVM 6.0 - Preprocessing
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆