Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to detect if my framework is running as part of the main app or an extension?

In an iOS or OS X Framework, which contains common code for both its App, and app Extensions, is there a way to detect if the code is being run under the main app, or one of its extensions? Specifically I'd like to detect if the framework is being used as part of a WatchKit extension as opposed to within the iPhone part of the App.

UIDevice.currentDevice always returns the iPhone as that is what is running the code. I believe I could check if WKInterfaceDevice exists, but that doesn't seem too elegant.

like image 847
Doug Avatar asked Jan 28 '15 19:01

Doug


People also ask

What is the iOS app extension?

App Extensions are an iOS feature that allows developers to extend the functionality and content of their app beyond the app itself, making it available to users in other apps or in the main operating system.

What is an extension in Xcode?

Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you don't have access to the original source code (known as retroactive modeling). Extensions are similar to categories in Objective-C.

What is appex?

IT Managers & Contractors Whether you're the trusted in-house IT expert or the go-to contractor, Appex tailors integrations that meet the exact needs of the businesses you serve, faster and for less cost than you'd imagine.


1 Answers

One option is to check the file extension of the current target. This has the advantage that it works in shared libraries and frameworks, whereas other options often only work within a target itself:

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

This answer was informed by this question and answer. Those answers also outline how to set a preprocessor macro, which would be another good option in some cases, though that wouldn't be accessible from your framework.

I've not marked this question as a duplicate of that one, however, as these options are generic to all App extensions, neither is particularly elegant, and there may be WatchKit-specific options for what you're trying to achieve.

like image 184
Duncan Babbage Avatar answered Oct 05 '22 12:10

Duncan Babbage