3

There's a similar question that works on Objective-C, but I tried the same code in Swift and it never executes, neither in the main app, nor in the action extension.

My situation is similar to the one in the question above, that is, when running from the main app I want to use UIApplication.shared.open to open a link in Safari, but I want to ignore this part of the code on the App Extension.

The problem isn't finding out whether the app is running from an App Extension or not, but ignoring the code when building for the App Extension, so that the compiler does not give me the following error on build:

enter image description here

Marcos Tanaka
  • 3,112
  • 3
  • 25
  • 41

2 Answers2

3

You could introduce a new Custom Flag (similar to the DEBUG flag) for the extension target. In your Build Settings look for the Custom Flags and add a new one (e.g. "EXTENSION"). Like here in the screenshot, but also do it for release. Extension Configuration

In your Code you could then do something like

#if EXTENSION
    // here goes code that is only compiled in the extension
#else 
    // here goes code that is only compiled outside the extension 
#endif
Tiago Mendes
  • 4,572
  • 1
  • 29
  • 35
Robin Bork
  • 297
  • 2
  • 8
0

Update: Please read the Apple provided documentation on App Extensions:

Some APIs Are Unavailable to App Extensions

Because of its focused role in the system, an app extension is ineligible to participate in certain activities. An app extension cannot:

  • Access a Application.shared object, and so cannot use any of the methods on that object

- Apple, App Extension Programming Guide

To programmatically find if the it the running extension via code it's really simple, just do this:

let bundleUrl: URL = Bundle.main.bundleURL
let bundlePathExtension: String = bundleUrl.pathExtension
let isAppex: Bool = bundlePathExtension == "appex"

// `true` when invoked inside the `Extension process`
// `false` when invoked inside the `Main process`
Community
  • 1
  • 1
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • 1
    Thanks, but this doesn't ignore the code when building for the App Extension, which causes the compiler error I shared in the question. – Marcos Tanaka Jun 02 '20 at 19:08
  • Check out my update. You might wanna update the files in your target according to the apple documentation. – Frankenstein Jun 02 '20 at 19:36