美文网首页唤起app
iOS-iBeacon靠近/离开推送

iOS-iBeacon靠近/离开推送

作者: 小天枢丶 | 来源:发表于2018-01-17 14:35 被阅读0次

最近用到iBeacon进行室内定位,发现这东西可以在不打开app的情况下靠近/离开iBeacon设备发送本地推送,根据网上的一些iBeacon教程搞了一天发现并不能触发这个方法,然后各种翻谷歌、论坛解决掉了。

先记录一下iBeacon的基本使用

首先需要一个能发送iBeacon信号的蓝牙芯片或者设备(我用的是一台6s测试机,在AppStore上可以下载一些能发送iBeacon信号的app或者自己写一个)

先创建一个BeaconItem.swift的文件

import CoreLocation

class BeaconItem: NSObject {
    let majorValue: CLBeaconMajorValue
    let minorValue: CLBeaconMinorValue
    let name: String
    let uuid: UUID
    
    init(name: String, uuid: UUID,
         majorValue: CLBeaconMajorValue,
         minorValue: CLBeaconMinorValue) {
        self.name = name
        self.uuid = uuid
        self.majorValue = majorValue
        self.minorValue = minorValue
    }
}

这是把iBeacon设备的一些数据进行封装

然后在ViewController里面写入(记得要先去Info.plist填写获取CoreLocation相关权限)

import UIKit
import CoreLocation

class ViewController: UIViewController {
    let locationManager = CLLocationManager()
    let beaconItem = BeaconItem(name: "test", uuid: UUID(uuidString: "FDA50693-A4E2-4FB1-AFCF-C6EB07647825")!, majorValue: 0, minorValue: 0)

    override func viewDidLoad() {
        super.viewDidLoad()
        
        locationManager.requestAlwaysAuthorization()
        locationManager.delegate = self
        startMonitoring(item: item)
    }
    
    /// 根据BeaconItem对象创建CLBeaconRegion
    ///
    /// - Parameter item: BeaconItem
    /// - Returns: CLBeaconRegion
    func beaconRegionWith(item: BeaconItem) -> CLBeaconRegion {
        let beaconRegion = CLBeaconRegion(proximityUUID: item.uuid, major: item.majorValue, minor: item.minorValue, identifier: item.name)
        
        return beaconRegion
    }
    
    /// 监听指定的iBeacon设备
    ///
    /// - Parameter item: BeaconItem
    func startMonitoring(item: BeaconItem) {
        let beaconRegion = beaconRegionWith(item: item)
        locationManager.startMonitoring(for: beaconRegion)
        locationManager.startRangingBeacons(in: beaconRegion)
    }
}

extension ViewController: CLLocationManagerDelegate {

    // 检测iBeacon设备的回调
    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        for beacon in beacons {
            // 可以获取iBeacon的uuid、major、minor、rssi还有距离
        }
    }
}
靠近/离开推送

通过上面的代码就可以运行监测指定的iBeacon设备的距离,但是如果想做到手机靠近则触发推送的话还需要做以下操作(配置推送这里不做介绍):

在AppDelegate.swift中加入以下内容

import CoreLocation

class AppDelegate: UIResponder, UIApplicationDelegate {

    let locationManager = CLLocationManager()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        locationManager.delegate = self
        
        return true
    }

extension AppDelegate: CLLocationManagerDelegate {
    
    // 用户进入iBeacon范围调用
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        // 推送代码
    }
    // 用户离开iBeacon范围调用    
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
         // 推送代码
    }

}

网上的很多教程基本就这样,然后运行,拿着设备靠近、走开发现根本就不会调用那两个方法,查资料后发现漏了点东西,还需要在刚刚的ViewController里面

startMonitoring(item: item)

这句之前加上

CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self)
CLLocationManager.authorizationStatus()

完整代码如下:

override func viewDidLoad() {
        super.viewDidLoad()
    
        locationManager.requestAlwaysAuthorization()
        locationManager.delegate = self
        CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self)
        CLLocationManager.authorizationStatus()
        startMonitoring(item: item)
    }

现在运行就可以达到预想中的效果。

相关文章

  • iOS-iBeacon靠近/离开推送

    最近用到iBeacon进行室内定位,发现这东西可以在不打开app的情况下靠近/离开iBeacon设备发送本地推送,...

  • 你靠近又离开

    我在黄昏出门 一只鸟停留在那儿 另一只一点点靠近它 突然惊起 飞离 那只鸟儿还在那儿 直到我离去 还在那儿 与月亮

  • 靠近谁,离开谁

    我喜欢更好的人,我喜欢与更优秀的人为伍,所以我选择离开你。我曾经对你好,不是因为你有多好,而是我自己很好。现在的我...

  • 距离

    《距离》 身未动,心已远,心远地自偏 心动了,身不见,身远心反怜 离开,只为靠近,靠近自己的心 靠近,又想离开,去...

  • 以离开的方式靠近

    匆匆赶路的河水无意 山峰田野都精心打扮好了 不浓不淡的妆容 这乡村的情人叫做四月 花儿满心欢喜随意跑到山腰 或者随...

  • 若你靠近我,那我就离开

    若你靠近我,那我就离开 蝴蝶飞走了,蝴蝶就回来 天使的翅膀,落满了尘埃 火山的喷涌,宁静成天籁

  • 遇见你,靠近你,离开你。

    很多的时候,都觉得自己的内心很脆弱。 我总是在某一个瞬间,觉得自己越来越强大了,可是却又在某一个别人看起来无关紧要...

  • 从想靠近到不离开

    年轻的时候,总要给爱附加上很多条件,比如金钱,比如地位。 其实爱很纯粹,它不会因金钱而俯身,也不会因地位而低头。 ...

  • 我和你

    离开你,和靠近你一样远。――题记 我想离开你 和靠近你 一样长 一样短 一样近 一样远

  • 关系的纠缠

    有人说人与人之间的关系就像刺猬一样,离远了会觉得寒冷,离进来又会互相伤害,想靠近又靠近不了,想离开又离开不了,这就...

网友评论

    本文标题:iOS-iBeacon靠近/离开推送

    本文链接:https://www.haomeiwen.com/subject/bjfuoxtx.html