2016-10-17 75 views
0

我剛開始使用iOS上的SWIFT編程。不過,我在諸如JAVA,C#和PHP等其他語言方面擁有豐富的經驗。也就是說,我遇到了一些問題。我正在創建一個簡單的應用程序,根據它們的地理位置顯示用戶信息。我正在按照列出的教程hereAlert Box要求用戶授予位置權限不顯示

但是,由於很多事情在Swift 2.x和3.x之間發生了變化,我不得不修改他的代碼。不太難,到目前爲止,我已經得到它編譯。不過,應用程序從來沒有要求我允許我使用設備的位置。

我遵循蘋果列出的here關於在Info.plist中放置「NSLocationAlwaysUsageDescription」的說明。不過,我從來沒有提示。下面列出了應顯示此警報的相關代碼。

任何幫助將不勝感激。謝謝。

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    // 1. status is not determined 
    if CLLocationManager.authorizationStatus() == .notDetermined { 
     locationManager.requestAlwaysAuthorization() 
    } 
     // 2. authorization were denied 
    else if CLLocationManager.authorizationStatus() == .denied { 
     let controller = UIAlertController(title: "Error!", message: "Location services were previously denied. Please enable location services for this app in Settings.", preferredStyle: .alert) 

     let alertAction = UIAlertAction(title: "Dismiss", style: .destructive) { (action) in 
      print("Dismiss button tapped!") 
     } 

     controller.addAction(alertAction) 

    } 
     // 3. we do have authorization 
    else if CLLocationManager.authorizationStatus() == .authorizedAlways { 
     locationManager.startUpdatingLocation() 
    } 
} 
+1

Yuck。你知道'switch'嗎? – matt

+0

@matt這不是生產代碼。我只是想讓mapView工作。我是iOS編程的新手,請原諒這個問題。 –

回答

1

您的代碼工作正常,你只需要出示您的警告:

self.present(controller, animated: true, completion: nil) 

而且也看看我的回答here你如何使用switch此類型的代碼。