iOS 框架的核心位置

Core Location to an iOS Framework

我正在创建一个 iOS 框架,我想使用 Core Location 与信标进行交互。出于测试原因,我正在尝试获取用户位置。

这是我在框架中创建的class。

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 和应用的 info.plist

中设置了 NSLocationAlwaysUsageDescription

从 iOS 8 开始,您需要做的是配置您的 Info.plist file 以适应 2 种定位行为。您需要提供一条默认消息,默认情况下会弹出一个消息,询问用户是否同意使用他们的位置。

NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

请参阅讨论此主题的 this article for a full walkthrough and another SO post。希望这对您有所帮助!

您可以在 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 的 plist

中添加描述

此代码放入 AppDelegate 文件

     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)
        }
    }

您可以首先创建将实现 CLLocationManagerDelegate 的报告器 class(使用共享实例),这样您就可以在委托方法中实现您的逻辑

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


}

接下来可以创建客户端class

 import Foundation
 import CoreLocation

 class LocationDetectionClient {

    private let locationManager = CLLocationManager()

    func start() {
        LocationReporter.sharedInstance.startUpdating(locationManager: locationManager)
    }

    func stop() {
        LocationReporter.sharedInstance.stopUpdating(locationManager: locationManager)
    }

}

最后在需要的地方调用客户端方法

let locationDetectionClient = LocationDetectionClient()

public func startLocationDetection() {
    locationDetectionClient.start()
}

public func stopLocationDetection() {
    locationDetectionClient.stop()
}

希望这会有所帮助