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

in viewDidLoad of an iOS 8 extension. the NSLog outputs nothing in Xcode. NSLog works as usual in the container app though.

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

idmean
  • 14,540
  • 9
  • 54
  • 83
ohho
  • 50,879
  • 75
  • 256
  • 383
  • I have this issue too. Maybe NSLog is just broken. I use Breakpoints to debug values at the moment. But they are really buggy too. – gallileo Jun 04 '14 at 12:45
  • I have a similar issue where the debugger never latches on and always says "waiting to attach" in Xcode. Unfortunately I see nothing of use from googling and posted a question here on Stack with no answers yet. – Alex Bollbach Jul 11 '16 at 14:55

16 Answers16

101
  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.


Updates on Aug 23, 2014:

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

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.

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

pkamb
  • 33,281
  • 23
  • 160
  • 191
Vince Yuan
  • 10,533
  • 3
  • 32
  • 27
  • 9
    So far the easiest way to debug (and see NSLog) I found is following this steps: /// 1 /// Select a container app schema and run it. /// 2 /// Once its up and running - switch back to XCode and (without even pressing a Stop button) select an extension schema, then press Run button. /// 3 /// When prompted to choose app to run, select Today. /// 4 /// Now breakpoints and NSLog should work without problems. – interrupt Nov 13 '14 at 06:10
  • 1
    This is not working even today, especially after a crash. This is very annoying, i do not understand why is so hard to fix. – Cristi Băluță Dec 17 '14 at 08:32
  • 3
    I tried to use the updated instructions above and it worked for me. However, in step 2, instead of using my extension name, I used "By Process Identifier (PID) or Name..." and entered the pid of the process. This magically allowed me to use my break points. – tony.tc.leung Jan 08 '15 at 23:11
  • Debug > Attach To Process by PID or name: in the text field enter the process name in the debug navigator, something like com.company.AppName.AppName-ExtensionName – TigerCoding Mar 11 '16 at 21:34
  • This answer isn't relevant to the question. The OP is asking why NSLog messages aren't written to the console in an extension (a legitimate concern as I came to this page with the same question). He mentions nothing about issues connecting to the debugger. – Joey Carson Jul 02 '16 at 16:01
  • Do you have any idea, how to do this with Xamarin studio – Johnykutty Nov 25 '16 at 06:20
  • hi from 2023. this helped me a lot, thank you! appreciate your advice – eugene_prg May 02 '23 at 08:44
49

It works for me if you go in your Simulator under the menu Debug -> Open System Log...

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

enter image description here

General Grievance
  • 4,555
  • 31
  • 31
  • 45
BalestraPatrick
  • 9,944
  • 4
  • 30
  • 43
  • it will open **"~/Library/Logs/CoreSimulator//system.log"** so you can easily find there all of your other simulator devices logs too just replace **** with the simulator device ID – Hofi Mar 30 '16 at 02:18
33

NSLog is working perfectly.

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 nearly completely independent from their containing app. They have separate bundle identifiers, for example, and they also are separate processes on the OS.

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.

Sometimes, I am able to run my extension target in 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 sometimes attach 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,

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 -> Devices in 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.

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.

Update: In the iOS 8 Beta 4 release notes:

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.
Community
  • 1
  • 1
Andrew
  • 15,357
  • 6
  • 66
  • 101
  • did you try the same approach for Storage Extensions ? – SRP-Achiever Jun 24 '14 at 21:54
  • It didn't worked for me in Xcode 6 beta 2, but [Vince Yuan answer](http://stackoverflow.com/a/24288906/514181) did. Unfortunately, in Xcode 6 beta 3 extension debugging don't work for me at all. I didn't found solution to that problem, probably we have to wait for next release. – Darrarski Jul 13 '14 at 11:16
  • @Darrarski My answer is left over from Beta 1. FYI, Beta 3 is out so you should be on that by now. – Andrew Jul 13 '14 at 12:17
  • @SantaClaus pretty clear. My comment was to inform that none of given solutions works in Xcode 6 beta 3 for me. It looks like extension debugging is even more broken in this release. Let's hope next beta will fix the problem. – Darrarski Jul 13 '14 at 15:49
  • I have tried several of these answers in beta 5 and it still says 'Waiting to attach'. What else can I try? How can I see NSLog messages? Please help, thank you! – barfoon Aug 10 '14 at 02:23
  • 3
    None of those bugs looked fixed to me in beta 4 (now beta 5). – matt Aug 10 '14 at 16:39
18

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:

Debug->Attach to process->Your extension name (mine was "com.example.MyExtensionApp.MyExtension")
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Masalis
  • 463
  • 1
  • 3
  • 10
16
  1. Create scheme for your extension
  2. Run scheme
  3. In a dialog choose container app
  4. Enjoy

It works for me :)

Maciek Czarnik
  • 5,950
  • 2
  • 37
  • 50
  • 1
    `NSLog` will only work if you're running the container app. It *will not* work if you're running the app-extension instead of the container. – JaredH Apr 11 '16 at 16:59
  • This worked for me as well. Thank you, Maciek Czarnik! The above solutions didn't worked for me. I'm using Xcode 8.1. – plam4u Dec 23 '16 at 08:56
5

Xcode 8 is able to debug extensions:

  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.

Result: breakpoints and log work as usual.

Jano
  • 62,815
  • 21
  • 164
  • 192
  • 2
    Not working in my case of a Today extension. getting CLANG error: ld: library not found for -lRPush clang: error: linker command failed with exit code 1 (use -v to see invocation) – BadmintonCat Sep 23 '16 at 07:05
  • This is an unrelated error. You are trying to link to a missing library. Add Liferay-Push to your target. – Jano Sep 23 '16 at 10:36
5

What finally allowed me to see the log in the debugging area, based on Michael's suggestion and 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.

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.

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.

Community
  • 1
  • 1
Andrew
  • 8,363
  • 8
  • 43
  • 71
  • This should be the accepted Answer. Doing it with this workflow enables Breakpoints and Logging! Thank you. – flymg Dec 29 '20 at 09:52
1

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.

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

Michael
  • 6,561
  • 5
  • 38
  • 55
1

A trick which works for me (although it is quite an ugly one) is to place a dummy UILabel somewhere 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.

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.

Andriy Gordiychuk
  • 6,163
  • 1
  • 24
  • 59
1

The only way debugging works for me is by selecting Debug->Attach To Process By PID or Name Then enter the PID not the extension name. You can find the PID by running the extension on a device, go to Window->Devices. Find your device and viewing the console. When you see the name of your extension, its followed by 5 digit number. That is the PID

I also put a bunch of NSLog's in the extension in order to find the PID as well. This is on xCode 7

Tony
  • 656
  • 8
  • 20
1

I could debug my extension with the way I describe below:

  1. Xcode : Debug -> Attach to process by PID or Name. Your extension scheme name.
  2. Then select your main app target and run.

I hope it also works for you guys.

abdullahselek
  • 7,893
  • 3
  • 50
  • 40
1

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

Device log can be found as following image. It is at 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 Notification key word, or process name must equal to the name of extension target, ex: equal to MyNotificationServiceExtension process name.

Device log

AechoLiu
  • 17,522
  • 9
  • 100
  • 118
0

As of Xcode 6 Beta 5, I've been able to use an actual device running iOS8 to debug my extension. Try running it on a device and select Safari to launch into

idmean
  • 14,540
  • 9
  • 54
  • 83
Rich86man
  • 6,507
  • 2
  • 26
  • 27
0

Clearly something is broken in Xcode6-B5.

If I try to run a Photo extension on the Simulator I cannot see any Photos.app as an option for the extension attaching process.

debug options within simulator

The same, running on a real device, give me the correct behaviour.

debug options withing a real device

In the first case any breakpoint is not honorated. In the latter case, breakpoints work like a charm.

valvoline
  • 7,737
  • 3
  • 47
  • 52
0

You should know that the container app and extension are totally two difference process in iOS while the LLVM debugs only a thread a time, so when you are debugging, the console never log the extension and never stop at breakpoint.

You can solve most of the problems by @Vince Yuan's method.

However, my problem is that Xcode debugger hardly hook on my keyboard extension on both iOS Simulator and devices, like 1 time in 7-8 runs, It's totally a matter of probability. @Vince Yuan's method also work for just sometimes.

My little experience is that when you run you debug scheme, if the debug Session in the left panel showing that 'No Debug Session', there is no need to open your extension and test it, the debugger didn't hook on, just run again for lucky.

but when you see com.xxx.xxx.xxx is waiting to Attach, the extension can definitely get debugged.

This is a little trick for whom cannot debug iOS extension, especially keyboard extension.

Henry
  • 481
  • 4
  • 17
0

To overcome all the states caused by ever changing IDE, I'm using iOS Console by lemonjar.com – it displays a console window for any connected iOS device rendering syslog messages regardless the process ID. You can see both app and extension debug log messages at once here.

igraczech
  • 2,408
  • 3
  • 25
  • 30