Braze Swift SDK 是一个全面的移动营销解决方案,为开发者提供了强大的用户互动和分析工具。该SDK支持多种消息格式和用户互动方式,帮助开发者构建卓越的移动应用体验。
在 Xcode 中通过 Swift Package Manager 添加依赖:
https://github.com/braze-inc/braze-swift-sdk
在 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 提供了完整的移动营销解决方案,从用户分析到消息推送,帮助开发者构建更加智能和互动的移动应用体验。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。