16

I'm developing WidgetKit extension on iOS 14, however, the extension doesn't always connect to Xcode debugger after build and run, causing I can't see logs, as this image shows: (But sometimes it'll automatically attach, I don't know why)

enter image description here

If the extension is attached to debugger, it should look like this, and print logs:

enter image description here

Manually attach process to debugger doesn't works, it should be attached at first launch to see logs.

Does anyone know how to properly debug iOS 14 widget?

Johnny
  • 2,589
  • 2
  • 27
  • 34
  • 1
    Same issue here. Did you ever solve it? It's frustrating because some days it sees to work fine and other days it does not. Some days I literally spend hours trying to see debug output. – Jackson Feb 06 '21 at 13:37
  • Same issue, if I delete the widget from the real device, and run widget target it's GOOD, but second time I run the widget is frozen on attaching message – zdravko zdravkin Aug 18 '22 at 10:01

4 Answers4

19

Xcode 14.1 known issue:

Xcode 14.1 release notes seems to mention a known issue

After Running a widget extension, users will need to manually start the debugging session with Debug -> Attach to Process. (99285608)

Approach (works on simulator and device)

  • Run app target and widget target at the same time
  • Attach debugger to your widget

Steps

  1. Select app scheme and run on iOS device (don't stop)
  2. Select widget scheme and run on iOS (don't stop)
    • So both the targets are running at the same time
  3. Select widget scheme then Debug > Attach to Process > Select your widget target name
  4. On device / simulator add widget

In case above doesn't work:

  1. Delete app from device
  2. Restart device
  3. Quit Xcode
  4. Clear DerivedData folder
  5. Open Xcode
  6. Try original steps

Note:

  • You need to attach the debugger every time you run (Xcode forgets debugger added for the previous run)

Now your breakpoints should work as expected and you can debug

Log messages

Use Logger to log messages, open the console (Mac app) and view the log messages there. That way you can debug even when you are not running the app / widget

user1046037
  • 16,755
  • 12
  • 92
  • 138
  • 2
    Thank you! I looked in so many places, and this was the only thing that helped! – Lucas Nov 11 '22 at 23:36
  • This not works for me. Xcode 14.1, Xcode 13.4, 13.1 are also the same issue. I also tried a lot of ways from many suggestions. But nothing works. – Binh Ho Nov 24 '22 at 13:33
  • 1
    @BinhHo Are you able to attach the debugger? Ensure you are attaching the debugger of the Widget (not the app). If nothing works, use a Logger and check the console – user1046037 Nov 24 '22 at 16:05
  • @user1046037 On real device, no ways to attaching to debugger. Of course I know how to attach it. But on "My Mac (designed for Iphone)" works fine. This mean something wrong on real device and XCode. Or maybe my device is iOS 16.1? – Binh Ho Nov 25 '22 at 04:36
  • @BinhHo I experienced your issue, strangely when I lock the phone the breakpoint is executed, not ideal but something you can try as a work around. Please raise a feedback with Apple – user1046037 Nov 25 '22 at 07:34
  • 1
    @BinhHo Delete app from device, restart device, clear `DerivedData`, Quit and re-open Xcode, try again and then it started working – user1046037 Nov 25 '22 at 07:41
  • @user1046037 let me try and get back later. – Binh Ho Nov 25 '22 at 08:12
  • These suggestion didn't work for me unfortunately, I ended up downloading Xcode 14.0 instead and debugging work in that version. – Paul Peelen Jan 14 '23 at 16:22
  • Whenever I try to run the widget scheme with the iOS device running the main app, the main app crashes with "Message from debugger: Terminated due to signal 9". Thoughts? – Bryce Sandlund Jan 21 '23 at 21:04
  • @BryceSandlund I get the feeling you are running the widget before running the app. App needs to be running first, then run the widget. Otherwise try a new simulator. – user1046037 Jan 22 '23 at 05:45
  • Thank you very much. There's the only answer to this problem that I've found that works. Print statements still not working, tough. – Dan Flict Jan 24 '23 at 00:23
  • 1
    @DanFlict Best to use Logger (Apple's framework for logging) and then debug via console app (separate Mac app), that seems to be the only way. Also Logger is the recommended approach for logging, refer to doc links in the answer – user1046037 Jan 24 '23 at 05:41
2

Try plugging in your physical device and running it on that instead of a simulator. That fixed the logging issue for me.

Andy
  • 829
  • 6
  • 11
2

To debug, or see print info: (This works for me)

On the top left of Xcode->
Click project name, you can see a list, select widget name, run it
Click widget name, you can see a list, select project name, run it
Xcode: Version 12.3, iPad: iPadOS 14.3

ho2103
  • 407
  • 5
  • 6
0

You can log your useful information in local file, just like this:

public func XXLogToFile(_ text: String) {
    if let documentsDirectory = FileManager().containerURL(forSecurityApplicationGroupIdentifier: "group.xxx") {
        let logFileUrl = documentsDirectory.appendingPathComponent("log.txt")
    
         do {
            var logContent = try String(contentsOf: logFileUrl, encoding: .utf8)
            logContent = (logContent.count > 0 ? "\(logContent)\n\(text)" : text)
            try logContent.write(to: logFileUrl, atomically: false, encoding: .utf8)
        }
        catch {}
    }
}
Finder丶Tiwk
  • 345
  • 2
  • 8