CoreLocationでiPhoneの位置情報を取得するプログラムを実行したところ、
startUpdatingLocation()が実行されません。
###前提・実現したいこと
環境:Swift3,iOS8.3
iPhoneの位置情報(緯度経度)の取得
###発生している問題・エラーメッセージ
startUpdatingLocation()が実行されず、
locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])が呼び出されない。
位置情報取得に失敗した時に呼ばれるデリゲートも呼び出されない。
###該当のソースコード
逆引きSwiftのソースコードをそのまま実行
Swift
1import UIKit 2import CoreLocation 3 4class ViewController: UIViewController , CLLocationManagerDelegate{ 5 6 var myLocationManager: CLLocationManager! 7 8 // 緯度表示用のラベル. 9 var myLatitudeLabel: UILabel! 10 11 // 経度表示用のラベル. 12 var myLongitudeLabel: UILabel! 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 // ボタンの生成. 18 let buttonWidth: CGFloat = 100 19 let buttonHeight: CGFloat = 100 20 let posX: CGFloat = (self.view.bounds.width - buttonWidth)/2 21 let posY: CGFloat = (self.view.bounds.height - buttonWidth)/2 22 let myButton = UIButton(frame: CGRect(x: posX, y: posY, width: buttonWidth, height: buttonHeight)) 23 myButton.backgroundColor = UIColor.orange 24 myButton.layer.masksToBounds = true 25 myButton.layer.cornerRadius = 50.0 26 myButton.setTitle("Get", for: .normal) 27 myButton.addTarget(self, action: #selector(ViewController.onClickMyButton(sender:)), for: .touchDown) 28 29 // 緯度表示用のラベルを生成. 30 myLatitudeLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 30)) 31 myLatitudeLabel.layer.position = CGPoint(x: self.view.bounds.width/2, y:self.view.bounds.height/2+100) 32 33 // 軽度表示用のラベルを生成. 34 myLongitudeLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 30)) 35 myLongitudeLabel.layer.position = CGPoint(x: self.view.bounds.width/2, y:self.view.bounds.height/2+130) 36 37 38 // 現在地の取得. 39 myLocationManager = CLLocationManager() 40 41 myLocationManager.delegate = self 42 43 // セキュリティ認証のステータスを取得. 44 let status = CLLocationManager.authorizationStatus() 45 print("authorizationStatus:\(status.rawValue)"); 46 47 // まだ認証が得られていない場合は、認証ダイアログを表示 48 // (このAppの使用中のみ許可の設定) 説明を共通の項目を参照 49 if(status == .notDetermined) { 50 self.myLocationManager.requestWhenInUseAuthorization() 51 } 52 53 // 取得精度の設定. 54 myLocationManager.desiredAccuracy = kCLLocationAccuracyBest 55 // 取得頻度の設定. 56 myLocationManager.distanceFilter = 100 57 58 self.view.addSubview(myButton) 59 } 60 61 /* 62 認証に変化があった際に呼ばれる 63 */ 64 private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 65 66 print("didChangeAuthorizationStatus"); 67 68 // 認証のステータスをログで表示. 69 var statusStr: String = ""; 70 switch (status) { 71 case .notDetermined: 72 statusStr = "未認証の状態" 73 case .restricted: 74 statusStr = "制限された状態" 75 case .denied: 76 statusStr = "許可しない" 77 case .authorizedAlways: 78 statusStr = "常に使用を許可" 79 case .authorizedWhenInUse: 80 statusStr = "このAppの使用中のみ許可" 81 } 82 print(" CLAuthorizationStatus: \(statusStr)") 83 } 84 85 /* 86 ボタンイベントのセット. 87 */ 88 func onClickMyButton(sender: UIButton){ 89 // 現在位置の取得を開始. 90 myLocationManager.startUpdatingLocation() 91 } 92 93 /* 94 位置情報取得に成功したときに呼び出されるデリゲート. 95 */ 96 private func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 97 // 緯度・経度の表示. 98 myLatitudeLabel.text = "緯度:\(manager.location!.coordinate.latitude)" 99 myLatitudeLabel.textAlignment = .center 100 101 myLongitudeLabel.text = "経度:\(manager.location!.coordinate.longitude)" 102 myLongitudeLabel.textAlignment = .center 103 104 105 self.view.addSubview(myLatitudeLabel) 106 self.view.addSubview(myLongitudeLabel) 107 108 } 109 110 /* 111 位置情報取得に失敗した時に呼び出されるデリゲート. 112 */ 113 func locationManager(_ manager: CLLocationManager,didFailWithError error: Error){ 114 print("error") 115 } 116 117}
###試したこと
info.plist変更済み
位置情報の使用を許可するかどうかの確認画面は出る
CLLocationManager.authorizationStatus()の出力はauthorizedWhenInUseとなっています
authorizationStatusの出力を教えて下さい。
回答2件
あなたの回答
tips
プレビュー