我使用完全相同的方法处理两个AppDelegate
实例方法-通用和URL方案链接。我需要提及的是,两个测试链接均已正确设置。我试图检查这种方法在iOS 12和13上的工作方式,并得到了相同的意外行为。
在我尝试单击通用链接的情况下,一切正常。在尝试对URL方案链接执行相同操作的情况下,我看到成功启动的方法application(_:open:options:)
没有被系统触发。
如果我在application(_:open:options:)
类中初始化了两个空方法application(_:continue:restorationHandler:)
和AppDelegate
,然后从下面的代码调用method_exchangeImplementations
,则我收到{{1} }和application(_:open:options:)
方法。在这两种情况下,当我尝试通过通用链接或URL方案链接进行单击时,都收到了预期的行为-系统触发了混乱的方法。
当我没有在application(_:continue:restorationHandler:)
类初始化空方法application(_:open:options:)
和application(_:continue:restorationHandler:)
时,如下面的代码所示,我调用方法AppDelegate
来添加实现飞向class_addMethod
实例。在这种情况下,我发现AppDelegate
和application(_:open:options:)
这两个方法都成功添加到了application(_:continue:restorationHandler:)
实例中,但是当我尝试通过URL方案链接进行点击时-系统没有触发我的麻烦方法AppDelegate
,反之亦然,方法application(_:open:options:)
-单击通用链接会触发我的麻烦方法。
application(_:continue:restorationHandler:)
为什么会发生这种情况,以及如何在通过URL方案链接单击后立即将方法// MARK: Swizzle AppDelegate universal links method.
private func swizzleContinueRestorationHandler() {
guard let swizzleMethod = class_getInstanceMethod(Swizzler.self, #selector(self.application(_:continue:restorationHandler:))) else { return }
let delegateClass: AnyClass! = object_getClass(UIApplication.shared.delegate)
let applicationSelector = #selector(UIApplicationDelegate.application(_:continue:restorationHandler:))
if let originalMethod = class_getInstanceMethod(delegateClass, applicationSelector) {
method_exchangeImplementations(originalMethod, swizzleMethod)
} else {
class_addMethod(delegateClass, applicationSelector, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod))
}
}
// MARK: Swizzle AppDelegate URL schemes method.
private func swizzleOpenOptions() {
guard let swizzleMethod = class_getInstanceMethod(Swizzler.self, #selector(self.application(_:open:options:))) else { return }
let delegateClass: AnyClass! = object_getClass(UIApplication.shared.delegate)
let applicationSelector = #selector(UIApplicationDelegate.application(_:open:options:))
if let originalMethod = class_getInstanceMethod(delegateClass, applicationSelector) {
method_exchangeImplementations(originalMethod, swizzleMethod)
} else {
class_addMethod(delegateClass, applicationSelector, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod))
}
}
// MARK: Swizzled AppDelegate universal links method.
@objc func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
...
}
// MARK: Swizzled AppDelegate URL schemes method.
@objc func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
...
}
添加到application(_:open:options:)
实例并获得预期的行为?