NearBee iOS SDK
简介
NearBee SDK 是一种通过符合 Eddystone 标准的 BLE 网络实现近距离营销的简单方式。
安装
使用 Cocoapods (推荐)
将以下代码添加到项目的 Podfile 中,我们支持 iOS 10.0+ 请确保您的 pod 设置了正确的平台。
platform :ios, '10.0'
target '<My-App-Target>''
pod 'NearBee'
end
在项目目录中运行 pod install
手动操作
-
在您的系统上下载或克隆此仓库。
-
将NearBee.framework文件拖拽至您的Xcode项目。确保已勾选“复制到目标组文件夹”。
-
将
NearBee.framework
、EddystoneScanner.framework
和Socket.IO-Client-Swift.framework
添加到目标应用的嵌入二进制文件部分。 -
在目标应用目标的构建阶段下,在“与库链接”部分添加以下框架:
- CoreData.framework
- SystemConfiguration.framework
- CoreBluetooth.framework
- CoreLocation.framework
- EddystoneScanner.framework
- NearBee.framework
- Socket.IO-Client-Swift.framework
配置您的项目
- 在Info.plist中添加新字段,即
NSLocationAlwaysUsageDescription
、NSLocationAlwaysAndWhenInUsageDescription
、NSBluetoothPeripheralUsageDescription
,并添加相应的用户展示值。iOS 10及以上版本强制要求此操作。
先决条件
位置
应用应该在设置中照顾到接收通知所需的权限,以便进入信标区域。
蓝牙
应用应该负责开启蓝牙以获取信标的范围。
MY_DEVELOPER_TOKEN
应用应该在初始化SDK时提供开发者令牌。从Beaconstac仪表盘账号页面获取。
MY_ORGANIZATION
应用应该在初始化SDK时提供组织。从Beaconstac仪表盘账号页面获取。
监控区域
如果您使用高级位置管理器的区域监控API,确保它不会影响NearBee SDK。
设置
- 在应用的
Info.plist
中添加以下键和值
<key>co.nearbee.api_key</key>
<string>__MY_DEVELOPER_TOKEN__</string>
<key>co.nearbee.organization_id</key>
<string>__MY_ORGANIZATION__</string>
- 在您的类中导入框架头文件
import NearBee
import <NearBee/NearBee.h>
- 使用
一行的初始化
初始化NearBee
,初始化后立即开始扫描信标。
var nearBee = NearBee.initNearBee()
NearBee *nearBee = [NearBee initNearBee];
- 如果您希望控制信标的扫描开始和停止
nearBee.startScanning() // Starts scanning for beacons...
nearBee.stopScanning() // Stops scanning for beacons...
[nearBee startScanning];
[nearBee stopScanning];
- 实现
NearBeeDelegate
协议方法,以在UITableView
或UICollectionView
中显示信标
func onBeaconsFound(_ beacons: [NearBeeBeacon]) {
// Display Beacons
}
func onBeaconsUpdated(_ beacons: [NearBeeBeacon]) {
// Display Beacons
}
func onBeaconsLost(_ beacons: [NearBeeBeacon]) {
// Display Beacons
}
func onError(_ error: Error) {
// Show Error
}
- (void)onBeaconsUpdated:(NSArray<NearBeeBeacon *> * _Nonnull)beacons {
// Display Beacons
}
- (void)onBeaconsLost:(NSArray<NearBeeBeacon *> * _Nonnull)beacons {
// Display Beacons
}
- (void)onBeaconsFound:(NSArray<NearBeeBeacon *> * _Nonnull)beacons {
// Display Beacons
}
- (void)onError:(NSError * _Nonnull)error {
// Error
}
- 一旦用户点击通知,将其传递给NearBee SDK以显示通知
// In the class where you want to listen to notification events...
let nearBeeInstance = try! NearBee.sharedInstance()
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let isNearBeeNotification = nearBee.checkAndProcessNearbyNotification(response.notification)
if (isNearBeeNotification) {
completionHandler()
} else {
// Not a near bee notification, you need to handle
}
}
// In the class where you want to listen to notification events...
NSError *error = nil;
NearBee *nearBeeInstance = [NearBee sharedInstanceAndReturnError:&error];
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler {
BOOL isNearBeeNotification = [nearBee checkAndProcessNearbyNotification: response.notification];
if (isNearBeeNotification) {
completionHandler()
} else {
// Not a near bee notification, you need to handle
}
}