ZBeaconKit
先决条件
始终使用位置
权限应启用(针对 iBeacon 区域监控)。- 设备蓝牙服务应始终处于激活状态(iBeacon 是 BLE 信标规范之一,它需要蓝牙才能正常工作)。
将 ZBeaconKit 集成到您的 iOS 应用中。
始终使用位置
权限。
添加 将 NSLocationAlwaysUsageDescription
、NSLocationAlwaysAndWhenInUseUsageDescription
、NSLocationWhenInUseUsageDescription
键添加到项目的 info.plist
文件中。更多信息
- 对于 ios < 11
NSLocationAlwaysUsageDescription
- 对于 ios >= 11
NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationWhenInUseUsageDescription
您可以更改消息。
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>
ZBeaconKit.framework
,并将其复制到您的项目根目录。
下载
ZBeaconKit.framework
拖动到您的 Xcode 项目路径中。
将
carthage
。
安装
添加复制文件的运行脚本。
- 在应用程序目标的“构建阶段”设置选项卡中,点击“+”图标并选择“新建运行脚本阶段”。创建一个带有以下内容的运行脚本
/usr/local/bin/carthage copy-frameworks
并在“输入文件”下添加您想要使用的框架的路径,例如
$(SRCROOT)/ZBeaconKit.framework
最后一步,配置您的应用特定信息。
如果您使用 Swift 构建
- 导入
ZBeaconKit
,配置您的应用认证信息。
import ZBeaconKit
...
override func viewDidLoad() {
super.viewDidLoad()
let manager = Manager(
email: "[email protected]",
authToken: "YOUR_AUTH_TOKEN",
target: .Production // For development, use .Development
)
Manager.debugMode = true // For debugging
Manager.customerId = self.generateSampleCustomerId()
// You must start manager manually.
manager.start()
// And if you want to stop,
manager.stop()
}
func generateSampleCustomerId() -> String {
let deviceId = UIDevice.current.identifierForVendor?.uuidString
let deviceIdWithSalt = deviceId! + "YOUR_SALT"
return deviceIdWithSalt.hmac(.sha512, key: "YOUR_KEY_FOR_HMAC")
}
如果您是用Objective-C构建的
- 在
Build Settings
中启用Embedded Content Contains Swift Code
标志。
- 导入
<ZBeaconKit/ZBeaconKit.h>
,配置您的应用认证信息。
- (void)viewDidLoad {
[super viewDidLoad];
self.manager = [[Manager alloc]
initWithEmail:@"[email protected]"
authToken:@"A1B2C3D4E5F6"
target:TargetProduction]; // For development, use TargetDevelopment
[Manager setDebugMode:true]; // For debugging
NSString *deviceId = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSString *deviceIdWithSalt = [deviceId stringByAppendingString:@"YOUR_SALT"];
NSString *customerId = [self hmac: deviceIdWithSalt withKey: @"YOUR_KEY_FOR_HMAC"];
[Manager setCustomerId: customerId];
// You must start manager manually.
[self.manager start];
NSLog(@"%@", [Manager customerId]);
NSLog(@"%@", [Manager packageId]);
// And if you want to stop,
[self.manager stop];
}
- (NSString *) hmac: (NSString *)plaintext withKey:(NSString *)key
{
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [plaintext cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMACData = [NSData dataWithBytes:cHMAC length:sizeof(cHMAC)];
const unsigned char *buffer = (const unsigned char *)[HMACData bytes];
NSMutableString *HMAC = [NSMutableString stringWithCapacity:HMACData.length * 2];
for (int i = 0; i < HMACData.length; ++i){
[HMAC appendFormat:@"%02x", buffer[i]];
}
return HMAC;
}
部署目标
注意 初始化时必须向经理描述部署目标。部署目标取决于ZOYI的实际O2O服务器端点。
此标志仅用于与ZOYI公司的互测。因此,大多数第三方不需要更改此设置。
Swift案例
// Set target as PRODUCTION
let manager = Manager(
email: "...",
authToken: "...",
target: .Production
)
// Set target as DEVELOPMENT (signals can not be seen by BLE API)
let manager = Manager(
email: "...",
authToken: "...",
target: .Development
)
Objective-C
// Set target as PRODUCTION
self.manager = [[Manager alloc]
initWithEmail:@"..."
authToken:@"..."
target:TargetProduction];
// Set target as DEVELOPMENT (signals can not be seen by BLE API)
self.manager = [[Manager alloc]
initWithEmail:@"..."
authToken:@"..."
target:TargetDevelopment];