检测运行目标是App Extension还是iOS App [英] Detect whether run target is App Extension or iOS App

查看:655
本文介绍了检测运行目标是App Extension还是iOS App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,它也有iOS 8 Today Extension(或小部件)。我正在做的一件事是在我的应用程序和我的小部件之间共享代码类,因为显然这使我不必拥有相同代码的多个副本。这一切都很好地解决了我遇到的一个问题,其中一个API UIApplication sharedApplication 出现了错误,因为你无法在iOS 8 Widgets上使用它。 / p>

所以我想到的是,有一个简单的if语句来检查最终目标是本机应用程序还是它是iOS 8小部件然后添加代码在适当的时候。这会有用吗?



这是我的代码:

  if( UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
{
CGSize ssz = sz;
sz = CGSizeMake(ssz.height,ssz.width);
}

如何更改此代码,以便可以执行以下操作之一:


  1. 只运行/编译 IF 结束目标是本机iOS应用而非小部件。

  2. 检查目标平台上的API是否可用,如果是,则使用它?

我希望我的问题有道理并感谢您的时间,Dan。

解决方案

运行时间检查已经结束,因为Xcode不允许您编译使用 sharedApplication 的扩展代码。它必须是编译时检查。



不幸的是,这个检查不是内置的,你必须添加它。您需要执行以下操作:


  1. 在今天扩展的项目设置中,在其他C标志下,添加您自己的自定义编译器宏。对于名为 TODAY_EXTENSION 的宏,添加 -DTODAY_EXTENSION


  1. 在您的代码中,使用

     <$ c之类的内容检查此宏$ c> #ifndef TODAY_EXTENSION 
    ...此处仅限应用代码...
    #endif

    相反,扩展程序中仅 的代码看起来像

      #ifdef TODAY_EXTENSION 
    ...此处仅限扩展名代码...
    #endif



I have a simple app which also has a iOS 8 Today Extension (or widget). One of the things I am doing is sharing code classes between my app and my widget, because obviously this saves me from having to have multiple copies of the same code. It all works nicely apart from one problem I am having, one of the APIs UIApplication sharedApplication is coming up with an error because you can't use that on iOS 8 Widgets.

So what I was thinking of, is have a simple if statement which checks if the end target is the native app OR if it is a iOS 8 widget and then add the code in as is appropriate. Would that work?

Here is my code:

if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
    {
        CGSize ssz = sz;
        sz = CGSizeMake(ssz.height, ssz.width);
    }

How can I change this code so that is can do one of the following:

  1. Only run/compile IF the end target is a native iOS app and not a widget.
  2. OR check to see if that API is available on the target platform and then use it if it is?

I hope my question makes sense and thanks for your time, Dan.

解决方案

Run time checks are out, because Xcode won't let you compile extension code that uses sharedApplication. It has to be a compile-time check.

Unfortunately this check isn't built in, you have to add it. You need to do something like:

  1. In the project settings for the today extension, under "Other C Flags", add your own custom compiler macro. For a macro named TODAY_EXTENSION, add -DTODAY_EXTENSION:

  1. In your code, check this macro using something like

    #ifndef TODAY_EXTENSION
    ... app-only code here ...
    #endif
    

    Conversely, code that should only exist in the extension would look like

    #ifdef TODAY_EXTENSION
    ... extension-only code here ...
    #endif
    

这篇关于检测运行目标是App Extension还是iOS App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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