2016-07-09 1 views
0

위치가 켜져 있는지 확인하고 위치가 맞지 않거나 사용자가 위치 사용 권한을 부여하지 않은 경우 앱이 종료됩니다 (실행되지 않음). 사람들이 exit (0)을 사용하지 않는 것이 좋지 않다고 말하면서 이것을 수행 할 수있는 방법이 있으며 Apple을 슬프게 만들 것입니다. : Dswift 2 위치가 꺼져있는 경우 프로그램 실행 차단

+1

[iPhone 응용 프로그램을 종료하는 올바른 방법] (http://stackoverflow.com/q/355168/335858) – dasblinkenlight

답변

2

사용자는 위치 서비스가 활성화 된 상태에서만 앱을 사용할 수 있음을 알리는 레이블을 사용하여 전체 너비와 높이를 화면에 표시 할 수 있습니다. 물론 사용자는 어떤 식 으로든이 뷰와 상호 작용할 수 없어야합니다.

1

애플이 이유 exit(0) 좋아하지 않는다 ...

import UIKit 
import CoreLocation 

class LocationHelper: NSObject, CLLocationManagerDelegate { 
    private static let sharedInstance = LocationHelper() 
    private var locationManager: CLLocationManager! { 
     didSet { 
      locationManager.delegate = self 
     } 
    } 

    private override init() {} 

    class func setup() { 
     sharedInstance.locationManager = CLLocationManager() 
    } 

    private func informUserToEnableLocationServices() { 
     let infoPopup = UIAlertController(title: "Location Services", message: "Sorry, but you have to enable location services to use the app...", preferredStyle: .Alert) 
     let tryAgainAction = UIAlertAction(title: "Try again", style: .Default) { (action) in 
      if CLLocationManager.authorizationStatus() != .AuthorizedWhenInUse { 
       self.informUserToEnableLocationServices() 
      } 
     } 
     infoPopup.addAction(tryAgainAction) 
     let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate 
     let rootViewController = appDelegate?.window?.rootViewController 
     rootViewController?.presentViewController(infoPopup, animated: true, completion: nil) 
    } 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     switch status { 
     case .NotDetermined: 
      locationManager.requestWhenInUseAuthorization() 
     case .AuthorizedWhenInUse: 
      break 
     default: 
      informUserToEnableLocationServices() 
     } 
    } 
} 

단순히 시작 응용 프로그램 후 LocationHelper.setup()를 호출하고 클래스가 나머지를 처리해야 : 여기

는 예 도우미 클래스입니다. 앱을 직접 해지하지 않는 것이 좋습니다. 어쩌면 사용자가 제한된 기능으로 앱을 사용할 수있게 할 수 있습니까? 또 다른 옵션은 아무런 행동도하지 않거나 아무 것도하지 않는 행동으로 경고를하는 것입니다.

관련 문제