【问题标题】:Detect when "back to app" is pressed检测何时按下“返回应用程序”
【发布时间】:2015-12-14 16:46:38
【问题描述】:

我创建了一个函数来处理位置权限,这样如果应用程序没有位置权限,它就会关闭。但是,当您按打开设置并在状态栏中按“返回应用程序”时,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) 放入您的代码中。
  • 好吧,我会找到解决方案的 :) 但问题是在按下返回时调用方法
  • 你用 appDidBecomeActive 试过了吗?
  • 成功了,请回答

标签: ios swift permissions core-location


【解决方案1】:

尝试将其添加到UIApplicationDelegate.applicationDidBecomeActive

【讨论】:

  • 它是 applicationDidBecomeActive 而不是 'add'
  • UIApplicationDidDidBecomeActive 也会在电话或短信等临时中断后触发,所以我建议改为 UIApplicationDidEnterForeground。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多