BleshSDK 5.5.0

BleshSDK 5.5.0

测试已测试
语言语言 Obj-CObjective C
许可证 未知
发布最新版本2023年10月

Arda YalçınerBlesh Technology维护。



BleshSDK 5.5.0

  • 作者
  • Blesh 技术团队

Blesh iOS SDK 5 开发者指南

版本: 5.4.10

本文档描述了 Blesh iOS SDK 与您的 iOS 应用程序的集成。

简介

Blesh iOS SDK 从安装有 iOS 应用的设备中收集位置信息。Blesh Ads 平台使用这些数据创建和增强受众群体,提供定向广告,并生成见解。

目录

  • 5.4.10 (发布日期:2023-07-20)

    • 添加了用于识别 Blesh 通知的辅助方法
  • 5.4.9 (发布日期:2023-07-14)

    • 改进了 APNs 支持
    • 最低 iOS 版本设置为 11
  • 5.4.8 (发布日期:2023-04-15)

    • 改进了与 Xcode 13+ 的兼容性
  • 5.4.7 (发布日期:2023-01-12)

    • 添加了对构建版本 React Native 的支持
  • 5.4.6 (发布日期:2022-12-20)

    • 添加了对 iOS 15+ 位置请求的兼容性
  • 5.4.5 (发布日期:2022-11-11)

    • 改进了信标扫描
  • 5.4.4 (发布日期:2022-11-10)

    • 改进了远程推送通知消息处理
  • 5.4.3 (发布日期:2022-11-08)

    • 添加了对文本 PN 令牌的支持
  • 5.4.2 (发布日期:2022-08-26)

    • 添加了向后兼容的初始化器
  • 5.4.1 (发布日期:2022-08-15)

    • 将 Swift 编译器更新到 5.6
    • 再次将 start(withSecretKey ...) 方法公开
  • 5.4.0 (发布日期:2022-07-03)

    • 添加了信标扫描
    • 添加了远程推送通知
    • 添加了运动活动过渡
  • 5.3.0 (发布日期:2022-04-12)

    • 添加了内联行为跟踪
  • 5.2.10 (发布日期:2022-03-22)

    • 为模拟器添加了对 arm64 的支持
  • 5.2.9 (发布日期:2021-11-22)

    • 将 Swift 编译器更新到 5.5
  • 5.2.8 (发布日期:2021-09-22)

    • 将 Swift 编译器更新到 5.4
  • 5.2.7 (发布日期:2021-05-04)

    • 作为 xcframework 发布
  • 5.2.6 (发布日期:2020-10-05)

    • 将 Swift 编译器更新到 5.3
  • 5.2.5 (发布日期:2020-07-27)

    • 将 Swift 编译器更新到 5.1
  • 5.2.4 (发布日期:2020-07-27)

    • 增强了渲染效果
  • 5.2.3 (发布日期:2020-05-20)

    • 添加了 SDK 的存档版本
  • 5.2.2 (发布日期:2020-05-18)

    • 为所有架构启用了位代码
  • 5.2.1 (发布日期:2020-05-08)

    • 添加了对自定义背景层的支持
  • 5.2.0 (发布日期:2020-05-02)

    • 将 Swift 编译器更新到 5.2
  • 5.1.5 (发布日期:2020-04-18)

    • 启用了位代码
  • 5.1.4 (发布日期:2020-01-20)

    • 将 Swift 编译器更改到 5
  • 5.1.3 (发布日期:2020-01-07)

    • 改进了与 iOS 13 的兼容性
  • 5.1.2 (发布日期:2020-01-07)

    • 改进了与 iOS 13 的兼容性
  • 5.1.1 (发布日期:2020-01-06)

    • 添加了英语和土耳其语的本地化支持
  • 5.1.0 (发布日期:2019-12-27)

    • 添加了本地推送通知支持
    • 增加了更多位置处理程序
  • 5.0.1 (发布于 2019-11-28)

    • 移除了 Core Bluetooth 框架
  • 5.0.0 (发布于 2019-11-26)

    • 增加了初始化支持
    • 增加了处理位置权限变化回调处理程序
    • 支持服务器端 HTTP 压缩
    • 编译为 Mac-O 通用二进制文件

需求

为了集成 Blesh iOS SDK,请确保您

  • 针对 iOS 版本 11 或更高版本
  • 针对 Swift 5 编译器
  • 启用 "始终嵌入 Swift 标准库" 构建选项(或者对于旧版本的 Xcode,启用 "嵌入内容包含 Swift 代码" 构建选项),如果您的应用程序是用 Objective-C 语言开发的
    • Swift 标准库对于 iOS 版本 12.1 或更早版本是必需的。有关详细信息,请参阅 QA1881
  • Blesh Publisher Portal 上注册
    • 您可能需要为 iOS 平台创建一个 Blesh Ads Platform 访问密钥

注意:请确保在 App Store Connect 上声明“您的应用程序使用 IDFA”。否则,您的应用程序可能会在审查时被拒绝。Blesh iOS SDK 从设备收集 IDFA,完全符合 Apple要求

集成

1. 添加 Blesh iOS SDK

Blesh iOS SDK 可以通过使用各种依赖管理器或手动方式添加。

1.1. 使用 Swift Package Manager 添加 Blesh iOS SDK

较新版本的 Xcode 支持通过 IDE 界面添加包。按照以下步骤可以轻松集成 Blesh iOS SDK。

添加步骤

  1. 在 "文件" 菜单中选择 "添加包"。
  2. 在 "搜索或输入包 URL" 输入框中输入 GitHub 地址 https://github.com/bleshcom/Blesh-iOS-SDK
  3. Xcode 会自动获取包详情。在 "依赖规则" 输入框中选择 "到下一个主要版本"。这可以使您轻松地保持 Blesh iOS SDK 的更新。
  4. 在 "添加到项目" 输入框中选择您的应用程序项目。
  5. 单击 "添加包" 按钮并选择 "添加到目标" 列表中的您的应用程序项目。
  6. 单击 "添加包" 按钮。

1.2. 使用 CocoaPods 添加 Blesh iOS SDK

Podfile 中引用 BleshSDK pod 就可以添加 Blesh iOS SDK 到您的项目中。

添加步骤

  1. 如果您的项目没有 Podfile,您可以在终端运行以下命令创建一个:
pod init
  1. Podfile 中引用 BleshSDK
target 'YOUR_APPLICATION_NAME' do

  # ... beginning of your Podfile ...

  pod 'BleshSDK' # this will reference the Blesh iOS SDK 5

  # ... remaining of your Podfile ...

end

注意:YOUR_APPLICATION_NAME 替换为 target 部分中您应用程序的名称

  1. 通过在终端运行以下命令安装 pods
pod install

1.3. 手动添加 Blesh iOS SDK

  1. 下载 SDK

您可以从以下仓库下载 SDK

https://github.com/bleshcom/Blesh-iOS-SDK.git

为了集成 SDK 的特定版本,应该检出带有所需版本标签的 git 修订版本。

  1. BleshSDK.framework 添加到您的 Xcode 项目中

2. 通知 Blesh iOS SDK 关于推送通知

Blesh iOS SDK 必须在推送通知即将显示以及用户与推送通知交互时得到通知。这将允许 Blesh iOS SDK

  • 在应用处于前台时显示通知(iOS 10+)
  • 当用户点击通知时显示广告

Blesh iOS SDK 支持通过 Apple Push Notification 服务(APNs)和 Firebase 云消息(FCM)进行远程推送通知。

注意: APNs 与 Firebase 证书需要在 Blesh 发布者门户 上注册。

2.1. APNs

以下示例假设您已经将 Apple Push Notification 服务集成到您的应用程序中。有关更多信息,请参阅 APNs iOS 文档

2.1.1. 示例

Swift

import UIKit
import UserNotifications
import BleshSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // mark this class as a UNUserNotificationCenterDelegate
        UNUserNotificationCenter.current().delegate = self

        // enable remote notifications
        application.registerForRemoteNotifications()

        // ... rest of the method ...

        return true
    }

    // support remote notifications
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // notify Blesh SDK
        BleshSdk.sharedInstance.didReceiveRemoteNotification(userInfo) { UIBackgroundFetchResult in
            completionHandler(.newData)
        }

        // ... rest of the method ...
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        NSLog("Failed to register for remote notifications: \(error)")
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // notify Blesh SDK
        BleshSdk.sharedInstance.didReceiveDeviceToken(deviceToken)

        // ... rest of the method ...
    }

    // this method will be called when app received push notifications in foreground
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .badge, .sound])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // deliver to Blesh SDK
        BleshSdk.sharedInstance.didReceiveUNNotificationResponse(response)

        completionHandler()
    }

    // ... rest of the class ...
}

示例: Objective-C (AppDelegate.h)

#import <UserNotifications/UserNotifications.h>

// ... rest of imports ...

@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>

// ... rest of the interface ...

@end

示例: Objective-C (AppDelegate.m)

#import <BleshSDK/BleshSDK.h>
// ... rest of imports ...

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // mark this class as a UNUserNotificationCenterDelegate
    [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];

    // enable remote notifications
    [[UIApplication sharedApplication] registerForRemoteNotifications];

    // ... rest of the method ...

    return YES;
}

// support remote notifications
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // notify Blesh SDK
    [[BleshSdk sharedInstance] didReceiveRemoteNotification:userInfo completion:^(UIBackgroundFetchResult result) {
        completionHandler(UIBackgroundFetchResultNewData);
    }];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // notify Blesh SDK
    [[BleshSdk sharedInstance] didReceiveDeviceToken:deviceToken];
}

// this method will be called when app received push notifications in foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
  // deliver to Blesh SDK
  [[BleshSdk sharedInstance] didReceiveUNNotificationResponse:response];

  completionHandler();
}

// ... rest of the class ...

@end

2.2. FCM

以下示例假设您已经将Firebase消息传递集成到您的应用程序中。有关更多信息,请参考FCM iOS文档

2.2.1. 示例

Swift

import UIKit
import UserNotifications
import BleshSDK
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // mark this class as a UNUserNotificationCenterDelegate
        UNUserNotificationCenter.current().delegate = self

        // enable remote notifications
        application.registerForRemoteNotifications()

        // ... rest of the method ...

        return true
    }

    // support remote notifications
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // notify FCM
        Messaging.messaging().appDidReceiveMessage(userInfo)

        // notify Blesh SDK
        BleshSdk.sharedInstance.didReceiveRemoteNotification(userInfo) { UIBackgroundFetchResult in
            completionHandler(.newData)
        }

        // ... rest of the method ...
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // notify FCM
        Messaging.messaging().apnsToken = deviceToken

        // notify Blesh SDK
        BleshSdk.sharedInstance.didReceiveDeviceToken(deviceToken)

        // ... rest of the method ...
    }

    // this method will be called when app received push notifications in foreground
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        // deliver to FCM
        Messaging.messaging().appDidReceiveMessage(notification.request.content.userInfo)

        completionHandler([.alert, .badge, .sound])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // deliver to FCM
        Messaging.messaging().appDidReceiveMessage(response.notification.request.content.userInfo)

        // deliver to Blesh SDK
        BleshSdk.sharedInstance.didReceiveUNNotificationResponse(response)

        completionHandler()
    }

    // ... rest of the class ...
}

示例: Objective-C (AppDelegate.h)

#import <UserNotifications/UserNotifications.h>
#import <FirebaseMessaging/FirebaseMessaging.h>

// ... rest of imports ...

@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate>

// ... rest of the interface ...

@end

示例: Objective-C (AppDelegate.m)

#import <BleshSDK/BleshSDK.h>
// ... rest of imports ...

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // mark this class as a UNUserNotificationCenterDelegate
    [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];

    // enable remote notifications
    [[UIApplication sharedApplication] registerForRemoteNotifications];

    // ... rest of the method ...

    return YES;
}

// support remote notifications
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // notify FCM
    [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

    // notify Blesh SDK
    [[BleshSdk sharedInstance] didReceiveRemoteNotification:userInfo completion:^(UIBackgroundFetchResult result) {
        completionHandler(UIBackgroundFetchResultNewData);
    }];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // notify FCM
    [[FIRMessaging messaging] setAPNSToken:deviceToken];

    // notify Blesh SDK
    [[BleshSdk sharedInstance] didReceiveDeviceToken:deviceToken];
}

// this method will be called when app received push notifications in foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  // deliver to FCM
  [[FIRMessaging messaging] appDidReceiveMessage:notification.request.content.userInfo];

  completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
  // deliver to FCM
  [[FIRMessaging messaging] appDidReceiveMessage:response.notification.request.content.userInfo];

  // deliver to Blesh SDK
  [[BleshSdk sharedInstance] didReceiveUNNotificationResponse:response];

  completionHandler();
}

// ... rest of the class ...

@end

3. 添加框架

Blesh iOS SDK使用了以下框架。请确保您的项目引用了所有这些框架

  • Foundation.framework
  • UIKit.framework
  • AdSupport.framework
  • CoreLocation.framework
  • CoreTelephony.framework
  • SystemConfiguration.framework

4. 添加支持文件

Blesh iOS SDK为其支持的广告播放默认声音文件BleshNotification.caf。您可以从此链接下载声音文件并复制到应用程序的支持文件文件夹中。此链接。如果此文件不存在,则SDK将播放默认iOS通知声音。

5. 检查权限

为了提供适当的推送通知和适当的信标跟踪,您需要从应用程序用户那里获取一些权限,在您的应用安装之后。根据iOS用户指南,应用可以使用自己的语句请求所需权限。这些权限可以在Info.plist文件中配置。

Blesh iOS SDK 使用 iBeacon 协议,默认需要地理位置权限。Blesh iOS SDK 即使在应用程序处于后台或被杀掉的状态下,也需要检测信标和地理围栏。因此,您必须向您的用户请求 "始终使用位置" 权限。

在 iOS 11 之前,当用户授权使用位置时,被认为是 "始终" ,并且应用程序可以在被杀或处于后台时使用位置。

在 iOS 11 之后,规则发生了变化,用户只能在应用程序在使用时才能授权使用位置权限。因此,对于 iOS 11 及更高版本的应用程序,必须请求两种不同类型的地理位置权限: WhenInUseAlwaysAndWhenInUse

为了向后兼容,您的应用程序应包含以下三个地理位置权限描述符

  • NSLocationAlwaysAndWhenInUseUsageDescription
  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

在地理位置权限项目的描述字段中,我们建议您鼓励您的用户允许 "始终使用我的位置" ,以获得更好的系统性能。以下小节提供了样品描述文本。请检查并考虑。

请注意,iOS 版本 11 之后,应用程序必须在其 Info.plist 文件中包含以下所有 3 个描述符,以便与较旧的操作系统版本保持向后兼容。

  1. 始终与使用中的

此描述符用于 iOS 11 及更高版本。提供了在使用中以及被杀掉时的权限。

示例文本:"此应用程序使用您的位置,以便向您通报附近的有趣优惠。我们建议您选择始终选项,以便即使在您不使用应用程序时也能获得优惠!"

您可以使用以下语法将其插入到您的 Info.plist 文件中。

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string><# insert always and when in use usage description text #></string>
  1. 始终

此描述符用于 iOS 10 及更早版本。提供了在使用中以及被杀掉时的权限。

示例文本:"此应用程序使用您的位置,以便向您通报附近的有趣优惠。我们建议您选择始终选项,以便即使在您不使用应用程序时也能获得优惠!"

您可以使用以下语法将其插入到您的 Info.plist 文件中。

<key>NSLocationAlwaysUsageDescription</key>
<string><# insert always usage description text #></string>
  1. 使用中

此描述符适用于所有 iOS 版本。仅在应用程序使用时提供权限。我们建议您提醒您的用户,仅允许使用时使用位置可能会导致查找附近优惠的性能非常低。

示例文本:“此应用程序使用您的位置,以便向您通报附近的有趣优惠。仅允许使用时可能在使用附近优惠时导致性能差!”

您可以使用以下语法将其插入到您的 Info.plist 文件中。

<key>NSLocationWhenInUseUsageDescription</key>
<string><# insert when in use usage description text #></string>

使用

为了利用 Blesh iOS SDK 的功能,它必须在应用程序的生命周期中的某个点启动。

1. 输入Blesh Ads平台访问密钥

Blesh iOS SDK要求Blesh Ads平台访问密钥。您可能需要在的iOS平台上创建一个密钥。如果您没有的账户,请联系我们的[email protected]。您可以在应用程序的Info.plist文件中用Blesh Platform Access Key键设置您的密钥

<key>Blesh Platform Access Key</key>
<string>YOUR_SECRET_KEY_HERE</string>

或者,您可以通过SDK的start方法提供Blesh Ads平台访问密钥,详细信息见下一节。

2. 启动Blesh iOS SDK

您可以选择创建一个新的BleshSdk实例进行管理,或者通过BleshSdk.sharedInstance访问BleshSdk的单例实例。

为了即使在应用程序被终止/不在后台运行时也能继续接收通知,调用start方法不需要应用程序在前台。请注意最佳实践是在应用程序代理中的applicationDidFinishLaunchingWithOptions中启动Blesh。

BleshSdk包含以下start方法

Swift

start(
      withSecretKey: String?,
      withApplicationUser: BleshSdkApplicationUser?,
      withConfiguration: BleshSdkConfiguration?,
      completionHandler: ((BleshSdkStartState) -> Void)?)

Objective-C

- (void)startWithApplicationUser:(BleshSdkApplicationUser *)applicationUser 
               withConfiguration:(BleshSdkConfiguration *)configuration 
               completionHandler:(void (^)(enum BleshSdkStartState))completionHandler;

- (void)startWithSecretKey:(NSString *)secretKey 
       withApplicationUser:(BleshSdkApplicationUser *)applicationUser 
         withConfiguration:(BleshSdkConfiguration *)configuration 
         completionHandler:(void (^)(enum BleshSdkStartState))completionHandler;
  • 如果您不选择使用Info.plist文件来定义Blesh Ads平台访问密钥,您也可以使用withSecretKey参数传递它。

  • withApplicationUser参数允许您通过提供有关应用程序主要用户(订阅者)的信息来增强观众数据。您可以在您的应用程序中提供任何使订阅者独一无二的信息。BleshSdkApplicationUser类包含以下

描述 Swift属性 Objective-C属性 例子
用户的可选唯一标识符 userId: String? (NSString *)userId 42
可选的用户的性别(.female或.male) gender: BleshSdkApplicationUserGender? (NSNumber *)genderCode .female (Swift) 0 (Obj-c)
可选的用户的出生年份 yearOfBirth: Int? (NSNumber *)yearOfBirth 1999
可选的用户的电子邮箱地址 email: String? (NSString *)email [email protected]
可选的用户的手机号码 phoneNumber: String? (NSString *)phoneNumber +905550000000
可选的用户的其他附加信息 other: Dictionary? (NSDictionary

**注意:** emailphoneNumber详细信息永远不会以明文方式发送到。这些值始终是不可逆的哈希,因此不会存储任何个人信息。

  • withConfiguration 参数允许您配置 Blesh iOS SDK 的行为。 BleshSdkConfiguration 类包含以下内容:
属性 类型 描述 例子
testMode 布尔值 在测试模式(true)中使用 SDK 或在发布模式(false)中使用 SDK false
pushNotificationToken 字符串 远程推送通知令牌(设备注册令牌) Messaging.messaging().fcmToken

有关 pushNotificationToken 的有效值,请参阅 FCM iOS 文档 以获取更多信息。

注意: testMode 默认关闭。您可以在集成测试期间启用此模式。当此标志设置为 true 时,不会影响发布环境。

  • completionHandler 参数允许您在 Blesh iOS SDK 初始化成功或失败后执行您的业务逻辑。
示例:简单初始化(单例)

您可以通过调用单例的 start 方法来开始 Blesh iOS SDK。

Swift

BleshSdk.sharedInstance.start()

Objective-C

[[BleshSdk sharedInstance] start];
示例:简单初始化

您可以通过调用实例的 start 方法来开始 Blesh iOS SDK。

Swift

let bleshSdk = BleshSdk()

bleshSdk.start()

Objective-C

BleshSdk* bleshSdk = [[BleshSdk alloc] init];
[bleshSdk start];
示例:完整初始化

Swift

let bleshSdkConfiguration = BleshSdkConfiguration(
	testMode: false,
	pushNotificationToken: Messaging.messaging().fcmToken ?? BleshSdk.sharedInstance.pushNotificationToken
)

let bleshSdkApplicationUser = BleshSdkApplicationUser(
	userId: "42",
	gender: .female,
	yearOfBirth: 1999,
	email: "[email protected]",
	phoneNumber: "+905550000000",
	other: nil
)

BleshSdk.sharedInstance.start(
	withApplicationUser: bleshSdkApplicationUser,
	withConfiguration: bleshSdkConfiguration) {
		(sdkState) -> () in
		// ... INSERT BUSINESS LOGIC HERE ...
		NSLog("BleshSDK start completed: " + sdkState.description)
	}

Objective-C

BleshSdkConfiguration *configuration = [[BleshSdkConfiguration alloc]
                                        initWithTestMode:false
                                        adsEnabled:true
                                        pushNotificationToken:[FIRMessaging messaging].FCMToken ?: [BleshSdk sharedInstance].pushNotificationToken];

BleshSdkApplicationUser *user = [[BleshSdkApplicationUser alloc] initWithUserId:@"42"
                                      genderCode:0 // 0: female 1: male
                                      yearOfBirth:@1999
                                      email:@"[email protected]"
                                      phoneNumber:@"+905550000000"
                                      other:nil];

[[BleshSdk sharedInstance] startWithApplicationUser:user
                                  withConfiguration:configuration
                                  completionHandler:^(enum BleshSdkStartState state) {
    // ... INSERT BUSINESS LOGIC HERE ...
    if (state == BleshSdkStartStateFailure) {
        NSLog(@"BleshSDK start completed: failure");
    } else if (state == BleshSdkStartStateSkipped) {
        NSLog(@"BleshSDK start completed: skipped");
    } else {
        NSLog(@"BleshSDK start completed: success");
    }
}];

3. 通知Blesh iOS SDK关于权限变更

从Blesh iOS SDK 4.0.7版本开始,SDK不再询问用户权限。您的应用需要主动请求位置权限。更多信息请见"审查权限"部分。

当位置权限发生变化时,您的应用应该使用新的状态来调用BleshSdk中的didChangeLocationAuthorization方法。

示例: Swift

import UIKit
import BleshSDK
import CoreLocation

class MyViewController: UIViewController, CLLocationManagerDelegate {
    var locationManager: CLLocationManager

    required init?(coder aDecoder: NSCoder) {
        self.locationManager = CLLocationManager()

        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.distanceFilter = 10
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.requestLocation()

        // ... rest of the method ...
    }

    public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        // Notify the Blesh iOS SDK about the change here
        BleshSdk.sharedInstance.didChangeLocationAuthorization(status)
    }

    // ... rest of the controller ...
}

示例: Objective-C (ViewController.h)

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <BleshSDK/BleshSDK.h>
// ... rest of imports ...

@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}

// ... rest of the interface ...

@end

示例: Objective-C (ViewController.m)

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    locationManager.distanceFilter = 10;
    [locationManager requestAlwaysAuthorization];
    [locationManager requestLocation];

    // ... rest of the method ...
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    // Notify the Blesh iOS SDK about the change here
    [[BleshSdk sharedInstance] didChangeLocationAuthorization:status];

    // ... rest of the method ...
}

// ... rest of the class ...

@end

4. 实现Blesh iOS SDK代理

Blesh iOS SDK允许您决定是否显示广告。BleshSdkDelegate协议提供了以下可选方法

optional func bleshSdk(_ sdk: BleshSdk, didCompleteStartWith state: BleshSdkStartState)
optional func bleshSdk(_ sdk: BleshSdk, willDisplayNotification notificationId: String) -> Bool

示例: Swift

import UIKit
import BleshSDK

class MyViewController: UIViewController, BleshSdkDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Mark this controller as the delegate of Blesh SDK
        BleshSdk.sharedInstance.delegate = self

        // ... rest of the method ...
    }


    func bleshSdk(_ sdk: BleshSdk, willDisplayNotification notificationId: String) -> Bool {
        NSLog("BleshSDK will display the notification with id: \(notificationId)")
        return true // allow BleshSDK to display this notification
    }

    // ... rest of the controller ...
}