iOS高级功能

创建内容引用

let buo = BranchUniversalObject.init(canonicalIdentifier: "content/12345")
buo.title = "My Content Title"
buo.contentDescription = "My Content Description"
buo.imageUrl = "https://lorempixel.com/400/400"
buo.publiclyIndex = true
buo.locallyIndex = true
buo.contentMetadata.customMetadata["key1"] = "value1"
BranchUniversalObject *buo = [[BranchUniversalObject alloc] initWithCanonicalIdentifier:@"content/12345"];
buo.title = @"My Content Title";
buo.contentDescription = @"My Content Description";
buo.imageUrl = @"https://lorempixel.com/400/400";
buo.publiclyIndex = YES;
buo.locallyIndex = YES;
buo.contentMetadata.customMetadata[@"key1"] = @"value1";

创建链接引用

let lp: BranchLinkProperties = BranchLinkProperties()
lp.channel = "facebook"
lp.feature = "sharing"
lp.campaign = "content 123 launch"
lp.stage = "new user"
lp.tags = ["one", "two", "three"]

lp.addControlParam("$desktop_url", withValue: "http://example.com/desktop")
lp.addControlParam("$ios_url", withValue: "http://example.com/ios")
lp.addControlParam("$ipad_url", withValue: "http://example.com/ios")
lp.addControlParam("$android_url", withValue: "http://example.com/android")
lp.addControlParam("$match_duration", withValue: "2000")

lp.addControlParam("custom_data", withValue: "yes")
lp.addControlParam("look_at", withValue: "this")
lp.addControlParam("nav_to", withValue: "over here")
lp.addControlParam("random", withValue: UUID.init().uuidString)
BranchLinkProperties *lp = [[BranchLinkProperties alloc] init];
lp.feature = @"facebook";
lp.channel = @"sharing";
lp.campaign = @"content 123 launch";
lp.stage = @"new user";
lp.tags = @[@"one", @"two", @"three"];

[lp addControlParam:@"$desktop_url" withValue: @"http://example.com/desktop"];
[lp addControlParam:@"$ios_url" withValue: @"http://example.com/ios"];
[lp addControlParam:@"$ipad_url" withValue: @"http://example.com/ios"];
[lp addControlParam:@"$android_url" withValue: @"http://example.com/android"];
[lp addControlParam:@"$match_duration" withValue: @"2000"];

[lp addControlParam:@"custom_data" withValue: @"yes"];
[lp addControlParam:@"look_at" withValue: @"this"];
[lp addControlParam:@"nav_to" withValue: @"over here"];
[lp addControlParam:@"random" withValue: [[NSUUID UUID] UUIDString]];

创建深度链接

buo.getShortUrl(with: lp) { (url, error) in
 print(url ?? "")
[buo getShortUrlWithLinkProperties:lp andCallback:^(NSString* url, NSError* error) {
    if (!error) {
        NSLog(@"@", url);
    }
}];

分享深度链接

let message = "Check out this link"
buo.showShareSheet(with: lp, andShareText: message, from: self) { (activityType, completed) in
  print(activityType ?? "")
}
buo showShareSheetWithLinkProperties:lp andShareText:@"Super amazing thing I want to share!" fromViewController:self completion:^(NSString* activityType, BOOL completed) {
    NSLog(@"finished presenting");
}];

读取深度链接

  • 从深度链接中提取 Branch 数据

  • 最佳做法是从listener 接收数据(以防止出现race condition)

  • 返回深度链接属性

// listener (within AppDelegate didFinishLaunchingWithOptions)
Branch.getInstance().initSession(launchOptions: launchOptions) { params, error in
  print(params as? [String: AnyObject] ?? {})
}

// latest
let sessionParams = Branch.getInstance().getLatestReferringParams()

// first
let installParams = Branch.getInstance().getFirstReferringParams()
[[Branch getInstance] initSessionWithLaunchOptions:launchOptions
                        andRegisterDeepLinkHandler:^(NSDictionary * _Nullable params,
                                                     NSError * _Nullable error) {
    if (!error) {
        //Referring params
        NSLog(@"Referring link params %@",params);
    }
}];

// latest
NSDictionary *sessionParams = [[Branch getInstance] getLatestReferringParams];

// first
NSDictionary *installParams =  [[Branch getInstance] getFirstReferringParams];

导航至内容

  • 在此方法中处理 Branch.initSession()
// within AppDelegate application.didFinishLaunchingWithOptions
Branch.getInstance().initSession(launchOptions: launchOptions) { params , error in
  // Option 1: read deep link data
  guard let data = params as? [String: AnyObject] else { return }

  // Option 2: save deep link data to global model
  SomeCustomClass.sharedInstance.branchData = data

  // Option 3: display data
  let alert = UIAlertController(title: "Deep link data", message: "\(data)", preferredStyle: .alert)
  alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
  self.window?.rootViewController?.present(alert, animated: true, completion: nil)

  // Option 4: navigate to view controller
  guard let options = data["nav_to"] as? String else { return }
  switch options {
      case "landing_page": self.window?.rootViewController?.present( SecondViewController(), animated: true, completion: nil)
      case "tutorial": self.window?.rootViewController?.present( SecondViewController(), animated: true, completion: nil)
      case "content": self.window?.rootViewController?.present( SecondViewController(), animated: true, completion: nil)
      default: break
  }
}
// within AppDelegate application.didFinishLaunchingWithOptions
[[Branch getInstance] initSessionWithLaunchOptions:launchOptions andRegisterDeepLinkHandler:^(NSDictionary * _Nonnull params, NSError * _Nullable error) {
  // Option 1: read deep link data
  NSLog(@"%@", params);

  // Option 2: save deep link data to global model
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  [defaults setObject:params.description forKey:@"BranchData"];
  [defaults synchronize];

  // Option 3: display data
  UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Title" message:params.description preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *button = [UIAlertAction actionWithTitle:@"Deep Link Data" style:UIAlertActionStyleDefault handler:nil];
  [alert addAction:button];
  [self.window.rootViewController presentViewController:alert animated:YES completion:nil];

  // Option 4: navigate to view controller
  if ([params objectForKey:@"navHere"]) {
    ViewController *anotherViewController = [[ViewController alloc] initWithNibName:@"anotherViewController" bundle:nil];
    [self.window.rootViewController presentViewController:anotherViewController animated:YES completion:nil];
  }
}];

展示

buo.automaticallyListOnSpotlight = true
buo.automaticallyListOnSpotlight = YES;

跟踪内容

BranchEvent.standardEvent(.viewItem, withContentItem: buo).logEvent()
[[BranchEvent standardEvent:BranchStandardEventViewItem withContentItem:buo] logEvent];

跟踪用户

  • 设置事件、深度链接和推荐的用户标识(电子邮件、ID 和 UUID 等)

在initSession之后调用setIdentity

initSession 必须先被调用,否则initSession 回调将无法正确触发。

// login
Branch.getInstance().setIdentity("your_user_id")

// logout
Branch.getInstance().logout()
// login
[[Branch getInstance] setIdentity:@"your_user_id"];

// logout
[[Branch getInstance] logout];

跟踪事件

  • 与客户购买相关的所有事件都属于商业活动(Commerce)类事件

  • 与用户与应用程序中内容交互相关的所有事件都属内容(Content)类事件。

  • 与用户在应用程序中的进展相关的所有事件都属于生命周期(Lifecycle)类事件。

  • 要跟踪自定义事件(在下表中没有的),请参见跟踪自定义事件

  • 使用 Branch 控制面板进行验证

使用下表快速找到您要跟踪的事件。

活动名称 活动类别 iOS
Add To Cart 商业活动 BranchStandardEventAddToCart
Add To Wishlist 商业活动 BranchStandardEventAddToWishlist
View Cart 商业活动 BranchStandardEventViewCart
Initiate Purchase 商业活动 BranchStandardEventInitiatePurchase
Add Payment Info 商业活动 BranchStandardEventAddPaymentInfo
Purchase 商业活动 BranchStandardEventPurchase
Spend Credits 商业活动 BranchStandardEventSpendCredits
Search 内容事件 BranchStandardEventSearch
View Item 内容事件 BranchStandardEventViewItem
View Items 内容事件 BranchStandardEventViewItems
Rate 内容事件 BranchStandardEventRate
Share 内容事件 BranchStandardEventShare
Complete Registration 生命周期事件 BranchStandardEventCompleteRegistration
Complete Tutorial 生命周期事件 BranchStandardEventCompleteTutorial
Achieve Level 生命周期事件 BranchStandardEventAchieveLevel
Unlock Achievement 生命周期事件 BranchStandardEventUnlockAchievement

处理推送通知

  • 允许您在推送通知中跟踪Branch深度链接

  • 将Branch推送处理代码包括在Branch初始化代码中

  • 在您的推送通知中添加Branch深度链接 payload

    • 用您的深度链接替换https://example.app.link/u3fzDwyyjF
{
  "aps": {
    "alert": "Push notification with a Branch deep link",
    "badge": "1"
  },
  "branch": "https://example.app.link/u3fzDwyyjF"
}

处理您自己的应用程序中的深度链接

  • 允许您从应用内深度链接到应用本身
Branch.getInstance().handleDeepLink(withNewSession: URL(string: "https://example.app.link/u3fzDwyyjF"))
[[Branch getInstance] handleDeepLinkWithNewSession:[NSURL URLWithString:@"https://example.app.link/u3fzDwyyjF"]];

在您的应用中处理新的深层链接

在您的应用程序中处理新的深度链接接将清除当前会话数据,并将新引用的"设为" 。

追踪Apple搜索广告

  • 允许Branch跟踪Apple Search Ads深度链接分析

  • Apple API的分析数据生成速度很慢,这会影响到我们的分析数据。此外,Apple的API不会每次都向我们发送广告的所有数据,这有时会导致我们跟踪的广告只显示为通用的广告系列。

  • initSession 之前初始化Branch

Branch.getInstance().delayInitToCheckForSearchAds()
[[Branch getInstance] delayInitToCheckForSearchAds];

启用100%匹配

  • 使用SFSafariViewController 来增加归因匹配成功率

  • 100%匹配有点用词不当,因为与用户从Safari浏览器点击时相比,只有100%匹配。根据我们的分析,通过Safari浏览器点击的大约有50-75%,具体取决于用例。例如,从Facebook或Chrome浏览器点击不会触发100%的匹配。不过此技术仍然对匹配精度有利,所以我们建议采用它。

  • 使用自定义域时,在Info.plist中添加带有自定义域
    branch_app_domain 字符串键以启用100%匹配。

  • 默认情况下,如果应用'的依赖项中包含SafariServices.framework
    ,并且该应用使用app.link子域或在Info.plist中设置branch_app_domain
    ,则在iOS 9和10上启用基于cookie的匹配。可以通过调用SDK来禁用它。

  • initSession 之前初始化Branch

Branch.getInstance().disableCookieBasedMatching()
[[Branch getInstance] disableCookieBasedMatching];

启用/禁用用户跟踪

如果您出于GDPR的目的需要遵循用户的请求而不被跟踪,或者以其他方式确定不应当跟踪用户,请利用此字段来防止Branch发送网络请求。也可以在所有用户的特定链接或Branch链接中启用此设置。

Branch.setTrackingDisabled(true)
[Branch setTrackingDisabled:YES];

您可以选择在应用程序的整个生命周期中调用它。调用后,将不会从SDK发送网络请求。链接生成将继续起作用,但将不包含有关用户的标识信息。此外,深度链接接将继续起作用,但不会为用户跟踪分析。

共享到电子邮件选项

lp.addControlParam("$email_subject", withValue: "Your Awesome Deal")
lp.addControlParam("$email_html_header", withValue: "<style>your awesome CSS</style>\nOr Dear Friend,")
lp.addControlParam("$email_html_footer", withValue: "Thanks!")
lp.addControlParam("$email_html_link_text", withValue: "Tap here")
[lp addControlParam:@"$email_subject" withValue:@"This one weird trick."];
[lp addControlParam:@"$email_html_header" withValue:@"<style>your awesome CSS</style>\nOr Dear Friend,"];
[lp addControlParam:@"$email_html_footer" withValue:@"Thanks!"];
[lp addControlParam:@"$email_html_link_text" withValue:@"Tap here"];

动态分享讯息

// import delegate
class ViewController: UITableViewController, BranchShareLinkDelegate

func branchShareLinkWillShare(_ shareLink: BranchShareLink) {
  // choose shareSheet.activityType
  shareLink.shareText = "\(shareLink.linkProperties.channel)"
}
// import delegate
@interface ViewController () <BranchShareLinkDelegate>

  - (void) branchShareLinkWillShare:(BranchShareLink*)shareLink {
  // choose shareSheet.activityType
  shareLink.shareText = [NSString stringWithFormat:@"@%", shareLink.linkProperties.channel];
}

2个月前更新

iOS高级功能


建议的编辑仅限于API参考页

您只能建议对Markdown正文内容进行修改,而不能建议对API规范进行修改。