Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug an iOS app extension? [duplicate]

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.

like image 603
pguzman Avatar asked Jan 29 '18 17:01

pguzman


People also ask

How do I debug a shared extension?

Go to that app on your device, create/choose whatever it is in the app that you want to share, click the share icon, choose your extension in the list of active sharing extensions. Then when your sharing extension starts up the debugger will automatically attach to it.

Can you debug on iPhone?

Here's how: Open the iPhone Settings menu. On an iPhone with an early version of iOS, access the Debug Console through Settings > Safari > Developer > Debug Console. When Safari on the iPhone detects CSS, HTML, and JavaScript errors, details of each display in the debugger.


3 Answers

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.

like image 58
mfaani Avatar answered Sep 29 '22 15:09

mfaani


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.

like image 32
pguzman Avatar answered Sep 29 '22 14:09

pguzman


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.

like image 44
sundance Avatar answered Sep 29 '22 13:09

sundance