热点搜索

概述

使用Branch轻松在Apple '的新Spotlight搜索中列出您的应用程序内容。请注意,本指南将同时列出云搜索本地聚光灯搜索

索引内容

一些较旧的设备无法索引内容。例如,iPad mini不能使用CoreSpotlight。 SDK包含对这些设备的检查,如果您尝试对这些设备上的内容编制索引,则会返回错误消息。

设置

先决条件

本指南要求您已经将将Branch SDK 集成到您的应用中。为了使Spotlight搜索结果按预期运行,您还应该配置深度链接接路由

清单内容

可以使用BranchUniversalObject将内容添加到Spotlight搜索中。我们' ð建议你把这个每一个呈现为用户一块内容页面上。这样,用户可以重新发现他们之前查看过的内容。

首先,用BranchUniversalObject来描述' d想要列出的内容。我们' d建议您在viewDidLoad尽早进行。

然后在BranchUniversalObject上调用registerView 方法。您将希望每次用户查看应用程序中的页面时都执行此操作,因此我们建议将其置于viewDidAppear ,这意味着您必须在viewDidAppear之前使用所有适当的元数据初始化Branch Universal Object。

  • Swift 3

      // Describe your content with a BranchUniversalObject:
      let branchUniversalObject: BranchUniversalObject = BranchUniversalObject(canonicalIdentifier: "item/12345")
      branchUniversalObject.title = "My Content Title"
      branchUniversalObject.contentDescription = "My Content Description"
      branchUniversalObject.imageUrl = "https://example.com/mycontent-12345.png"
      branchUniversalObject.addMetadataKey("property1", value: "blue")
      branchUniversalObject.addMetadataKey("property2", value: "red")
    
      // Set this to true if you'd like the content to be publicly searchable through Google and Apple search:
      branchUniversalObject.publiclyIndex = true
    
      // Important! Set this flag to true for local spotlight indexing:
      branchUniversalObject.locallyIndex = true
    
      ...
    
      // Next, register the content view, perhaps in your `viewDidAppear` method:
      branchUniversalObject.registerView()
    
  • Objective C

      // Describe your content with a BranchUniversalObject:
      BranchUniversalObject *branchUniversalObject = [[BranchUniversalObject alloc] initWithCanonicalIdentifier:@"item/12345"];
      branchUniversalObject.title = @"My Content Title";
      branchUniversalObject.contentDescription = @"My Content Description";
      branchUniversalObject.imageUrl = @"https://example.com/mycontent-12345.png";
      [branchUniversalObject addMetadataKey:@"property1" value:@"blue"];
      [branchUniversalObject addMetadataKey:@"property2" value:@"red"];
    
      // Set this to true if you'd like the content to be publicly searchable through Google and Apple search:
      branchUniversalObject.publiclyIndex = YES;
    
      // Important! Set this flag to true for local spotlight indexing:
      branchUniversalObject.locallyIndex = YES;
    
      ...
    
      // Next, register the content view, perhaps in your `viewDidAppear` method:
        [branchUniversalObject registerView];
    

这将创建适当的NSUserActivity并告诉Apple已发生视图,并将其添加到本地Spotlight索引(如果尚不存在),以及将其在全局索引中的排名提高' 。要了解更多信息,请查看此博客文章

处理来自Spotlight的传入流量

打开AppDelegate.m 文件并添加以下方法。如果您完成了基本的SDK集成指南,则可能已经存在。

  • Swift 3

      func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
          // pass the url to the handle deep link call
          return Branch.getInstance().continueUserActivity(userActivity)
    
          return true
      }
    
  • Objective C

      - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
          [[Branch getInstance] continueUserActivity:userActivity];
    
          return YES;
      }
    

高级

使用深度视图进行用户获取

如果用户没有按' T的程序安装后,通过搜索找到你的内容,射灯将打开浏览器。在这种情况下,您可以显示Deepview ,它是应用程序内容的自动生成的移动Web渲染。

定制内容

  • 编制索引时,可以使用我们的标识符对列出的内容执行高级自定义。

    • Swift 3

       branch.getSpotlightUrl(withParams: ["$og_title": "My App",
                                           "$og_description": "My app is disrupting apps",
                                           "$og_thumb": "https://s3-us-west-1.amazonaws.com/branchhost/mosaic_og.png",
                                           "object_id": "1234"],
                                callback: { (params, error) in
      
               if let params = params {
                       // params will contain @"url" and @"spotlight_identifier"
                       // the example below shows where to use them
      
                       // Index via the NSUserActivity strategy
                       // you must set the new NSUserActivity to the property of a UIViewController
                       self.userActivity = NSUserActivity(activityType: params["spotlight_identifer"] as! String)
                       self.userActivity.webpageURL = URL(string: params["url"] as! String)
                       self.userActivity.becomeCurrent()
               }
       })
      
      • Objective-C

        [branch getSpotlightUrlWithParams:@{@"$og_title": @"My App",
                                               @"$og_description": @"My app is disrupting apps",
                                               @"$og_thumb": @"https://s3-us-west-1.amazonaws.com/branchhost/mosaic_og.png",
                                               @"object_id": @"1234"}
                                    callback:^(NSDictionary *params, NSError *error) {
               if (!error && params) {
                   // params will contain @"url" and @"spotlight_identifier"
                   // the example below shows where to use them
        
                   // Index via the NSUserActivity strategy
                   // you must set the new NSUserActivity to the property of a UIViewController
                   self.userActivity = [[NSUserActivity alloc] initWithActivityType:params[@"spotlight_identifer"]];
                   self.userActivity.webpageURL = [NSURL URLWithString:params[@"url"]];
                   [self.userActivity becomeCurrent];
               }
        }];
        

规模索引内容

如果目标是仅对应用程序的内容建立索引而不创建BranchUniversalObject 或者您希望按比例编制索引内容,则建议使用以下方法:

  • Swift 3

      let set:BranchCSSearchableItemAttributeSet = BranchCSSearchableItemAttributeSet()
      set.title = "My Content Title"
      set.contentDescription = "My Content Description"
      set.params = ["property1" : "blue", "property2" : "red"]
      set.keywords = ["array", "of", "keywords"]
      set.thumbnailURL = URL(string: "https://example.com/mycontent-12345.png")
      set.index { (url, spotlightIdentifier, error) in
          print("url \(String(describing: url)), spotlightIdentifier \(String(describing: spotlightIdentifier)), error \(error.debugDescription)")
      }
    
  • Objective-C

      BranchCSSearchableItemAttributeSet *set = [[BranchCSSearchableItemAttributeSet alloc] init];
      set.title = @"My Content Title";
      set.contentDescription = @"My Content Description";
      set.params = @{@"property1" : @"blue", @"property2" : @"red"};
      set.keywords = [NSSet setWithArray:@["array", "of", "keywords"]];
      set.thumbnailURL = @"https://example.com/mycontent-12345.png";
      [set indexWithCallback:^(NSString * _Nullable url, NSString * _Nullable spotlightIdentifier, NSError * _Nullable error) {
          NSLog(@"url %@, spotlightIdentifier %@, error %@ ", url, spotlightIdentifier, error);
      }];
    

在Spotlight上使用Branch Universal Object索引内容

  • 索引单个Branch通用对象

    • Swift 3

        universalObject.listOnSpotlight(with: linkProperty) { (url, error) in
            if (!error) {
                print("Successfully indexed on spotlight")
             }
        }
      
    • Objective-C

        [universalObject listOnSpotlightWithLinkProperties:linkProperties callback:^(NSString * _Nullable url, NSError * _Nullable error) {
            if (!error) {
                 NSLog(@"Successfully indexed on spotlight");
            }
        }];
      

内容索引

如果BranchUniversalObject的contentIndexMode 为Public,则将使用NSUserActivity 内容建立索引;否则将使用CSSearchableIndex对其进行索引。

  • 使用CSSearchableIndex在Spotlight上对Branch通用对象进行索引批处理

    • Swift 3

        Branch.getInstance().indexOnSpotlight(usingSearchableItems: universalObjects,
                                                completion: { (universalObjects, error) in
             if (!error) {
                // Successfully able to index all the BUO on spotloght
            }
        })
      
    • Objective-C

        [[Branch getInstance] indexOnSpotlightUsingSearchableItems:universalObjects
                                                    completion:^(NSArray<BranchUniversalObject *> *universalObjects,
                                                                 NSError *error) {
                if (!error) {
                   // Successfully able to index all the BUO on spotloght
                }
        }];
      

BUO索引

所有分支通用对象都将使用CSSearchableIndex 进行索引,而不管它是' s contentIndexMode

  • 如果已私有索引,则从Spotlight中删除单个Branch通用对象

    • Swift 3

        universalObject.removeFromSpotlight { (error) in
            if(!error) {
                print("BUO successfully removed")
            }
        }
      
    • Objective-C

        [universalObject removeFromSpotlightWithCallback:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"universal Object removed from spotlight");
            }
        }];
      
  • 如果已私有索引,则从Spotlight中删除一批Branch通用对象

    • Swift 3

        Branch.getInstance().removeSearchableItems(with: [BUO1,BUO2]) { (error) in
            if (!error) {
                print("An array of BUOs removed from spotlight")
            }
        }
      
    • Objective-C

        [[Branch getInstance] removeSearchableItemsWithBranchUniversalObjects:@[BUO1,BUO2] callback:^(NSError *error) {
            if (!error) {
                NSLog(@"An array of BUOs removed from spotlight");
            }
        }];
      

spotlightIdentifier

spotlightIdentifier Branch Universal Object的成员变量应包含Spotlight标识符,该标识符用于索引内容以成功删除索引。

  • 如果使用Branch SDK进行了私有索引,则从Spotlight中删除所有内容

    • Swift 3

        Branch.getInstance().removeAllPrivateContentFromSpotLight { (error) in
            if (!error) {
                print("All branch privately indexed content removed from spotlight")
            }
        }
      
    • Objective-C

        [[Branch getInstance] removeAllPrivateContentFromSpotLightWithCallback:^(NSError *error) {
            if (!error) {
                NSLog(@"All branch privately indexed content removed from spotlight");
            }
        }];
      

4个月前更新

热点搜索


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

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