How to debug iOS 14 widget in Xcode 12?

Viewed 6633

I am working on an iOS 14 widget which I would like to my existing iOS 11+ app. The whole process is quite cumbersome because it happens quite often that something does not work as expected.

For example the widget shows unexpected data, or is not rendered as expected (e.g. as described here). I assume that something wents wrong when the system requests the widget and its content from my app extension. However I cannot figure out what this might be.

Is there any way to actually debug the widget extension? Seeing when the code is executed and how it works?

I have already tried to hook up the debugger to the widget extension (using the Debug/Attach to process menu in Xcode). While the process is listed there, the debugger shows no log output nor stops on breakpoints.

Using the system console to show the logs of the iOS simulator devices does not work as well. It does not matter if print(...) or NSLog(...) is used. No output reaches the console.

6 Answers

On the top left of Xcode->

  1. Click project name, you can see a list, select widget name, run it
  2. Click widget name, you can see a list, select project name, run it

Xcode: Version 12.3, iPad: iPadOS 14.3
This works for me.

Since I found no other way to debug the widget extension I wrote a logging method which adds the log output to a a file a the apps group folder:

static func logToFile(_ text: String) {        
    if let documentsDirectory = FileManager().containerURL(forSecurityApplicationGroupIdentifier: "my.app.group.key") {
        let logFileUrl = documentsDirectory.appendingPathComponent("log.txt")
        
        do {
            var logContent = try String(contentsOf: logFileUrl, encoding: .utf8)
            logContent = (logContent.count > 0 ? "\(text)\n\(logContent)" : text)
            try logContent.write(to: logFileUrl, atomically: false, encoding: .utf8)
        }
        catch {}
    }
}

Not the best solution but the only one I found so far.

Seems Xcode bug here. Quick fix works for me is open log console manually by cmd + shift + y. And also add breakpoints. And then run widget to see logs.

Try disabling bitcode in your extensions, I've had a hard time getting logs until I do it.

I've run into this issue as well. Logs were not showing (or only showing sometimes) when I run my widget extension in Xcode.

After hours of debugging, the thing that fixed it for me was plugging in my actual device (iPhone 12 Pro Max) and running the widget on that physical device instead of the simulator.

Related