什么是预处理器宏来测试是否正在构建应用程序扩展? [英] What is the preprocessor macro to test whether an application extension is being built?

查看:75
本文介绍了什么是预处理器宏来测试是否正在构建应用程序扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题完全基于公开发布的有关在iOS中引入应用程序扩展的文档。



随着 app extensions ,它是现在可以扩展您的应用之外的自定义功能和内容,并在用户使用其他应用时将其提供给用户。



在我的扩展实现中,我我在我的扩展程序(模型等)中包含了我实际应用程序中的一些类。问题是这些类调用 UIApplication ,这在app扩展中是不可用的,编译器告诉我。



我认为一个简单的解决方案是在 #if 中附加对 UIApplication 的任何调用指令。



例如,如果我想在模拟器上运行时只包含代码,我会使用:

  #if TARGET_IPHONE_SIMULATOR 
//代码此处
#endif

当目标是应用程序扩展时,是否有类似定义的宏?

解决方案

你可以定义你自己的宏。



在项目设置中,使用顶部栏中的下拉列表选择你的扩展目标:



然后:



< blockquote>


  1. 点击构建设置

  2. Apple LLVM 6.0 - 预处理
  3. 下查找(或搜索)预处理器宏
  4. 在调试和发布部分添加 TARGET_IS_EXTENSION 或您选择的任何其他名称。


然后在你的代码中:

  #ifndef TARGET_IS_EXTENSION 
//拨打UIApplication电话
#endif


This question is based purely on publicly released documents regarding the introduction of application extensions in iOS.

With the introduction of app extensions in iOS 8, it is now possible to "extend custom functionality and content beyond your app and make it available to users while they’re using other apps".

In my implementation of my extension, I am including some classes from my actual app in my extension (models, etc). The problem is that these classes make calls to UIApplication, which is not available in an app extension, and the compiler tells me so.

I thought an easy solution to this would to be enclose any calls to UIApplication in an #if directive.

For example, if I wanted to only include code if I was running on a simulator, I would use:

#if TARGET_IPHONE_SIMULATOR
    // Code Here
#endif

Is there a similar defined macro when the target is an application extension?

解决方案

You can define your own macro.

In the project settings use the dropdown in the topbar to select your extension target:

Then:

  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.

Then in your code:

#ifndef TARGET_IS_EXTENSION
    // Do your calls to UIApplication
#endif

这篇关于什么是预处理器宏来测试是否正在构建应用程序扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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