ios8扩展如何检测运行。

时间:2021-06-07 00:19:57

Does anyone know how you detect from within your code if you're running inside an iOS 8 Extension.

如果你在ios8扩展中运行,有人知道你是如何在代码中检测的吗?

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:

我有一个在应用和扩展之间共享类的应用。应用程序代码使用[UIApplication sharedApplication],但是这在扩展中是不可用的,所以它不会编译说:

'sharedApplication' is unavailable: not available iOS (App Extension)

“sharedApplication”不可用:无法使用iOS(应用扩展)

So I need a way to detect that I'm in the extension and use an to sharedApplication if that's the case.

所以我需要一种方法来检测我在扩展中,并使用to sharedApplication,如果是这样的话。

5 个解决方案

#1


43  

You can use a preprocessor macro:

可以使用预处理器宏:

In the project settings use the dropdown in the topbar to select your extension target: ios8扩展如何检测运行。

在项目设置中,使用topbar中的下拉菜单来选择您的扩展目标:

Then:

然后:

  1. Click Build Settings
  2. 单击Build设置
  3. Find (or search) Preprocessor Macros under Apple LLVM 6.0 - Preprocessing
  4. 查找(或搜索)苹果LLVM 6.0下的预处理器宏——预处理
  5. Add TARGET_IS_EXTENSION or any other name of your choice in both the debug and release sections.
  6. 在调试和发布部分中添加TARGET_IS_EXTENSION或您选择的任何其他名称。

Then in your code:

然后在你的代码:

#ifndef TARGET_IS_EXTENSION // if it's not defined
    // Do your calls to UIApplication
#endif

#2


22  

The preprocessor macro will work mostly, but will not work in shared library (e.g. cocoapods or shared frameworks).

预处理器宏将主要工作,但不会在共享库(例如cocoapods或共享框架)中工作。

Alternatively you can use following code.

您也可以使用以下代码。

@implementation ExtensionHelpers

+(BOOL) isAppExtension
{
    return [[[NSBundle mainBundle] executablePath] containsString:@".appex/"];
}

@end

This work by checking the bundle executablePath, as only App Extension has extension ".appex".

这是通过检查bundle executablePath完成的,因为只有应用程序扩展名有扩展名“.appex”。

#3


20  

As Apple's documentation says:

像苹果的文档表示:

When you build an extension based on an Xcode template, you get an extension bundle that ends in .appex.

当您基于Xcode模板构建扩展时,您将得到一个以.appex结尾的扩展包。

So, we can use the following code:

因此,我们可以使用以下代码:

if ([[[NSBundle mainBundle] bundlePath] hasSuffix:@".appex"]) {
    // this is an app extension
}

#4


2  

You can add a preprocessor macro on the extension target and then check with a #ifdef inside of your class.

您可以在扩展目标上添加一个预处理器宏,然后在类内部使用#ifdef检查。

#5


0  

For my shared library I created a separate target who's app extensions flag is set to yes, and used preprocessor macro's within the build settings for that specific target.

对于我的共享库,我创建了一个单独的目标,它的应用程序扩展标志被设置为yes,并在特定目标的构建设置中使用了预处理器宏。

#1


43  

You can use a preprocessor macro:

可以使用预处理器宏:

In the project settings use the dropdown in the topbar to select your extension target: ios8扩展如何检测运行。

在项目设置中,使用topbar中的下拉菜单来选择您的扩展目标:

Then:

然后:

  1. Click Build Settings
  2. 单击Build设置
  3. Find (or search) Preprocessor Macros under Apple LLVM 6.0 - Preprocessing
  4. 查找(或搜索)苹果LLVM 6.0下的预处理器宏——预处理
  5. Add TARGET_IS_EXTENSION or any other name of your choice in both the debug and release sections.
  6. 在调试和发布部分中添加TARGET_IS_EXTENSION或您选择的任何其他名称。

Then in your code:

然后在你的代码:

#ifndef TARGET_IS_EXTENSION // if it's not defined
    // Do your calls to UIApplication
#endif

#2


22  

The preprocessor macro will work mostly, but will not work in shared library (e.g. cocoapods or shared frameworks).

预处理器宏将主要工作,但不会在共享库(例如cocoapods或共享框架)中工作。

Alternatively you can use following code.

您也可以使用以下代码。

@implementation ExtensionHelpers

+(BOOL) isAppExtension
{
    return [[[NSBundle mainBundle] executablePath] containsString:@".appex/"];
}

@end

This work by checking the bundle executablePath, as only App Extension has extension ".appex".

这是通过检查bundle executablePath完成的,因为只有应用程序扩展名有扩展名“.appex”。

#3


20  

As Apple's documentation says:

像苹果的文档表示:

When you build an extension based on an Xcode template, you get an extension bundle that ends in .appex.

当您基于Xcode模板构建扩展时,您将得到一个以.appex结尾的扩展包。

So, we can use the following code:

因此,我们可以使用以下代码:

if ([[[NSBundle mainBundle] bundlePath] hasSuffix:@".appex"]) {
    // this is an app extension
}

#4


2  

You can add a preprocessor macro on the extension target and then check with a #ifdef inside of your class.

您可以在扩展目标上添加一个预处理器宏,然后在类内部使用#ifdef检查。

#5


0  

For my shared library I created a separate target who's app extensions flag is set to yes, and used preprocessor macro's within the build settings for that specific target.

对于我的共享库,我创建了一个单独的目标,它的应用程序扩展标志被设置为yes,并在特定目标的构建设置中使用了预处理器宏。