原生广告与你的app内容无缝结合,在不影响用户体验的前提下提升用户参与度。你可以选择或自定义匹配app设计的布局。理想情况下,原生广告应用于新闻、工具和通信app。
按照以下这些简易的步骤开始利用原生广告盈利:
在添加app后,选择 NATIVE CONTENT,创建一个放置,将广告类型设置为NATIVE(原生)。
从选择项中选择合适的布局:
创建原生广告放置之后,将生成放置id。
在你的 ViewController.h
文件中导入框架,并声明一个native(原生)实例。 你的 ViewController
文件应该如下:
#import <UIKit/UIKit.h>
@import InMobiSDK;
@interface ViewController : UIViewController <IMNativeDelegate>
@property (nonatomic, strong) IMNative* nativeAd;
@property (nonatomic, strong) NSString* nativeContent;
@end
实例化原生对象。你的ViewController.m
文件应该如下:
- (void)viewDidLoad {
[super viewDidLoad];
self.nativeAd = [[IMNative alloc] initWithPlacementId:<Insert native Placement ID Here>];
self.nativeAd.delegate = self;
[self.nativeAd load];
}
将 UIViewController
dealloc
函数中的委托值设为零,或是其他任何发布横幅的时间。
-(void)dealloc {
nativeAd.delegate = nil;
}
广告状态回调时,执行 IMNative
的委托性能。支持以下回调函数:
/*The native ad notifies its delegate that it is ready. Fetching publisher-specific ad asset content from native.adContent. The publisher will specify the format. If the publisher does not provide a format, no ad will be loaded.*/
-(void)nativeDidFinishLoading:(IMNative*)native{
NSLog(@"Native Ad loaded %@", native.adContent);
}
/*The native ad notifies its delegate that an error has been encountered while trying to load the ad.Check IMRequestStatus.h for all possible errors.Try loading the ad again, later.*/
-(void)native:(IMNative*)native didFailToLoadWithError:(IMRequestStatus*)error{
NSLog(@"Native Ad load Failed");
}
/* Indicates that the native ad is going to present a screen. */ -(void)nativeWillPresentScreen:(IMNative*)native{
NSLog(@"Native Ad will present screen");
}
/* Indicates that the native ad has presented a screen. */
-(void)nativeDidPresentScreen:(IMNative*)native{
NSLog(@"Native Ad did present screen");
}
/* Indicates that the native ad is going to dismiss the presented screen. */
-(void)nativeWillDismissScreen:(IMNative*)native{
NSLog(@"Native Ad will dismiss screen");
}
/* Indicates that the native ad has dismissed the presented screen. */
-(void)nativeDidDismissScreen:(IMNative*)native{
NSLog(@"Native Ad did dismiss screen");
}
/* Indicates that the user will leave the app. */
-(void)userWillLeaveApplicationFromNative:(IMNative*)native{
NSLog(@"User leave");
}
当你得到回调 nativeAdDidFinishLoading
时,你已经成功接收原生广告。 self.nativeAd.adContent
对象提供原生广告内容,并可在任何新建或已有的UIView中显示。原生广告内容中有一组资源是由你在InMobi门户站点选择的格式决定的。
例如,对于新闻视图流,内容往往包含以下资源:
如下所示,这些资源可用于视图里的诸多组件中:
if (self.adContent!=nil) {
NSData* data = [self.adContent dataUsingEncoding:NSUTF8StringEncoding];
NSError* error = nil;
NSDictionary* jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableDictionary* nativeJsonDict = [NSMutableDictionary dictionaryWithDictionary:jsonDict];
/*The above lines of code convert the native JSON content to NSDictionary.*/
if (error == nil && nativeJsonDict != nil) {
// The native JSON is parsed and can be used.
[nativeJsonDict setValue:[NSNumber numberWithBool:YES] forKey:@"isAd"];
// To differentiate the native JSON dictionary from other data.
/* Examples of fetching the image and title are listed below*/
/*Fetch the image url from the dictionary in the following way.*/
NSDictionary* imageDict = [nativeJsonDict valueForKey:@"icon"];
NSString* url = [imageDict valueForKey:@"url"];
/*Fetch the tile from the dictionary*/
NSString* title = [imageDict valueForKey:@"title"];
}
}
用这些值链接必要的视图。例如,如果你想创建一个表格视图,且广告位于其中一格内,那么在 tableView:cellForRowAtIndexPath:
函数中渲染视图。例如:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* cellIdentifier = @"someIdentifier";
UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
// If this is the row where the ad should be displayed,
if([dict valueForKey:@"isAd"]) {
UILabel* titleLabel = [[UILabel alloc] initWithFrame:<FRAME_FOR_YOUR_LABEL>];
title.text = title; //This title is from the native JSON dictionary
UIImage* image = [UIImage imageWithData:<DATA_FROM_THE_IMAGE_URL>];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:<FRAME_FOR_YOUR_IMAGE>];
imageView.image = image;
//Add subviews to the cell
[cell addSubView:titleLabel];
[cell addSubView:imageView];
//Bind the cell to the native ad as it has rendered the ad.
[IMNative bindNative:self.nativeView toView:cell];
}
else {
// This is a content item
// UnBind the cell as it might be a reused cell from a native ad.
[IMNative unBindView:cell];
}
} Copy 下面是一个访问原生广告内容并为信息流对其进行渲染的例子。根据选择的视图不同,内容资源可能会与其他流相异。
渲染广告内容后,调用以下函数报告
bindNative:(IMNative*)native toView:(UIView*) method on the IMNative class
[IMNative bindNative:nativeAd toView:renderedUIViewInstance];
Here, 在这里, 'renderedUIViewInstance'
'指代渲染后的原生广告。也就是说,对于表格视图,如果你正在渲染其中一格内的原生广告,只要你渲染这个广告,就必须把这一格传递给该函数。
IMNative
类的 unBindView:(UIView*)view
函数。如果广告还未加载,那么这两个函数都将调用失败。
调用 reportAdClick:(NSDictionary*)
参数向原生广告实例报告点击。
参数词典可以包含发行者想向原生实例提供的任何附加信息。
若想自己报告点击并操控打开登陆页面, 调用 ;[nativeAd reportAdClick:paramsDictionary];
reportAdClick
函数后你必须操控该原生广告的登陆页面。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary* dict = [self.items objectAtIndex:[indexPath row]];
url = [dict valueForKey:@"link"];
if ([dict valueForKey:@"isAd"]) {
NSString* landingUrl = [dict valueForKey:@"landing_url"];
[self.native reportAdClick:nil];
if (landingUrl != nil){
NSURL* landingPageURL = [NSURL URLWithString:landingUrl];
[[UIApplication sharedApplication] openURL:landingPageURL];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
报告点击并允许InMobi打开登陆页面,调用 ;[nativeAd reportAdClickAndOpenLandingURL:paramsDictionary];
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary* dict = [self.items objectAtIndex:[indexPath row]];
if ([dict valueForKey:@"isAd"]) {
[self.native reportAdClickAndOpenLandingURL:nil];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
选择工具(Tools)-诊断(Diagnostics),将测试模式设为 Global ON或者Selective ON。
如果你是第一次集成某个广告单元 | 将测试模式 ( Test Mode)设为 Global ON。 |
如果你希望有选择性地为某组设备打开测试流量 你之前已经为某广告单元集成过SDK,因此你应该把测试局限于几个设备 |
Set 将测试模式( Test Mode)设为Selective ON。
设备部分:
如果你已经配置过某个设备,你可以选择这个设备,测试模式将激活。 |
注意: 在启动前你必须关闭测试模式,否则你将无法利用你的app盈利。
现在你已经准备好测试广告了。
获取设备ID
设备id就是IDFA或者IFA(广告标识符)。获取设备ID最简单的函数就是将SDK的日志级别设为“调试(debug)”。为激活这些调试日志,在 AppDelegate.m
文件的didFinishLaunchingWithOptions
函数中添加以下内容:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Initialize InMobi SDK with your account ID
[IMSdk initWithAccountID:@"Insert InMobi account ID here"];
//Set log level to Debug
[IMSdk setLogLevel:kIMSDKLogLevelDebug];
// Do your stuff.
return YES;
}
处于调试模式时,在Xcode开发者控制台登陆的设备ID为: “Publisher device ID is <device-id>”
。
你可以在 Device ID对话框中输入该设备id。
[InMobi] InMobi SDK初始化账户id: <这里是你的账户id>
[InMobi] 读取原生广告获知放置id: <这里是你的放置id>
[InMobi] 发行者设备id是 <这里是设备id>
[InMobi] 成功读取原生广告获知放置id: <这里是你的放置id >
** 错误 ** [InMobi] ___ 请在创建
** 错误 ** [InMobi] ___ 账户id不能为零或空值。请提供有效的账户id。
[InMobi] 无效的账户id无法初始化。请提供有效的账户id。
下表中为全套关键日志。
初始化SDK
情境 | 日志级别 | 日志 |
发行者传递零值或空值账户id | 错误 | 账户id不能为零或空值。请提供有效的账户id。 |
发行者传递有效的账户id | 调试 | InMobi SDK初始化账户id: <账户id> |
发行者传递无效的账户id | 错误 | 无效的账户id无法初始化。请提供有效的账户id。 |
有更新版本的SDK可使用 | 调试 | 有新版本 (ver. 6.0.0) 的InMobi SDK可使用!你现在使用的为旧版(ver. 5.3.1)。从 http://www.inmobi.com/products/sdk/#downloads下载最新版本的InMobi SDK。 |
普通广告
情境 | 日志级别 | 日志 |
无网络连接时发送广告请求 | 错误 | 此时网络连接不可以。请重试。 |
广告处于加载中或广告可看时发送广告请求 | 错误 | 广告正在加载,请等待加载完成再请求下一个广告(放置id : <placement id="" /> )。 |
用户浏览时发送广告请求 | 错误 | 用户正在浏览广告,请等待用户关闭再请求下一个广告(放置id : <placement id="" /> )。 |
发行者设备Id | 调试 | 发行者设备id是<Device Id="" /> |
原生广告
情境 | 日志级别 | 日志 |
发行者在未初始化SDK时创建原生广告 | 错误 | 请在创建原生广告前初始化SDK |
发行者使用无效的放置id创建原生广告 | 错误 | 请提供有效的放置id以创建原生广告 |
发行者访问加载某原生广告 | 调试 | 读取原生广告获知放置id: <placement id="" /> |
成功读取广告 | 调试 | 成功读取原生广告获知放置id: <placement id="" /> |
无法读取广告 | 错误 | 无法读取原生广告获知放置id: <placement id="" /> 出错: <status code="" /> |
发行者将原生广告与视图绑定 | 调试 | 将原生广告绑定到视图: <view info="" /> 获知放置id: <placement id="" /> |
发行者取消原生广告与视图绑定 | 调试 | 将原生广告与视图解绑: <view info="" /> 获知放置id: <placement id="" /> |
开始加载环境代码 | 调试 | 开始在web视图中加载原生广告标记获知放置id: <placement id="" /> |
加载环境代码 | 调试 | 成功在web视图中加载原生广告标记获知放置id: <placement id="" /> |
记录可视效果 | 调试 | 以放置id: <placement id="" /> 记录原生广告的可视效果 |
绑定旧日志空视图 用空视图调用 prepareToTrackImpressions 函数 |
错误 | 请传递非空视图对象绑定原生广告 |
已绑定视图 用已绑定视图调用 prepareToTrackImpressions 函数 |
错误 | 无法绑定视图 |
在接收广告前绑定视图 在还未从服务器接收到广告时调用 prepareToTrackImpressions 函数 |
错误 | 请等待广告加载完毕再调用 prepareToTrackImpressions 函数 |
无效广告状态下 reportAdClick 在空视图或无效视图中调用reportAdClick 函数 |
错误 | 错误状态下调用 reportAdClick |
报告定制效果跟踪器或点击,采用 IMCustomNative
类。
@import InMobiSDK;
传递 renderedUIViewInstance
和定制效果追踪器脚本作为参数。
+(void)bindNative:toView:withImpressionTrackerScript:
传递 renderedUIViewInstance
和定制效果追踪器url作为参数。
+(void)bindNative:toView:withImpressionTrackerURL:
传递点击参数和定制点击追踪脚本作为参数。
-(void)reportAdClick: withCustomClickTrackerScript:
传递点击参数和定制点击追踪脚本作为参数。
reportAdClick: withCustomClickTrackerURL:
传递点击参数和定制点击追踪脚本作为参数。
-(void)reportAdClickAndOpenLandingURL:(NSDictionary*)extras withCustomClickTrackerScript:
传递点击参数和定制点击追踪脚本作为参数。
-(void)reportAdClickAndOpenLandingURL:(NSDictionary*)extras withCustomClickTrackerURL:
viewcontroller
类应如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
self.nativeAd = [[IMCustomNative alloc] initWithPlacementId:@"Insert native Placement ID Here"];
self.nativeAd.delegate = self;
[self.nativeAd load];
}
//Track impressions using custom script
[IMCustomNative bindNative:self.nativeAd toView:UIRenderedView withImpressionTrackerScript:@"custom script here"];
//Track impressions using custom URL
[IMCustomNative bindNative:self.nativeAd toView:UIRenderedView withImpressionTrackerURL:"custom impression tracking URL here"]
//Track clicks using custom script. (Note: Landing page needs to be opened by you)
[self.nativeAd reportAdClick:"clicks params" withCustomClickTrackerScript
:"custom script here"];
//Track clicks using custom URL. (Note: Landing page needs to be opened by you)
[self.nativeAd reportAdClick:"clicks params" withCustomClickTrackerURL
:"custom click tracking URL here"];
//Track clicks using custom script and let InMobi open the Landing Page
[self.nativeAd reportAdClickAndOpenLandingURL:"clicks params" withCustomClickTrackerScript
:"custom script here"];
//Track impressions using custom URL and let InMobi open the Landing Page
[self.nativeAd reportAdClickAndOpenLandingURL:"clicks params" withCustomClickTrackerURL
:"custom impression tracking URL here"];