Braze Swift SDK
Braze Swift SDK 是一个全面的移动营销解决方案,为开发者提供了强大的用户互动和分析工具。该SDK支持多种消息格式和用户互动方式,帮助开发者构建卓越的移动应用体验。
功能特性
- 多平台支持: 全面支持 iOS、visionOS、tvOS 和 Mac Catalyst
- 丰富的消息类型: 支持应用内消息、内容卡片、推送通知等多种消息格式
- 无障碍访问: 新增 imageAltText 和 language 字段,提升 VoiceOver 支持
- 自动化推送: 提供完整的推送通知自动化配置和管理
- 地理位置服务: 集成 BrazeLocation 提供地理围栏和位置追踪功能
- GIF 支持: 通过 SDWebImage 集成提供丰富的媒体内容支持
- 实时分析: 完整的用户行为分析和事件追踪功能
安装指南
Swift Package Manager
在 Xcode 中通过 Swift Package Manager 添加依赖:
- 选择 File > Add Packages
- 输入仓库地址:
https://github.com/braze-inc/braze-swift-sdk - 选择版本规则并添加包
CocoaPods
在 Podfile 中添加:
pod 'BrazeKit'
pod 'BrazeUI' # 可选:UI组件
pod 'BrazeLocation' # 可选:地理位置服务
手动集成
运行安装脚本下载预构建框架:
cd your-project-directory
./manual-integration-setup.sh
使用说明
基础配置
在 AppDelegate 中初始化 Braze:
#import "AppDelegate.h"
@import BrazeKit;
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
BRZConfiguration *configuration =
[[BRZConfiguration alloc] initWithApiKey:@"YOUR_API_KEY"
endpoint:@"YOUR_ENDPOINT"];
configuration.logger.level = BRZLoggerLevelInfo;
Braze *braze = [[Braze alloc] initWithConfiguration:configuration];
AppDelegate.braze = braze;
return YES;
}
@end
用户识别与管理
#import "AuthenticationManager.h"
#import "AppDelegate.h"
@implementation AuthenticationManager
- (void)userDidLogin:(User *)user {
[AppDelegate.braze changeUser:user.identifier];
BRZUser *brazeUser = AppDelegate.braze.user;
[brazeUser setEmail:user.email];
[brazeUser setDateOfBirth:user.birthday];
[brazeUser setCustomAttributeWithKey:@"last_login_date"
dateValue:[NSDate date]];
}
@end
事件追踪与购买记录
#import "CheckoutViewController.h"
#import "AppDelegate.h"
@implementation CheckoutViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[AppDelegate.braze logCustomEvent:@"open_checkout_controller"
properties:@{
@"checkout_id": self.checkoutId,
@"product_ids": self.productIds
}];
}
- (void)userDidPurchaseProduct:(NSString *)productId {
double price = [self priceForProduct:productId];
[AppDelegate.braze logPurchase:productId
currency:@"USD"
price:price
properties:@{@"checkout_id": self.checkoutId}];
}
@end
核心代码
内容卡片管理
#import "AppDelegate.h"
@import BrazeKit;
@implementation AppDelegate
- (void)printCurrentContentCards {
NSLog(@"%@", AppDelegate.braze.contentCards.cards);
}
- (void)refreshContentCards {
[AppDelegate.braze.contentCards
requestRefreshWithCompletion:^(
NSArray<BRZContentCardRaw *> *_Nullable cards,
NSError *_Nullable error) {
NSLog(@"cards: %@", cards);
NSLog(@"error: %@", error);
}];
}
- (void)subscribeToContentCardsUpdates {
self.cardsSubscription = [AppDelegate.braze.contentCards
subscribeToUpdates:^(NSArray<BRZContentCardRaw *> *_Nonnull cards) {
NSLog(@"cards: %@", cards);
}];
}
@end
应用内消息处理
#import "CustomInAppMessagePresenter.h"
#import "AppDelegate.h"
#import "InAppMessageInfoViewController.h"
@implementation CustomInAppMessagePresenter
- (void)presentMessage:(BRZInAppMessageRaw *)message {
NSLog(@"extras: %@", message.extras);
NSLog(@"url: %@", message.url);
// 自定义消息展示逻辑
InAppMessageInfoViewController *viewController =
[[InAppMessageInfoViewController alloc] initWithMessage:message];
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:viewController];
[UIApplication.sharedApplication.delegate.window.rootViewController
presentViewController:navigationController
animated:YES
completion:nil];
}
@end
推送通知处理
#import "AppDelegate.h"
@import BrazeKit;
@import UserNotifications;
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Braze 配置
BRZConfiguration *configuration =
[[BRZConfiguration alloc] initWithApiKey:brazeApiKey
endpoint:brazeEndpoint];
configuration.push.automation.automaticSetup = YES;
configuration.push.automation.requestAuthorizationAtLaunch = YES;
Braze *braze = [[Braze alloc] initWithConfiguration:configuration];
AppDelegate.braze = braze;
// 推送通知订阅
self.notificationSubscription = [AppDelegate.braze.notifications
subscribeToUpdates:^(BRZNotificationsPayload *_Nonnull payload) {
NSLog(@"Push notification received: %@", payload.title);
}];
return YES;
}
@end
Braze Swift SDK 提供了完整的移动营销解决方案,从用户分析到消息推送,帮助开发者构建更加智能和互动的移动应用体验。