Bazaarvoice移动sdk提供了对Bazaarvoice提供的大多数核心产品的访问。必威手机版本要了解更多信息,请参阅Mobile SDK文档主页

内容

+显示 ——隐藏

本文档解释了如何检索和显示产品推荐。

简介

产品推荐模块目前处于有限beta版。如果你想参加,联系我们

使用以下指南在您的iOS应用程序中使用Bazaarvoice Mobile sdk获取和显示产品推荐。说明包括Swift和Objective-C实现,以显示产品推荐UICollectonView.如果您熟悉实现的设计模式UICollectionViewUITableView在iOS中,产品推荐模块的实现应该很简单。

Swift和Objective-C代码演练的完整项目在SDK路径中/ / ProductRecommendations例子.有关对话API的更全面的演示,请参见下面的BVSDKDemo项目/ / BVSDKDemo例子

开始实现此Mobile SDK模块之前,请检查上的步骤来验证您的安装安装页面。

设置用户信息

在用户登录工作流中对用户进行身份验证后,在BVSDKManager # sharedManager ()对象。

您必须将使用现有Bazaarvoice实现逻辑创建的userAuthString提供给BVSDKManager对象。但是,即使您不是会话客户端,加密userAuthString的步骤也适用。

用于测试的认证字符串示例

如果尚未设置授权令牌,请使用用户认证令牌的例子。

显示产品推荐

产品推荐仅仅是呈现给用户的视图。使用您在故事板中已经使用的类来显示建议。你可以设置它们的格式来匹配你的应用程序的视觉设计。当一组产品推荐加载时,你的应用程序会收到一个数组BVRecommendedProduct对象,然后应用程序(而不是SDK)负责显示每个对象BVRecommendedProduct对象。

内置的观点

Bazaarvoice Mobile SDK提供了以下视图容器来显示多个产品推荐:

  • BVProductRecommendationsCollectionView
  • BVProductRecommendationsTableView
  • BVProductRecommendationsContainer

使用上面的类作为容器的基本视图,在其中显示产品推荐。Mobile SDK还提供了以下视图对象来显示单个产品推荐:

  • BVRecommendationCollectionViewCell
  • BVRecommendationTableViewCell
  • BVProductRecommendationView

使用上面的类作为显示产品推荐的视图的基类。

示例使用

下面的示例显示了BVProductRecommendationsCollectionView的实现。按照下面的例子生成一个可滚动的产品推荐视图:

的子类BVRecommendationCollectionViewCell

的子类,完成以下步骤BVRecommendationCollectionViewCell被称为DemoCell

  1. 创建一个UICollectionViewCell通过选择File >新建>文件

  2. 使用下面的代码将其超类设置为BVRecommendationCollectionViewCellDemoCell文件,并添加适当的视图出口。

    本例中的starRating对象是通过开源项目HCSStarRatingView完成的。
    // DemoCell.swift导入BVSDK导入HCSStarRatingView类DemoCell: bvrecommend collectionviewcell {@IBOutlet weak var productName: UILabel!@IBOutlet弱var价格:UILabel!@IBOutlet弱var numReview: UILabel!@IBOutlet弱var评级:UILabel!@IBOutlet弱var productImageView: UIImageView!@IBOutlet弱var starRating: HCSStarRatingView!重写var bvRecommendedProduct: bvrecommend product !{didSet {let imageUrl = NSURL(string: bvRecommendedProduct. imageUrl) self.productName.text = bvRecommendedProduct。productName self.rating.text = "\(bvrecommend product . "averageRating ? ?0)" self.numReview.text = "(\(bvrecommend product。numReviews ? ?0) reviews)" self.starRating.value = (CGFloat)(bvrecommend product . averagerating . floatvalue) self.productImageView?.sd_setImageWithURL(imageUrl)}}}
    / / DemoCell。m // #import "DemoCell.h" #import  @interface DemoCell () @property IBOutlet UILabel *productName;@property IBOutlet UILabel *价格;@property IBOutlet UILabel *numReview;@property IBOutlet UILabel *评级;@ IBOutlet UIImageView *productImageView;@property IBOutlet HCSStarRatingView;DemoCell - (void) setbvrecommend product:(bvrecommend product *) bvrecommend product{超级。bvrecommend product = bvrecommend product;NSURL *url = [NSURL URLWithString:self. bvrecommend product . imageurl];(自我。productImageView sd_setImageWithURL:url]; self.productName.text = bvRecommendedProduct.productName; self.rating.text = [NSString stringWithFormat:@"%.1f", [bvRecommendedProduct.averageRating floatValue]]; self.starRating.value = [bvRecommendedProduct.averageRating floatValue]; self.numReview.text = [NSString stringWithFormat:@"(%ld)", [bvRecommendedProduct.numReviews longValue]]; self.price.text = bvRecommendedProduct.price; } @end
    上面的DemoCell outlet是构建自定义单元的建议。出口有效地映射到每个bvrecommend product数据对象。但是,输出并不是必需的,您可以按照自己想要的方式配置单元格。
  3. DemoCell.xib,添加和定位您的视图,并连接您的出口,如下面的截图所示。

创建一个BVProductRecommendationsCollectionView

接下来,完成以下步骤以创建一个BVProductRecommendationsCollectionView.在本例中,您将创建一个ui然后放一个BVProductRecommendationsCollectionView在里面。

  1. 创建一个ui通过选择File >新建>文件

  2. 为集合视图添加一个outlet:

    类DemoViewController: UIViewController {@IBOutlet弱var推荐视图:BVProductRecommendationsCollectionView!}
    // DemoViewController.h #import  #import  @interface DemoViewController: UIViewController @property IBOutlet BVProductRecommendationsCollectionView * recommended sview;@end
  3. 添加一个UICollectionView到您的nib,并将其类设置为BVProductRecommendationsCollectionView

  4. 连接BVProductRecommendationsCollectionView到它的出口,recommendationsView

获取产品推荐

接下来,完成以下步骤以获取用户的产品推荐。

  1. 使用下面的代码获取产品推荐并将它们存储在一个变量中以供以后使用。您还将设置您的recommendationsView委托和数据源,并注册前面创建的nib。

    class DemoViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {@IBOutlet weak var recommendations sview: BVProductRecommendationsCollectionView!var recommendations: [bvrecommend product] = [] override func viewDidLoad() {super.viewDidLoad() recommendations。registerNib(UINib(nibName: "DemoCell", bundle: nil), forCellWithReuseIdentifier: "DemoCellIdentifier") self.建议sview .delegate = self self.建议sview . datasource = self let request = bv建议srequest (limit: 20) self.建议sview。loadRequest(request, completionHandler: {(recommendations:[bvrecommendations product]))在self中。建议=建议self.recommendations sview . reloaddata()}){(错误:NSError)在//处理错误情况}}}
    / / DemoViewController。m# import "DemoViewController.h" #import "DemoCell.h" @interface DemoViewController ()  @property NSArray< bvrecommedproduct *>* pr必威手机版本oducts;@end @implementation DemoViewController - (void)viewDidLoad {[super viewDidLoad];自我。必威手机版本products = [NSArray数组];UINib* nib = [UINib nibWithNibName:@"DemoCell" bundle:nil];(自我。建议查看registerNib:nib forCellWithReuseIdentifier:@"DemoCellIdentifier"];self.recommendations . view .delegate = self;self.recommendations . view . datasource = self; [self loadRecommendations]; } // Fetch the product recommenations from the API method on the BVRecommendationsCollectionView container. - (void)loadRecommendations { BVRecommendationsRequest *request = [[BVRecommendationsRequest alloc] initWithLimit:20]; [self.recommendationsView loadRequest:request completionHandler:^(NSArray * proudcts) { NSLog(@"Recommendations Result: %@", proudcts.description); self.products = proudcts; [self.recommendationsView reloadData]; } errorHandler:^(NSError * error) { NSLog(@"ERROR: %@", error.localizedDescription); }]; } // Lays out the recommendationView with horitontal scrolling - (void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.recommendationsView.collectionViewLayout; layout.itemSize = CGSizeMake(self.recommendationsView.bounds.size.height, self.recommendationsView.bounds.size.height); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.itemSize = CGSizeMake(self.recommendationsView.bounds.size.width / 2, self.recommendationsView.bounds.size.width / 2); layout.minimumInteritemSpacing = 0; layout.minimumLineSpacing = 0; } @end
  2. 创建并完成collectionView: numberOfItemsInSection

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{返回self. recommended .count ??0}
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{返回self.produc必威手机版本ts.count;}
  3. 创建并完成collectionView: cellForItemAtIndexPath

    本演示中的图像加载使用SDWebImage
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {let cell = collectionView. dequeuereusablecellwithreuseidentifier ("DemoCellIdentifier", forIndexPath: indexPath) as!DemoCell let product = recommendations[indexPath.]行细胞。bvProduct =产品返回单元格;}
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{DemoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DemoCellIdentifier" forIndexPath:indexPath];细胞。bvrecommend product = [self.]必威手机版本产品,objectAtIndex indexpath。row):;NSURL *url = [NSURL URLWithString:cell. bvrecommend product . imageurl];(细胞。productImageView sd_setImageWithURL: url];//初始化其他输出返回单元;}
让profileQuery = BVRecommendationsProfileQuery() profileQuery .configure(configuration) .field(.brandId("brandId")) .field(.include(.interests)) .field(.include(.categories)) .field(.include(.brands)) .field(.include(.recommendations)) .handler{在if case中响应let.failure(errors) =响应{//错误返回}保护case let。success(meta, result) = response else {return} // success: result is type [bvrecommationsprofile]} profilquery .async()

运行应用程序

应用程序应该用产品推荐填充集合视图。虽然应用程序应该可以正常运行,但它仍然需要样式化以看起来专业。