Adhese iOS SDK
介绍
这是用于原生 iOS 的 Adhese SDK。该 SDK 允许您从 Adhese API 加载广告数据,并以原生视图显示它们。
添加 SDK
SDK 通过 Cocopods 提供。将以下内容添加到您的 Podfile
并运行 pod install
pod 'Adhese'
代码示例
Objective-C
初始化
为应用程序一次初始化SDK。这应该在AppDelegate类中进行调用。最好是调用willFinishLaunchingWithOptions
方法,否则可能会因为SDK在未正确初始化之前被调用而出现错误。
#import <AdheseSDK/Adhese.h>
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions {
// Initialise the Adhese SDK
[Adhese initializeSdk:@"<YOUR_ACCOUNT>" withDebuggingEnabled:YES];
return YES;
}
Storyboard实现
在storyboard或.xib文件中添加一个UIView,在身份检查器中将其类设置为AdView
。
代码实现
在viewDidLayoutSubviews
中初始化AdView
。设置frame并将其添加为子视图。
halfPageAdview = [[AdView alloc] initWithFrame:self.halfPageContainerView.bounds];
[self.halfPageContainerView addSubview:halfPageAdview];
加载广告
#import <AdheseSDK/Adhese.h>
#import <AdheseSDK/CookieMode.h>
AdheseOptions *options = [[AdheseOptions alloc] initWithLocation:@"_demo_ster_a_"];
options.cookieMode = kAll;
options.slots = @[@"billboard", @"halfpage"];
[Adhese loadAds:options withCompletionHandler:^(NSArray<Ad *> * _Nonnull ads, AdheseError * _Nullable error) {
if (error) {
NSLog(@"Failed loading ads with errors: %@", error.description);
return;
}
Ad *billboard = [self findAd:ads byType:@"billboard"];
Ad *halfPage = [self findAd:ads byType:@"halfpage"];
if (billboard) {
[self.billboardAdview setAd:billboard];
} else {
[self.billboardAdview setHidden:YES];
}
if (halfPage) {
[self.halfpageAdView setAd:halfPage];
} else {
[self.halfpageAdView setHidden:YES];
}
}];
Swift
桥接头
要使用SDK,必须创建一个新的桥接头文件(更多信息请参阅这里)。
请确保在桥接头文件中导入SDK头文件
#import <AdheseSDK/Adhese.h>
初始化
为应用程序一次初始化SDK。这应该在AppDelegate类中进行调用。最好是调用willFinishLaunchingWithOptions
方法,否则可能会因为SDK在未正确初始化之前被调用而出现错误。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Adhese.initializeSdk("<YOUR_ACCOUNT>", withDebuggingEnabled: true)
return true
}
Storyboard实现
在storyboard或.xib文件中添加一个UIView,在身份检查器中将其类设置为AdView
。
代码实现
在viewDidLayoutSubviews
中初始化AdView
。设置frame并将其添加为子视图。
self.halfPageAdView = AdView(frame: self.adContainerView.bounds)
guard let adView2 = self.halfPageAdView else {
self.halfPageAdView?.isHidden = true
return
}
adView2.ad = halfpage
self.adContainerView.addSubview(adView2)
加载数字广告
SDK现在可以获取广告数据。以下是如何获取广告数据的示例(例如在viewDidLayoutSubviews
中):
let options = AdheseOptions(location: "_demo_ster_a_")
options.cookieMode = kAll
options.slots = ["billboard", "halfpage"]
Adhese.loadAds(options) { [weak self] (ads, error) in
if let error = error {
print("Failed loading ads with errors: %@", error.description)
return;
}
guard let self = self else { return }
guard let billboard = ads?.first(where: { (ad) -> Bool in
return ad.adType == "billboard"
}) else {
self.adView.isHidden = true
return
}
self.adView.ad = billboard
guard let halfpage = ads?.first(where: { (ad) -> Bool in
return ad.adType == "halfpage"
}) else {
return
}
guard let adView2 = self.halfPageAdView else {
self.halfPageAdView?.isHidden = true
return
}
adView2.ad = halfpage
self.adContainerView.addSubview(adView2)
}
代理事件
如果想要接收代理事件,可以在ViewController中应用AdViewDelegate
。以下是可以实现的可选代理方法。
- (void)adDidLoad:(id)adView withError:(AdheseError * _Nullable)error; // Triggers when the ad is loaded inside the view
- (void)trackerWasNotified:(id)adView withError:(AdheseError * _Nullable)error; // Triggers when the tracker URL has been called.
- (void)viewImpressionWasNotified:(id)adView withError:(AdheseError * _Nullable)error; // Triggers when the ad has become visible in the viewport
- (void)adClicked:(id)adView withError:(AdheseError * _Nullable)error; // Triggers when the advertisement was clicked.
额外
将adView.shouldOpenAd = NO
设置为开启/关闭在浏览器中自动打开广告。默认值是true,所以会自动打开。然而,当设置为false并实现adClicked
代理时,可以实现自定义行为。
发布 SDK
- 在
Adhese.podspec
文件中更改版本号 - 在 xcode 的 Adhese SDK 目标中更改版本号
- 将更改提交到 Github
- 用与 podspec 文件中指定的版本相同的标签标记您的提交
- 在项目的根目录中通过终端执行
pod trunk push
并按指示操作