2016-10-17 74 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此类型的代码。