Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug iOS 14 widget in Xcode 12?

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.

like image 275
Andrei Herford Avatar asked Oct 05 '20 09:10

Andrei Herford


4 Answers

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.

like image 178
Parth Patoliya Avatar answered Sep 23 '22 23:09

Parth Patoliya


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.

like image 31
Andrei Herford Avatar answered Sep 23 '22 23:09

Andrei Herford


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.

like image 26
orli Avatar answered Sep 22 '22 23:09

orli


Select your widget target at the top left corner of Xcode, build and run the app, then the widget process will be able to debug and show logs. (This example project is downloaded provided by Apple: https://developer.apple.com/documentation/widgetkit/building_widgets_using_widgetkit_and_swiftui)

enter image description here

like image 42
Johnny Avatar answered Sep 25 '22 23:09

Johnny