集成 FBNativeads 的正确方法是将FBNativeAdTableViewCellProvider.h类与[self.adsCellProvider adjustCount:self.objects.count forStride:adRowStep];
执行
1) .h 中的声明
@interface TableviewViewController : UITableViewController<FBNativeAdsManagerDelegate>{
int adRowStep;
}
@property (strong, nonatomic) FBNativeAd *nativeAd;
@property (strong, nonatomic) FBNativeAdsManager*adsManager;
@property (retain, nonatomic) FBNativeAdTableViewCellProvider*adsCellProvider;
2) forNumAdsRequested下的ViewDidLoad(你可以自定义你想要多少广告)
-(void)viewDidLoad{
self.adsManager = [[FBNativeAdsManager alloc]initWithPlacementID:@"PLACEMENT_ID" forNumAdsRequested:3];
self.adsManager.delegate = self;
[self.adsManager loadAds];
}
3) 从 FBNativeAdManager 加载的原生广告
-(void)nativeAdsLoaded{
self.adsCellProvider = [[FBNativeAdTableViewCellProvider alloc]initWithManager:self.adsManager forType:FBNativeAdViewTypeGenericHeight120];
self.adsCellProvider.delegate = self;
if (self.tableView != nil) {
[self.tableView reloadData];
}
}
- (void)nativeAd:(FBNativeAd *)nativeAd didFailWithError:(NSError *)error
{
NSLog(@"Native ad failed to load with error: %@", error);
}
- (void)nativeAdDidClick:(FBNativeAd *)nativeAd
{
NSLog(@"Native ad was clicked.");
}
- (void)nativeAdDidFinishHandlingClick:(FBNativeAd *)nativeAd
{
NSLog(@"Native ad did finish click handling.");
}
- (void)nativeAdWillLogImpression:(FBNativeAd *)nativeAd
{
NSLog(@"Native ad impression is being captured.");
}
4) UITableview 部分 + 行设置
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.adsCellProvider != nil) {
return [self.adsCellProvider adjustCount:self.objects.count forStride:adRowStep];
}
else{
return self.objects.count;
}
return 0;
}
5)按顺序返回不同的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell*cell;
if (self.adsCellProvider != nil && [self.adsCellProvider isAdCellAtIndexPath:indexPath forStride:adRowStep]) {
cell = [self.tableView dequeueReusableCellWithIdentifier:@"facebook_cell" forIndexPath:indexPath];
FBNativeAd*nativeAdx = self.adsManager.nextNativeAd;
if (nativeAdx) {
[nativeAdx unregisterView];
}
NSURL*image_url = nativeAdx.coverImage.url;
NSURL*image_icon_url = nativeAdx.icon.url;
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: image_url];
NSData* data2 = [[NSData alloc] initWithContentsOfURL:image_icon_url];
if ( data == nil && data2 == nil)
return;
dispatch_async(dispatch_get_main_queue(), ^{
// WARNING: is the cell still using the same data by this point??
cell.adCoverMediaView.image = [UIImage imageWithData:data];
cell.adIconImageView.image = [UIImage imageWithData:data2];
});
});
// Render native ads onto UIView
cell.adTitleLabel.text = nativeAdx.title;
cell.adBodyLabel.text = nativeAdx.body;
cell.adSocialContextLabel.text = nativeAdx.socialContext;
cell.sponsoredLabel.text = @"Sponsored";
[cell.adCallToActionButton setTitle:nativeAdx.callToAction
forState:UIControlStateNormal];
cell.adChoicesView.nativeAd = nativeAdx;
cell.adChoicesView.corner = UIRectCornerTopRight;
[nativeAdx registerViewForInteraction:cell withViewController:self];
return cell;
}
else{
long dif = indexPath.row - (indexPath.row / adRowStep);
NSDictionary*object = [self.objects objectAtIndex:dif];
//Do whatever you want with your object + create his own cell
// This is not a Facebook Ad Cell
}
}
6) DidSelectRowAtIndexPath --> 用这个模式回答你的问题
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.adsCellProvider != nil && [self.adsCellProvider isAdCellAtIndexPath:indexPath forStride:adRowStep]) {
}
else{
long dif = indexPath.row - (indexPath.row / adRowStep);
PFObject*sender_obx = [self.objects objectAtIndex:dif];
PostDetailsViewController*rootViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"post_detail_VC"];
rootViewController.post_obx = sender_obx;
rootViewController.post_id = sender_obx.objectId;
[self.navigationController pushViewController:rootViewController animated:YES];
}
}
我就是这样做的。我希望它会帮助你