我正在创建一个iOS框架,我想使用Core Location与Beacons进行交互。出于测试原因,我试图获得用户位置。
这是我在框架中创建的类。
import Foundation
import CoreLocation
public class BeaconManager:NSObject,CLLocationManagerDelegate{
var locationManager:CLLocationManager = CLLocationManager()
public override init() {
super.init()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.startUpdatingLocation()
}
public func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
println(location)
}
}
}
我从具有这样框架的测试应用程序调用它
import UIKit
import OtravitaSDK
import CoreLocation
class ViewController: UIViewController {
var bm = BeaconManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
但是不工作,不打印位置。我在框架的info.plist和app的info.plist中都设置了NSLocationAlwaysUsageDescription
你可以添加你的描述在qazxsw poi&qazxsw poi in plist
这段代码放入NSLocationAlwaysUsageDescription
文件中
NSLocationWhenInUseUsageDescription
从iOS 8开始,您需要做的是配置AppDelegate
以满足2种位置行为。您需要提供默认情况下显示弹出窗口的默认消息,要求用户同意使用其位置。
var locationManager:CLLocationManager?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//You can give this permission for fetch current location
var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound;
var setting = UIUserNotificationSettings(forTypes: type, categories: nil);
UIApplication.sharedApplication().registerUserNotificationSettings(setting);
UIApplication.sharedApplication().registerForRemoteNotifications();
locationManager = CLLocationManager()
locationManager?.requestAlwaysAuthorization()
locationManager?.delegate = self
locationManager?.startUpdatingLocation()
// Override point for customization after application launch.
return true
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
println(location)
}
}
和Info.plist file
有关完整演练的信息,请参阅NSLocationWhenInUseUsageDescription
,讨论此主题的NSLocationAlwaysUsageDescription
。希望这可以帮助!
您可以首先创建将实现CLLocationManagerDelegate的记者类(带有共享实例),这样您就可以在委托方法中实现您的逻辑
this article
接下来,您可以创建一个Client类
another SO post
最后在您需要的地方调用Client方法
import Foundation
import CoreLocation
class LocationReporter: NSObject, CLLocationManagerDelegate {
static let sharedInstance = LocationReporter()
func startUpdating(locationManager: CLLocationManager) {
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func stopUpdating(locationManager: CLLocationManager) {
locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
print("latitude: ", location.coordinate.latitude)
print("longitude: ", location.coordinate.longitude)
}
}
//implement other locationManger delegate methods
}
希望这会有所帮助