使用 openUrl 从 TodayExtension 打开时如何调试 iOS 应用程序?

How to debug iOS App when opened from TodayExtension with openUrl?

我有带有 Today Extension 的迷你应用程序,我创建了一个自定义 URL 方案。 然后在扩展中单击一个 UIButton 后,我将打开函数称为

extensionContext?.open(URL(string: "todayextensionexample://inform")!, completionHandler: nil)

应用程序启动成功,但无法调试。

我尝试了 3 种不同的方法。

首先用于打开调试Edit Scheme -> Info -> Wait for executable to be launched用于在应用程序方案中启动可执行文件但应用程序等待并且没有启动所以没有调试。

我的(打开url:URL)函数如下:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

    NotificationCenter.default.post(name: NSNotification.Name.urlOpenedNotification, object: nil, userInfo: nil)

    return true

}

在viewDidLoad方法中ViewController;

override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(notificationHandler(_:)), name: NSNotification.Name.urlOpenedNotification, object: nil)

    }

这是我的第二种方法。

3. 方法是使用解析 URL 参数并记录如下:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {


let sendingAppID = options[.sourceApplication];
print("source application = \(sendingAppID ?? "Unknown") ")

guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
    let path = components.path,
    let params = components.queryItems else {
        print("Invalid URL or album path missing")
        return false
}



print("components > ", components)
print("path > ", path)
print("params >", params)

var param: [String: Any] = ["components": components, "path": path, "params": params];

NotificationCenter.default.post(name: NSNotification.Name.urlOpenedNotification,
                                object: nil,
                                userInfo: param)


return true
}

在 3. 方法中,我尝试了通知并打印了一些变量。

在我提问之前;我将一些 print() 更改为 NSLog(),但在 Device -> Show Logs 中没有任何效果。

应用程序已使用 openUrl 打开,但我无法调试它,因为如果我启动 TodayExtension 目标应用程序不调试,如果我启动应用程序目标,当我单击 TodayExtension 按钮然后打开该应用程序在背景时再次出现

func application(_ app: UIApplication, open url: URL ...)

功能不工作。看过真机debug,没啥可看的

有什么方法可以在点击 Today Extension 上的按钮后 Xcode 启动主要应用程序目标并且我将能够看到所有日志?

我如何调试它并查看它是否成功运行?

1) 在您的 App Delegate 中重写初始化程序,并在其中添加 sleep 调用。

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    override init() {
        sleep(10000)
    }
    ...
}

2) 在您的设备或模拟器上安装此应用程序。

3) 在你的代码中放置一个断点。

4) 从扩展中触发深层链接。

5) 当应用程序打开并处于睡眠状态时,通过 Xcode 应用程序菜单将 Xcode 调试器连接到您的应用程序 (Debug->Attach To Process->< your app name >).这将立即唤醒您的应用程序,它会立即进入断点。