How to debug an iOS app extension?

倾然丶 夕夏残阳落幕 提交于 2020-07-05 09:11:29

问题


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.


回答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.




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/48507016/how-to-debug-an-ios-app-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!