【问题标题】:Swift - Get iBeacon - external classSwift - 获取 iBeacon - 外部类
【发布时间】:2023-04-08 19:25:02
【问题描述】:

我已经建立了一个应该接收 ibeacons 的类。 据我所知,我做的一切都是正确的。但它不起作用。 我的应用程序不要求位置权限,并且位置管理器不会开始更新。 我还在 plist 中设置了属性。

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

仅当我在 AppDelegate 中初始化所有内容时才有效。 就像这里的教程一样。

http://ibeaconmodules.us/blogs/news/14702963-tutorial-swift-based-ibeacon-app-development-with-corelocation-on-apple-ios-7-8

但我想将此服务外包给一个班级。 我不知道我做错了什么。我是 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)

    }

}

【问题讨论】:

    标签: swift location ibeacon


    【解决方案1】:

    看起来您正在将广告服务分配给 viewDidLoad 末尾的局部变量。这个实例将在这个方法结束时被销毁(一行之后;))

    请确保将您的广告客户分配给实例变量或属性,并将自己(您的视图控制器)分配给您的广告客户。

    这应该可以解决问题。

    【讨论】:

      【解决方案2】:

      我至少看到一件事非常简单。看起来 initLocationMangager() 方法永远不会被调用。您可能应该从init(parauuidString:String,parabeaconIdentifier:String) 方法中添加对此方法的调用。

      【讨论】:

        【解决方案3】:

        对不起,我忘记了,我已经拿出来了。 但它也不适合调用

        self.initLocationMangager()
        

        【讨论】:

          猜你喜欢
          • 2022-01-14
          • 1970-01-01
          • 1970-01-01
          • 2021-01-16
          • 1970-01-01
          • 2010-12-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多