【发布时间】:2015-04-02 05:03:32
【问题描述】:
我是 iOS Swift 的初学者,正在编写 iOS Swift 代码并使用 UIWebView 加载我的网页。
我的网页会要求用户启用用户位置。
我想在 iOS Swift 代码中做类似的行为(弹出一个对话框并说“TestApp 想访问你的位置。你同意吗?”)
我在模拟器上运行,但在使用 CLLocationManager 时失败了
以下是我的 Swift 代码
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.
}
}
有人知道如何请求位置吗?
提前致谢。
埃里克
【问题讨论】:
-
检查 NSHipster 以获得好的摘要(适用于 iOS 8,但仍然有效)。他们的文章讨论了在 info.plist 中设置密钥的要求:NSHipster - Core Location in iOS 8 此外,用户提示包含来自应用程序 Info.plist 文件中 NSLocationWhenInUseUsageDescription 密钥的文本,并且该密钥的存在是必需的调用此方法时。来自CLLocationManager Class Reference
标签: ios swift uiwebview core-location