我正在尝试通过单击按钮从消息扩展中打开我的母亲应用程序。我在我的扩展中使用了这个代码:
@IBAction func open(_ sender: UIButton) {
let url = URL(string: "swiftexamples://")
self.extensionContext?.open(url!, completionHandler: {(succes) in })
}
当我的母亲应用程序正在运行并在后台运行时,一切正常,但是当我想打开已关闭的应用程序时,它崩溃了。没有崩溃日志,我有类似的情况 iOS app crashes when first opened by URL Scheme . 我很确定我必须向应用程序委托(delegate)添加一些内容。 我找到了函数应用程序(_penptions。问题是,我现在不知道如何实现“选项”部分。我写了这个:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? NSURL
let sourceApp = launchOptions?[UIApplicationLaunchOptionsKey.sourceApplication] as? String
let annotation = launchOptions?[UIApplicationLaunchOptionsKey.annotation] as? AnyObject
self.application(application, open: url, options:[sourceApp: String, annotation:AnyObject] )
return true
}
我还听说过通用链接,它们具有与 url 方案相似的能力。是否可以使用通用链接来实现我想要的?
您可以尝试此解决方案。它对我有用:
func openUrl(url: URL?) {
let selector = sel_registerName("openURL:")
var responder = self as UIResponder?
while let r = responder, !r.responds(to: selector) {
responder = r.next
}
_ = responder?.perform(selector, with: url)
}
func canOpenUrl(url: URL?) -> Bool {
let selector = sel_registerName("canOpenURL:")
var responder = self as UIResponder?
while let r = responder, !r.responds(to: selector) {
responder = r.next
}
return (responder!.perform(selector, with: url) != nil)
}
正如 https://stackoverflow.com/a/44694703/2064473 中的建议
关于ios - 在swift 3中从消息扩展打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42737059/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |