[英]Containing App crashes when opened from Today extension in swift 4
i have a swift app for which i have a today extension,it has a button which opens the containing app. 我有一个快速应用程序,我有一个今天的扩展程序,它有一个按钮可以打开包含的应用程序。
the button opens the app perfectly when the app is in the recent list but crashes when the app is moved from the recent list. 当应用程序位于最近列表中时,该按钮可以完美地打开该应用程序,但是当该应用程序从最近列表中移出时,该按钮会崩溃。
there's no crashlogs too 也没有崩溃日志
this is my code on app delegate : 这是我在应用程序委托上的代码:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
var mainViewController : MainViewController!
mainViewController = UIApplication.shared.keyWindow?.rootViewController?.childViewControllers[1].childViewControllers[0] as! MainViewController
if url.scheme == "open"
{
switch url.host
{
case "1"?:
mainViewController.isTaxi = true
break
case "2"?:
mainViewController.isPfp = true
break
case "3"?:
mainViewController.isDarbi = true
break
default:
break
}
}
return true
}
this is how i open in main VC : 这就是我在主VC中打开的方式:
var isTaxi : Bool? {
didSet{
if UserDefaults.getUser() != nil {
self.taxiRegViewController.show()
} else {
self.taxiNotRegViewController.show()
}
}
}
this is where i fire tap event in extension : 这是我在扩展中触发点击事件的地方:
@IBAction func bookTaxiTapped(_ sender: UIButton) {
if let url = URL(string: "open://\(sender.tag)")
{
self.extensionContext?.open(url, completionHandler: nil)
}
}
I solved my problem,by modifying the app delegate's method like this : 通过修改应用程序委托的方法,我解决了我的问题:
Actual Problem was : we had SWRevealViewController which has to be initiated before calling other view controller's 实际问题是:我们有SWRevealViewController,必须在调用其他视图控制器的SWRevealViewController之前将其启动
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
var mainViewController : MainViewController!
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyBoard.instantiateViewController(withIdentifier: "swRevealController") as! SWRevealViewController
mainViewController = storyBoard.instantiateViewController(withIdentifier: "mainView") as! MainViewController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
viewController.setFront(mainViewController, animated: true)
if url.scheme == "open"
{
switch url.host
{
case "1"?:
mainViewController.isTaxi = true
break
case "2"?:
mainViewController.isPfp = true
break
case "3"?:
mainViewController.isDarbi = true
break
default:
break
}
}
return true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.