首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Braze Swift SDK 全面指南 - 构建卓越的移动应用体验

Braze Swift SDK 全面指南 - 构建卓越的移动应用体验

原创
作者头像
qife122
发布2025-09-14 21:54:39
发布2025-09-14 21:54:39
1250
举报

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 添加依赖:

  1. 选择 File > Add Packages
  2. 输入仓库地址: https://github.com/braze-inc/braze-swift-sdk
  3. 选择版本规则并添加包

CocoaPods

在 Podfile 中添加:

代码语言:ruby
复制
pod 'BrazeKit'
pod 'BrazeUI'        # 可选:UI组件
pod 'BrazeLocation'  # 可选:地理位置服务

手动集成

运行安装脚本下载预构建框架:

代码语言:bash
复制
cd your-project-directory
./manual-integration-setup.sh

使用说明

基础配置

在 AppDelegate 中初始化 Braze:

代码语言:objective-c
复制
#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

用户识别与管理

代码语言:objective-c
复制
#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

事件追踪与购买记录

代码语言:objective-c
复制
#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

核心代码

内容卡片管理

代码语言:objective-c
复制
#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

应用内消息处理

代码语言:objective-c
复制
#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

推送通知处理

代码语言:objective-c
复制
#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 提供了完整的移动营销解决方案,从用户分析到消息推送,帮助开发者构建更加智能和互动的移动应用体验。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Braze Swift SDK
    • 功能特性
    • 安装指南
      • Swift Package Manager
      • CocoaPods
      • 手动集成
    • 使用说明
      • 基础配置
      • 用户识别与管理
      • 事件追踪与购买记录
    • 核心代码
      • 内容卡片管理
      • 应用内消息处理
      • 推送通知处理
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档