How to debug an iOS app extension?

I found a way, it's not perfect but at least i can see what i print now. I used os_log("this is what i will see") function (you have to import os)

1- i set an environment variable OS_ACTIVITY_MODE = default (not disable) in my extension's scheme

2- i run build my application in my device

3- i attach my extension to a process (my app)

4- you can see your logs in the Console app (Open /Applications/Utilities/Console.app) or opening Windows -> Devices and Simulators to see your devices console.

The Console app option is the best.


You can debug an app extension like any other app by just choosing target and device and then running the extension. You need to choose an app to run with the extension in the dialog opening.

After you started the extension from the app the console clears and you see anything you print with NSLog or print. The NSLog messages should also appear in the device logs but are very hard to detect.

I recommend using QorumLogs, which gives you a structured and colored log output, so you will not miss any messages.


Knowing that:

  • At any given time you can only debug 1 target
  • The target must be attached to Xcode's debugger
  • The App Extension is a separate target from its containing app (target)

Solution:

Go to Xcode >> Debug >> Attach to process >> Then just select your appExtension target

Note:

If you're trying to debug an extension that is trigged by the OS then you need to do something so that the OS triggers it. Only then you can attach it to the debugger. Example: If you're trying to debug the 'Notification Service Extension' then you'd only see it among the list of targets after you've connected your iPhone to your Mac and have a push notification sent to open the service extension.

FWIW I also ran into weird issues were running the app would just crash on launch. The fix was to detach the appex from the debugger and launch the normal app and then attach the appex again.


I believe if you also use os.log it would also just log to the console regardless, but I'm not 100% positive about that.