如何使用 NSLog 调试 iOS 8 扩展?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24031612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:22:30  来源:igfitidea点击:

How to debug iOS 8 extensions with NSLog?

iosios8xcode6ios-app-extension

提问by ohho

- (void)viewDidLoad
{
    NSLog(@"%s", __func__);
    // ...
}

in viewDidLoadof an iOS 8 extension. the NSLogoutputs nothing in Xcode. NSLogworks as usual in the container app though.

viewDidLoadiOS 8扩展中。在 NSLog输出没有在Xcode。NSLog不过,在容器应用程序中照常工作。

How can I get output from debug messages from an extension?

如何从扩展的调试消息中获取输出?

回答by Vince Yuan

  1. Debugging works for app extensions.
  2. It works on simulator too.
  3. If your app ext crashes in the simulator, you may find it is not easy to restart your app ext. Restarting your simulator is a quick solution.
  4. Steps to debug an app extension:

    1. Run the container app. In this step, Xcode uploads the container app and app extension to the device or simulator.

    2. Stop the container app. This step is important when you debug in simulator. If you don't do it, Xcode will tell you the simulator is in use.

    3. In Xcode, press menu Debug -> Attach to Process -> By Process Identifer (PID) or Name..., input the app ext's identifier, e.g. com.abc.ContainerApp.MyExtension, to start debugging. Don't forget to set break points. (Update on Aug 25, 2014: you can input MyExtension(your extension's name) directly.)

    4. In the device or simulator, open your app extension.

  1. 调试适用于应用程序扩展。
  2. 它也适用于模拟器。
  3. 如果您的 app ext 在模拟器中崩溃,您可能会发现重新启动 app ext 并不容易。重新启动模拟器是一个快速的解决方案。
  4. 调试应用扩展的步骤:

    1. 运行容器应用。在这一步中,Xcode 将容器应用程序和应用程序扩展上传到设备或模拟器。

    2. 停止容器应用程序。在模拟器中调试时,这一步很重要。如果你不这样做,Xcode 会告诉你模拟器正在使用中。

    3. 在Xcode中,按菜单Debug -> Attach to Process -> By Process Identifer (PID) or Name...,输入app ext的标识符,例如com.abc.ContainerApp.MyExtension,开始调试。不要忘记设置断点。(2014 年 8 月 25 日更新:您可以直接输入 MyExtension(您的扩展名)。)

    4. 在设备或模拟器中,打开您的应用扩展程序。



Updates on Aug 23, 2014:

2014 年 8 月 23 日更新:

I found the debugging steps above do not work well on Xcode 6 beta 6 with iOS 8 SDK beta 5 on the simulator.

我发现上面的调试步骤在 Xcode 6 beta 6 和模拟器上的 iOS 8 SDK beta 5 上不能很好地工作。

Solution:

解决方案:

  1. Run your extension in the simulator.
  2. Xcode menu Debug -> Attach to Process -> Choose "MyExtension(your extension's name)" in System section in the menu.
  1. 在模拟器中运行您的扩展。
  2. Xcode 菜单 Debug -> Attach to Process -> 在菜单的 System 部分选择“MyExtension(your extension's name)”。

The breakpoints work. But I don't know why logs do not show in the output window.

断点起作用。但我不知道为什么日志没有显示在输出窗口中。

回答by BalestraPatrick

I'm having this problem too. It works for me if you go in your Simulator under the menu Debug -> Open System Log...

我也有这个问题。如果您在菜单 Debug -> Open System Log... 下进入模拟器,它对我有用。

From here you can see all the logs of the iPhone Simulator (included your extension's logs).

从这里您可以看到 iPhone 模拟器的所有日志(包括您的扩展程序的日志)。

enter image description here

在此处输入图片说明

回答by Santa Claus

NSLogis working perfectly.

NSLog工作完美

You just don't see what is being logged in the debug area of Xcode because the Xcode debugger isn't attached to your extension. Extensions are nearlycompletely independent from their containing app. They have separate bundle identifiers, for example, and they also are separate processes on the OS.

您只是看不到 Xcode 调试区域中记录的内容,因为 Xcode 调试器未附加到您的扩展。扩展几乎完全独立于它们包含的应用程序。例如,它们具有单独的包标识符,并且它们也是操作系统上的单独进程。

I have had varied success in getting Xcode to attach to extensions. Supposedly it seems it would attach automatically, and it appears in the debug navigator as "Waiting to attach", but never attaches.

我在让 Xcode 附加到扩展方面取得了不同的成功。据说它似乎会自动附加,并且在调试导航器中显示为“等待附加”,但从未附加。

Sometimes, I am able to run my extension target in Xcode:

有时,我可以在 Xcode 中运行我的扩展目标:

enter image description here

在此处输入图片说明

And then have the option to choose what application to run my extension in. In this case, I would choose its recommendation of "today", which is notification center.

然后可以选择在哪个应用程序中运行我的扩展程序。在这种情况下,我会选择它的推荐“今天”,即通知中心。

enter image description here

在此处输入图片说明

And then it would sometimesattach the debugger to my extension. Note this method only works on physical devices, it seems.

然后它有时会将调试器附加到我的扩展。请注意,此方法似乎仅适用于物理设备。

If it doesn't attach you can use the manual attachment method in @VinceYaun's answer,

如果没有附加,您可以在@VinceYaun 的回答中使用手动附加方法,

I have also had varied success using other methods of attachment. Most have been unsuccessful, and it seems they are just bugs that will be fixed at a later date.

我使用其他依恋方法也取得了不同的成功。大多数都没有成功,似乎它们只是稍后会修复的错误。

To view your log messages you go to Window-> Devicesin the top bar and select your device. You can bring up the device log from the bottom of that window. If you are testing on a simulator, you can use @BalestraPatrick's answer.

要查看您的日志消息,请转到顶部栏中的Window->Devices并选择您的设备。您可以从该窗口底部调出设备日志。如果您在模拟器上进行测试,则可以使用 @BalestraPatrick's answer

Some of the bugs have been fixed in Beta 2, and my guess is that eventually the debugger will attach automagically when launching the extension.

Beta 2 中已经修复了一些错误,我猜测最终调试器将在启动扩展时自动附加。

Update:In the iOS 8 Beta 4 release notes:

更新:在 iOS 8 Beta 4发行说明中

Extensions

Fixed in beta 4

  • Extensions sometimes fail to launch when debugging from Xcode.
  • When Extension with UI is killed, it relaunches and is not dismissed.
  • Sometimes your Sharing or Action extension can hang.
  • Redeploying an extension may disable it in Notification Center.

扩展

已在测试版 4 中修复

  • 从 Xcode 调试时,扩展有时无法启动。
  • 当带有 UI 的扩展被终止时,它会重新启动并且不会被解除。
  • 有时,您的共享或操作扩展程序可能会挂起。
  • 重新部署扩展可能会在通知中心禁用它。

回答by Masalis

I'm having this problem too. Xcode never attaches my debugger to the extension or displays NSLog messages. If you attach your Xcode debugger manually to your extension process, at least breakpoints work like a charm:

我也有这个问题。Xcode 从不将我的调试器附加到扩展或显示 NSLog 消息。如果您手动将 Xcode 调试器附加到您的扩展进程,至少断点工作就像一个魅力:

Debug->Attach to process->Your extension name (mine was "com.example.MyExtensionApp.MyExtension")

回答by Maciek Czarnik

  1. Create scheme for your extension
  2. Run scheme
  3. In a dialog choose container app
  4. Enjoy
  1. 为您的扩展创建方案
  2. 运行方案
  3. 在对话框中选择容器应用
  4. 享受

It works for me:)

它对有用:)

回答by Jano

Xcode 8 is able to debug extensions:

Xcode 8 能够调试扩展:

  1. Choose the extension scheme in the combo next to the stop button and run it.
  2. Select the parent application in the dialog that appears.
  1. 在停止按钮旁边的组合中选择扩展方案并运行它。
  2. 在出现的对话框中选择父应用程序。

Result: breakpoints and log work as usual.

结果:断点和日志照常工作。

回答by Andrew

What finally allowed me to see the log in the debugging area, based on Michael's suggestionand Apple's documentation, is:

根据迈克尔的建议苹果的文档,最终让我在调试区看到日志是:

Build & Run the app extension in the simulator and, when prompted for a host app, choose the specific app that you're going to call the extension from. In my case I was launching my Action extension from Safari by pulling up a share sheet on a PDF.

在模拟器中构建并运行应用程序扩展,当提示输入主机应用程序时,选择您要从中调用扩展程序的特定应用程序。就我而言,我通过在 PDF 上拉出共享表来从 Safari 启动我的 Action 扩展。

What wasn't working before was following other people's suggestions of using Today as the host but then leaving that app and going over to Safari to call my extension. I no longer even need to run my containing app first before running the extension.

之前不起作用的是遵循其他人的建议,即使用 Today 作为主机,然后离开该应用程序并转到 Safari 调用我的扩展程序。在运行扩展程序之前,我什至不再需要先运行我的包含应用程序。

From Apple's Documentation:

来自苹果的文档:

In your extension scheme's Run phase, you specify a host app as the executable. Upon accessing the extension through that specified host's UI, the Xcode debugger attaches to the extension.

在您的扩展方案的运行阶段,您指定一个主机应用程序作为可执行文件。通过指定主机的 UI 访问扩展后,Xcode 调试器将附加到扩展。

回答by AechoLiu

Encounter the same problem about extension for NSLog, and break points. I have fought it with many days.

遇到同样的问题,扩展为NSLog, 和断点。我已经和它斗争了很多天。

Device logcan be found as following image. It is at XCode -> Window -> Devices and Simulators.

Device log可以找到如下图。它在XCode -> Window -> Devices and Simulators

After enter the Open Console, there is a search field at the top-right of dialog. I can apply filter rule in there. For example, a process name contains Notificationkey word, or process name must equal to the name of extension target, ex: equal to MyNotificationServiceExtensionprocess name.

输入 后Open Console,对话框右上角有一个搜索字段。我可以在那里应用过滤规则。例如,进程名称包含Notification关键字,或者进程名称必须等于扩展目标的名称,例如:等于MyNotificationServiceExtension进程名称。

Device log

设备日志

回答by Michael

I actually got the logs to run quite simply in Xcode 6.3. Firstly, build and run the containing app. Once the containing app is running on the device, build and run the app extension by changing the scheme to the app extension.

我实际上让日志在 Xcode 6.3 中非常简单地运行。首先,构建并运行包含的应用程序。一旦包含的应用程序在设备上运行,通过将方案更改为应用程序扩展来构建并运行应用程序扩展。

Apple has documentation directly related to debugging, profiling and testing your app extension.

Apple 有与调试、分析和测试应用扩展直接相关的文档。

回答by Andriy Gordiychuk

A trick which works for me (although it is quite an ugly one) is to place a dummy UILabelsomewhere in the bottom corner of my extension. I usually call it logLabel. It is then possible to update the text of this label with any log statement that you want to get logged. Such approach is not very good if you need to log statements from instances of different classes. And, obviously, it clutters your UI.

一个对我有用的技巧(虽然它很丑)是UILabel在我的扩展的底角放置一个假人。我通常称之为logLabel。然后可以使用您想要记录的任何日志语句更新此标签的文本。如果您需要记录来自不同类的实例的语句,这种方法不是很好。而且,很明显,它会使您的用户界面变得混乱。

However, if you have a fairly simple widget and you don't mind about slight cluttering of the UI this does the trick. I have tried all other solutions outlined in this discussion and, sadly, none of them worked for me.

但是,如果您有一个相当简单的小部件并且您不介意 UI 的轻微混乱,那么这就是诀窍。我已经尝试了本次讨论中概述的所有其他解决方案,但遗憾的是,它们都不适合我。