ZBeaconKitKDDI 1.2.6

ZBeaconKitKDDI 1.2.6

维护者:WooEng



  • Eng

ZBeaconKit for KDDI

先决条件

  • Location Always Usage 权限应该开启(用于 iBeacon 区域监控)。
  • 设备蓝牙服务应始终处于活跃状态(iBeacon 是 BLE Beacon 规范之一,它需要蓝牙才能正常工作)。

将 ZBeaconKit for KDDI 集成到您的 iOS 应用中。

添加 Location Always Usage 权限。

将以下键添加到项目的 info.plist 中: NSLocationAlwaysUsageDescriptionNSLocationAlwaysAndWhenInUseUsageDescriptionNSLocationWhenInUseUsageDescription更多信息

  • 对于 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>  

安装 Cocoapods

  • 将以下内容添加到您的 Podfile 中
target YOUR_PROJECT_TARGET do
  pod 'ZBeaconKitKDDI'
end
  • 运行以下命令
pod repo update
pod install

安装 carthage

添加复制文件运行脚本。

  • 在您的应用程序目标的“构建阶段”设置选项卡中,单击“+”图标并选择“新运行脚本阶段”。创建一个运行脚本,内容如下
/usr/local/bin/carthage copy-frameworks

并在“输入文件”下添加您要使用的框架的路径,例如

$(SRCROOT)/ZBeaconKitKDDI.framework

最后一步,配置您的应用特定信息。

如果您使用 Swift 构建

  • 导入 ZBeaconKitKDDI,配置您的应用认证信息。
import ZBeaconKitKDDI
...

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 构建

  • 构建设置 中启用 支持 Swift 代码的嵌入式内容 标志。

  • 导入 <ZBeaconKitKDDI/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];