Tôi là người mới bắt đầu trong iOS Swift và viết mã iOS Swift và sử dụng UIWebView để tải trang web của tôi.iOS Swift: Yêu cầu vị trí người dùng
Và trang web của tôi sẽ yêu cầu người dùng bật vị trí người dùng.
Tôi muốn làm những hành vi tương tự trong mã Swift iOS (bật lên một hộp thoại và nói điều gì đó như "TestApp muốn truy cập vị trí của bạn. Bạn có đồng ý không?")
Tôi đang chạy trên Simulator và tôi đã thất bại khi sử dụng CLLocationManager
sau đây là mã Swift tôi
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var customWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let url = NSURL(string: "http://ctrlq.org/maps/where/") {
let request = NSURLRequest(URL: url)
customWebView.loadRequest(request)
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Bất cứ ai cũng biết làm thế nào để yêu cầu vị trí?
Xin cảm ơn trước.
Eric
Kiểm tra NSHipster ra cho một bản tóm tắt tốt (dành cho iOS 8, nhưng vẫn còn hiệu lực). Bài viết của họ nói về yêu cầu thiết lập khóa trong info.plist: [NSHipster - Vị trí cốt lõi trong i OS 8] (http://nshipster.com/core-location-in-ios-8/) Ngoài ra, _The lời nhắc người dùng chứa văn bản từ khóa NSLocationWhenInUseUsageDescription trong tệp Info.plist của ứng dụng và sự hiện diện của khóa đó là bắt buộc khi gọi phương thức này._ từ [Tham chiếu lớp CLLocationManager] (https://developer.apple.com/library/ ios/documentation/CoreLocation/Reference/CLLocationManager_Class /) – leanne