检测何时“返回应用”被压了 [英] Detect when "back to app" is pressed

查看:90
本文介绍了检测何时“返回应用”被压了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个函数,假设它可以处理带位置的权限,因此如果应用程序没有位置权限,它将关闭。但是,当您按下打开设置并在状态栏中按返回应用程序时, determeinePermission 方法不会再次执行。我试图将它添加到 viewDidLoad viewDidAppear viewWillAppear 。我该怎么办?

I've created a function which is suppose to handle the permission with location, so that the app will close if it does not have permission to location. However when you press open settings and press "back to app" in the status bar the determeinePermission method is not being executed again. I've tried to add it to viewDidLoad, viewDidAppear and viewWillAppear. what can I do?

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)
    }
}


推荐答案

尝试将其添加到 UIApplicationDelegate.applicationDidBecomeActive

这篇关于检测何时“返回应用”被压了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆