从swift 4中的今日扩展程序打开时,包含应用程序崩溃

时间:2018-06-09 06:42:17

标签: ios uiviewcontroller swift4 today-extension

我有一个快速的应用程序,我有一个今天的扩展,它有一个按钮,打开包含应用程序。

当应用程序位于最近的列表中时,该按钮可以完美地打开应用程序,但当应用程序从最近的列表中移出时会崩溃。

也没有崩溃日志

这是我在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
}

这就是我在主VC中打开的方式:

var isTaxi : Bool? {
    didSet{
        if UserDefaults.getUser() != nil {
            self.taxiRegViewController.show()
        } else {
            self.taxiNotRegViewController.show()
        }
    }
}

这是我在扩展程序中点击tap事件的地方:

@IBAction func bookTaxiTapped(_ sender: UIButton) {
    if let url = URL(string: "open://\(sender.tag)")
    {
        self.extensionContext?.open(url, completionHandler: nil)
    }
}

1 个答案:

答案 0 :(得分:0)

我通过修改app delegate的方法解决了我的问题:

实际问题是:我们有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
}