10

I'm trying to debug an iOS app extension and all I can do is to set breakpoints but it would be nice to be able to print to console.

Is this possible with Xcode 9?

Or at least read it somewhere, maybe a file?

I went to see my device's logs in "devices and simulators" but could't find what I printed from my iOS app extension.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
pguzman
  • 141
  • 1
  • 7
  • 2
    https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/debugging_tools.html – koen Jan 29 '18 at 17:49
  • @Koen apparently it's not the same as debugging an app, so this is not working for me. – pguzman Jan 30 '18 at 08:46
  • 1
    **This question has already an answer at: [How to debug an iOS extension (.appex)?](https://stackoverflow.com/questions/33524051/how-to-debug-an-ios-extension-appex)** – Top-Master Sep 28 '21 at 12:00
  • @Top-Master Do you mind adding your own answer? I honestly didn't understand the code you included in my own answer and had to roll it back. It was a huge change. It would be great if you can explain what the code is doing line by line as it wasn't standard Swift. It was C – mfaani Sep 28 '21 at 15:39
  • @Honey see [Detect if Swift app is being run from Xcode](https://stackoverflow.com/a/33177600/8740349) (I added comments there instead, and in the post which this is marked as duplicate of) – Top-Master Sep 28 '21 at 21:29

4 Answers4

6

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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • This answer, and especially the note about the extension's process not showing up in the list until it's been triggered, was exactly what I was looking for. I still can't get a breakpoint set in the extension code to be tripped, though. – NRitH Aug 30 '21 at 19:30
4

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.

pguzman
  • 141
  • 1
  • 7
1

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.

sundance
  • 2,930
  • 1
  • 20
  • 25
  • 1
    i did what you said but nothing, i also tried attaching the extension to process (my main app) but nothing on the console or device's logs. – pguzman Jan 30 '18 at 08:41
  • i think QorumLogs uses os_log so it should display in the console, i will try it later, thanks – pguzman Jan 30 '18 at 11:08
0

You can use "placeholder" variable to print some debug messages

Dmih
  • 530
  • 6
  • 9