Eri*_*eng 8 uiwebview core-location ios swift
我是iOS Swift的初学者,编写iOS Swift代码并使用UIWebView加载我的网页.
我的网页将要求用户启用用户位置.
我想在iOS Swift代码中执行类似的操作(弹出一个对话框并说"TestApp想访问你的位置.你同意吗?")
我在Simulator上运行,但在使用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.
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何申请位置?
提前致谢.
埃里克
请求和更新用户位置的正确方法是通过CLLocationManagerDelegate.
在您的View Controller继承时CLLocationManagerDelegate,它似乎没有实现必要的委托功能.正如@Rob所提到的,您应该将locationManager声明为类属性,而不是局部变量.
如果/当用户更改授权状态时,此委托功能允许您实现逻辑:
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
// authorized location status when app is in use; update current location
locationManager.startUpdatingLocation()
// implement additional logic if needed...
}
// implement logic for other status values if needed...
}
Run Code Online (Sandbox Code Playgroud)
如果/当用户位置发生变化时,此委托功能允许您实现逻辑:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
// implement logic upon location change and stop updating location until it is subsequently updated
locationManager.stopUpdatingLocation()
}
}
Run Code Online (Sandbox Code Playgroud)
此外,locationServicesEnabled()确定用户是否启用了位置服务.
你在didFinishLaunchingWithOptions中显示调用做这样的事情
let status = CLLocationManager.authorizationStatus()
if status == .NotDetermined || status == .Denied || status == .AuthorizedWhenInUse {
// present an alert indicating location authorization required
// and offer to take the user to Settings for the app via
// UIApplication -openUrl: and UIApplicationOpenSettingsURLString
dispatch_async(dispatch_get_main_queue(), {
let alert = UIAlertController(title: "Error!", message: "GPS access is restricted. In order to use tracking, please enable GPS in the Settigs app under Privacy, Location Services.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Go to Settings now", style: UIAlertActionStyle.Default, handler: { (alert: UIAlertAction!) in
print("")
UIApplication.sharedApplication().openURL(NSURL(string:UIApplicationOpenSettingsURLString)!)
}))
// self.presentViewController(alert, animated: true, completion: nil)
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
})
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
}
Run Code Online (Sandbox Code Playgroud)
几点想法:
您已将自己定义CLLocationManager为局部变量,当它超出范围时将被释放.
使这成为一个类属性.
如果您确实需要requestAlwaysAuthorization,请不要忘记NSLocationAlwaysUsageDescription按照文档中的说明设置plist.
(如果您不需要总是授权,但可以使用"使用中"授权,呼叫requestWhenInUseAuthorization和设置NSLocationWhenInUseUsageDescription值.)
| 归档时间: |
|
| 查看次数: |
14264 次 |
| 最近记录: |