2015-09-17 16 views
6

Tôi đã tạo một hàm giả sử để xử lý quyền với vị trí, để ứng dụng sẽ đóng nếu ứng dụng không có quyền truy cập vị trí. Tuy nhiên, khi bạn bấm thiết lập mở và bấm "quay lại ứng dụng" trên thanh trạng thái, phương thức determeinePermission sẽ không được thực hiện lại. Tôi đã cố thêm nó vào viewDidLoad, viewDidAppearviewWillAppear. tôi có thể làm gì?Phát hiện khi "quay lại ứng dụng" được nhấn

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

Thử thêm nó vào appDidBecameActive. P.S. bạn không thể đóng ứng dụng theo chương trình mà bạn muốn làm. Không cho phép thoát (0) trong mã của bạn. – Gruntcakes

+0

cũng ổn, tôi sẽ tìm thấy một giải pháp cho điều đó :) nhưng vấn đề đang gọi một phương pháp khi nhấn trở lại –

+0

Bạn đã thử với appDidBecomeActive chưa? – Gruntcakes

Trả lời

5

Thử thêm số này vào UIApplicationDelegate.applicationDidBecomeActive.

+1

nó là applicationDidBecomeActive không 'thêm' – Max

+1

UIApplicationDidDidBecomeActive cũng sẽ kích hoạt sau khi gián đoạn tạm thời như cuộc gọi điện thoại hoặc tin nhắn SMS, vì vậy tôi khuyên bạn nên UIApplicationDidEnterForeground thay thế. – Vadoff

Các vấn đề liên quan