我有一个带有 Today Extension 的迷你应用程序,我创建了一个自定义 URL Scheme。 然后在扩展中单击一个 UIButton 之后,我调用了 open 函数,如
extensionContext?.open(URL(string: "todayextensionexample://inform")!, completionHandler: nil)
应用程序已成功启动,但我无法调试它。
我尝试了 3 种不同的方法。
首先用于打开调试 Edit Scheme -> Info -> Wait for executable to be launched
在 Application scheme 中启动可执行文件,但应用程序等待并且没有启动,因此没有调试。
我的(打开网址:网址)功能如下:
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
}
在ViewController的viewDidLoad方法中;
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) 当应用程序打开并处于 sleep 状态时,通过 Xcode 应用程序菜单 ( Debug
-> Attach To Process
-> < your app name >
) 将 Xcode 调试器连接到您的应用程序。这将立即唤醒您的应用程序,它会立即进入断点。
关于ios - 使用 openUrl 从 TodayExtension 打开时如何调试 iOS App?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59602267/