簡體   English   中英

CLLocationManager授權消息

[英]CLLocationManager authorization message

您好,我在我的應用程序中使用CLLocation,我已經初始化了CLLocationManager,如下所示:

func initLocationManager(){    
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    let authstate = CLLocationManager.authorizationStatus()
    if(authstate == CLAuthorizationStatus.NotDetermined || authstate == CLAuthorizationStatus.Denied){
        println("Not Authorised")
        locationManager.requestWhenInUseAuthorization()
    }
    locationManager.startUpdatingLocation() 
}

我還向我的plist添加了NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription鍵。

第一次打開我的應用程序時,我收到提示消息,提示我的應用程序要訪問位置,它有2個按鈕允許和不允許。 如果我單擊不允許按鈕並關閉該應用程序,則當我再次打開它時,我不會再收到提示消息。

每當用戶打開應用程序時,如何使該提示消息出現? 謝謝

  1. 每次都提示警報不是有效的方法。
  2. 另一種選擇是,只有在禁用 位置服務或最初“ 不允許 ”的情況下,您才可以顯示警報。
  3. 首先遵循代碼提示警報,並且在禁用 位置服務時遵循自定義警報

     import UIKit import CoreLocation class ViewController: UIViewController,CLLocationManagerDelegate { var locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() initLocationManager() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func initLocationManager(){ let status = CLLocationManager.authorizationStatus() if(status == CLAuthorizationStatus.NotDetermined) { locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters let iosVersion = NSString(string: UIDevice.currentDevice().systemVersion).doubleValue if iosVersion >= 8.0 { //For Foreground locationManager.requestWhenInUseAuthorization() } locationManager.startUpdatingLocation() } else { if(status != CLAuthorizationStatus.AuthorizedWhenInUse) { var alert = UIAlertView(title: "Location", message: "Please turn on Location Services", delegate: nil, cancelButtonTitle: "Cancel") alert.addButtonWithTitle("Open Setting") alert.show() /*Add Action on Open Setting alertbutton to directly open settings in iOS 8 and later -> UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)*/ } } } } 

一旦用戶拒絕 ,便無法執行此操作,您必須顯示一個對話框,向用戶解釋他/她必須轉到“設置”並手動啟用該功能。

該消息要求獲得使用設備GPS的許可,並嘗試獲取設備的位置,這就是為什么非常需要GPS的原因。 第二,您不想顯示它,您可以使用NSUseDefaults設置條件並存儲密鑰,然后不要調用locationmanager startupdatinglocation方法。 這是不再次顯示它的唯一方法,否則它將每次顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM