检测“返回应用程序”按钮被按下的情况

7

我创建了一个函数,它应该处理位置权限,以便如果应用程序没有位置权限,则关闭应用程序。但是当您按下“打开设置”并在状态栏中按下“返回应用程序”时,determeinePermission方法不会再次执行。我尝试将其添加到viewDidLoadviewDidAppearviewWillAppear中。我该怎么办?

func determinePermission() {
    switch CLLocationManager.authorizationStatus() {

    case .Authorized:
        if CLLocationManager.locationServicesEnabled() {
            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            manager.startUpdatingLocation()
        }


    case .NotDetermined:
        manager.requestWhenInUseAuthorization()
    case .AuthorizedWhenInUse, .Restricted, .Denied:
        let alertController = UIAlertController(
            title: "Background Location Access Disabled",
            message: "In order to be notified about adorable kittens near you, please open this app's settings and set location access to 'Always'.",
            preferredStyle: .Alert)

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
            exit(0)
        }
        alertController.addAction(cancelAction)

        let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
            if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
                UIApplication.sharedApplication().openURL(url)
            }
        }
        alertController.addAction(openAction)

        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

尝试将其添加到appDidBecameActive中。顺便说一句,您无法以编程方式关闭应用程序,这就是您想要做的事情。在您的代码中放置exit(0)是不允许的。 - Gruntcakes
好的,我会为此找到解决方案 :) 但问题是在按返回键时调用一个方法。 - Peter Pik
你尝试过使用appDidBecomeActive吗? - Gruntcakes
它起作用了,请给出一个答案。 - Peter Pik
1个回答

6

尝试将其添加到 UIApplicationDelegate.applicationDidBecomeActive


1
它是applicationDidBecomeActive而不是'add'。 - Max
1
UIApplicationDidBecomeActive 在像电话或短信之类的临时中断后也会触发,因此我建议使用 UIApplicationDidEnterForeground。 - Vadoff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接