skip to Main Content

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?

4

Answers


  1. 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 {}
        }
    }
    
    Login or Signup to reply.
  2. Try plugging in your physical device and running it on that instead of a simulator. That fixed the logging issue for me.

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
  4. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search