Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

swift - requestWhenInUseAuthorization() not Work in iOS 8 With NSLocationWhenInUseUsageDescription Key in Info.plist

I am using iOS SDK 8.1 trying to call requestWhenInUseAuthorization() method to prompt user to grant access to my app. I imported CoreLocation.framework, and added NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription keys into info.plist. When I ran the app, it never prompted me for location access. Below is my code, what have I missed?

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        var manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest

        let authorizationStatus = CLLocationManager.authorizationStatus()
        switch authorizationStatus {
        case .Authorized:
            println("authorized")
        case .AuthorizedWhenInUse:
            println("authorized when in use")
        case .Denied:
            println("denied")
        case .NotDetermined:
            println("not determined")
        case .Restricted:
            println("restricted")
        }

        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        let location = locations[0] as CLLocation
        println("Latitude: (location.coordinate.latitude). Longitude: (location.coordinate.longitude).")
    }

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        switch status {
        case .Authorized:
            println("authorized")
        case .AuthorizedWhenInUse:
            println("authorized when in use")
        case .Denied:
            println("denied")
        case .NotDetermined:
            println("not determined")
        case .Restricted:
            println("restricted")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

The console only showed "not determined" once and nothing else. So I went to iPhone Simulator => Settings => Privacy => Location => My App. It showed me 3 options: "Never", "While Using the App", "Always". But nothing was selected.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Problem solved. My manager was declared as local var inside viewDidLoad() method, but it should've been a class level property.

After I moved manager declaration out of viewDidLoad(), my app worked.

Not sure how exactly manager.requestWhenInUseAuthorization() work behind the scene and why exactly manager defined within viewDidLoad() not work. Hope someone who knows this detail enlighten me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...