【发布时间】:2023-04-08 19:25:02
【问题描述】:
我已经建立了一个应该接收 ibeacons 的类。 据我所知,我做的一切都是正确的。但它不起作用。 我的应用程序不要求位置权限,并且位置管理器不会开始更新。 我还在 plist 中设置了属性。
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
仅当我在 AppDelegate 中初始化所有内容时才有效。 就像这里的教程一样。
但我想将此服务外包给一个班级。 我不知道我做错了什么。我是 Swift 的新手,在 Objective-C 中它一直有效。 谁能帮帮我?
这里是代码-
视图控制器
导入 UIKit
类 ViewController: UIViewController,BeaconServiceProtocolDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//Set BackgroundImage and hide Navigationbar
self.view.backgroundColor = UIColor(patternImage: UIImage(named:"bg"));
self.navigationController?.navigationBar.hidden = true
////////////////////////create new Beacon Service which is looking for beacon which are given with the parameters
var beaconAdvertiseService = BeaconService(parauuidString: "F0018B9B-7509-4C31-A905-1A27D39C003C",parabeaconIdentifier:"iBeaconTalentTeamAudi");
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//Notification if our service found a beacon
func sendLocalNotificationWithMessage(message: String!) {
let notification:UILocalNotification = UILocalNotification()
notification.alertBody = message
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
//Delegate from BeaconService
func foundBeacon() {
println("Found Beacon")
}
}
当然还有这个类 - BeaconAdvertiseService
import Foundation
import CoreBluetooth
import CoreLocation
//BeaconService Delegate
protocol BeaconServiceProtocolDelegate
{
func foundBeacon()
}
import Foundation
import CoreBluetooth
import CoreLocation
//BeaconService Delegate
protocol BeaconServiceProtocolDelegate
{
func foundBeacon()
}
class BeaconService: NSObject, CLLocationManagerDelegate{
let beaconServiceDelegate:BeaconServiceProtocolDelegate?
let uuidString:String//UUID Beacon as String
let beaconIdentifier:String//Identifier Beacon
let beaconUUID:NSUUID//UUID Beacon
let beaconRegion:CLBeaconRegion//Beacon Region
let locationManager:CLLocationManager ////location manager
////////////////////////Init class (constructor) // Params UUID as String & and Identifier from beacon
init(parauuidString:String,parabeaconIdentifier:String) {
self.uuidString = parauuidString
self.beaconIdentifier = parabeaconIdentifier
self.beaconUUID = NSUUID(UUIDString: self.uuidString)
self.beaconRegion = CLBeaconRegion(proximityUUID:self.beaconUUID,identifier: self.beaconIdentifier)
self.locationManager = CLLocationManager();
super.init()
self.initLocationMangager()
}
////////////////////////Init location manager
private func initLocationMangager()
{
println(self.locationManager)
if(self.locationManager.respondsToSelector("requestAlwaysAuthorization")) {
locationManager.requestAlwaysAuthorization()
}
self.locationManager.delegate = self
self.locationManager.pausesLocationUpdatesAutomatically = false
self.locationManager.startMonitoringForRegion(beaconRegion)
self.locationManager.startRangingBeaconsInRegion(beaconRegion)
self.locationManager.startUpdatingLocation()
}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
////////////////////////LocationManager Delegates
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
////////////////////////LocationManager didStartMonitoringForRegion
func locationManager(manager: CLLocationManager!, didStartMonitoringForRegion region: CLRegion!) {
manager.startRangingBeaconsInRegion(region as CLBeaconRegion)
println("Did Start")
}
////////////////////////LocationManager didEnterRegion
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
manager.startRangingBeaconsInRegion(region as CLBeaconRegion)
}
////////////////////////LocationManager didRangeBeacons
func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
var message:String = ""
if(beacons.count > 0) {
let nearestBeacon:CLBeacon = beacons[0] as CLBeacon
switch nearestBeacon.proximity {
case CLProximity.Far:
message = "Far"
case CLProximity.Near:
message = "Near"
case CLProximity.Immediate:
message = "Immediate"
case CLProximity.Unknown:
return
}
} else {
message = "No Beacons"
}
//Call BeaconServiceDelegate
beaconServiceDelegate?.foundBeacon()
}
////////////////////////LocationManager monitoringDidFailForRegion
func locationManager(manager: CLLocationManager!, monitoringDidFailForRegion region: CLRegion!, withError error: NSError!) {
println(error)
}
////////////////////////LocationManager didExitRegion
func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) {
manager.stopRangingBeaconsInRegion(region as CLBeaconRegion)
}
}
【问题讨论】: