SinoAdsPod集成文档
1. 将SinoAdsPod添加到Xcode项目中
SinoAdsPod可以通过Cocoapods集成到Xcode项目中。请添加以下代码到您的 Podfile
中:####
pod "SinoAds"
当前最新版本是 0.0.4
将以下代码添加到项目的 info.plist
中
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
2. 添加代码
2.1 初始化SinoAds
在 AppDelegate.m
中导入头文件 <SinoAds/SinoAds.h>
Objective-C
#import <SinoAds/SinoAds.h>
Swift
import SinoAds
在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中初始化sdk
Objective-C
[[SA shared] setAppId:@"Your appId" andAppSecret:@"Your appSecret"];
Swift
SA.shared()?.setAppId("Your appId", andAppSecret: "Your appSecret")
“您的appId”和“您的appSecret”需要从后台配置中获取。
如果您想使用 RewardedVideoAd
,需要设置 deviceId
,代码如下
Objective-C
[[SA shared] setDeviceId:@"Your deviceId"];
Swift
SA.shared()?.setDeviceId("Your deviceId")
deviceId
可以由您自行生成,或者使用设备的 IDFV
,代码如下
Objective-C
[[[UIDevice currentDevice] identifierForVendor] UUIDString]
Swift
UIDevice.current.identifierForVendor?.uuidString
2.2 设置用户信息(可选)
设置用户性别,代码如下:Objective-C
[[SA shared] setUserGender:User gender];
Swift
SA.shared()?.setUserGender(User gender)
用户性别选项如下
Objective-C
SAUserGenderUnknown,
SAUserGenderMan,
SAUserGenderWoman
Swift
SAUserGender.unknown
SAUserGender.man
SAUserGender.woman
设置用户年龄,代码如下
Objective-C
[[SA shared] setUserAge:User age];
Swift
SA.shared()?.setUserAge(User age)
设置是否支持支付,默认值是 NO
。代码如下
Objective-C
[[SA shared] setIsPaidApp:NO];
Swift
SA.shared()?.setIsPaidApp(false)
设置是否显示调试信息,代码如下
Objective-C
[[SA shared] setEnableLog:YES];
Swift
SA.shared()?.setEnableLog(true)
InterstitialAd
2.3 集成 InterstitialAd
,代码如下
2.3.1 初始化 Objective-C
SAInterstitial *interstitial = [[SAInterstitial alloc] initWithUnitId:@"Your unitId"];
Swift
var interstitial: SAInterstitial? = SAInterstitial(unitId: "Your unitId")
"您的单位ID"需要从后台配置中获取。
InterstitialAd
委托,代码如下
2.3.2 设置 Objective-C
interstitial.delegate = self;
Swift
interstitial?.setDelegate(self)
InterstitialAd
,代码如下
2.3.3 加载 Objective-C
[interstitial load];
Swift
interstitial?.load()
每次广告播放后,您需要调用'load'方法来重新加载新广告。您不能再次播放缓存的广告。
SAInterstitialDelegate
2.3.4 使用以下代码实现 Objective-C
//Ad loaded successfully
- (void)interstitialDidLoad:(SAInterstitial * _Nonnull)interstitial {
NSLog(@"interstitialDidLoad");
}
//Ad video loaded successfully
- (void)interstitialVideoDidLoad:(SAInterstitial * _Nonnull)interstitial {
NSLog(@"interstitialVideoDidLoad");
}
//Ad failed to load
- (void)interstitial:(SAInterstitial * _Nonnull)interstitial didFailToLoadWithError:(NSError * _Nonnull)error {
NSLog(@"interstitial didFailToLoadWithError %ld", (long)[error code]);
}
//Ad is about to show
- (void)interstitialWillPresentScreen:(SAInterstitial * _Nonnull)interstitial {
NSLog(@"interstitialWillPresentScreen");
}
//Ad has been shown
- (void)interstitialDidPresentScreen:(SAInterstitial * _Nonnull)interstitial {
NSLog(@"interstitialDidPresentScreen");
}
//Ad display failed
- (void)interstitial:(SAInterstitial * _Nonnull)interstitial didFailToPresentScreenWithError:(NSError * _Nonnull)error {
NSLog(@"interstitial didFailToPresentScreenWithError %ld", (long)[error code]);
}
//Ad completed display
- (void)interstitialDidPlayFinish:(SAInterstitial * _Nonnull)interstitial {
NSLog(@"interstitialDidPlayFinish %@ %@", [interstitial getUnitId], [interstitial getSdkName]);
}
//Ad is about to close
- (void)interstitialWillDismissScreen:(SAInterstitial * _Nonnull)interstitial {
NSLog(@"interstitialWillDismissScreen");
}
//Ad is closed
- (void)interstitialDidDismissScreen:(SAInterstitial * _Nonnull)interstitial {
NSLog(@"interstitialDidDismissScreen");
}
//User clicks on the ad
- (void)interstitialDidClick:(SAInterstitial * _Nonnull)interstitial {
NSLog(@"interstitialDidClick");
}
//User clicks to skip
- (void)interstitialDidClickSkip:(SAInterstitial * _Nonnull)interstitial {
NSLog(@"interstitialDidClickSkip");
}
Swift
//Ad loaded successfully
func interstitialDidLoad(_ interstitial: SAInterstitial) {
print("interstitialDidLoad")
}
//Ad video loaded successfully
func interstitialVideoDidLoad(_ interstitial: SAInterstitial) {
print("interstitialVideoDidLoad");
}
//Ad failed to load
func interstitial(_ interstitial: SAInterstitial, didFailToLoadWithError error: Error) {
print("interstitial didFailToLoadWithError \((error as NSError).code)")
}
//Ad is about to show
func interstitialWillPresentScreen(_ interstitial: SAInterstitial) {
print("interstitialWillPresentScreen")
}
//Ad has been shown
func interstitialDidPresentScreen(_ interstitial: SAInterstitial) {
print("interstitialDidPresentScreen")
}
//Ad display failed
func interstitial(_ interstitial: SAInterstitial, didFailToPresentScreenWithError error: Error) {
print("interstitial didFailToPresentScreenWithError \((error as NSError).code)")
}
//Ad completed display
func interstitialDidPlayFinish(_ interstitial: SAInterstitial) {
print("interstitialDidPlayFinish \(interstitial.getUnitId() ?? "") \(interstitial.getSdkName() ?? "")")
}
//Ad is about to close
func interstitialWillDismissScreen(_ interstitial: SAInterstitial) {
print("interstitialWillDismissScreen")
}
//Ad is closed
func interstitialDidDismissScreen(_ interstitial: SAInterstitial) {
print("interstitialDidDismissScreen")
}
//User clicks on the ad
func interstitialDidClick(_ interstitial: SAInterstitial) {
print("interstitialDidClick")
}
//User clicks to skip
func interstitialDidClickSkip(_ interstitial: SAInterstitial) {
print("interstitialDidClickSkip")
}
SAInterstitial
。如果缓存成功,播放 SAInterstitial
。代码如下
2.3.5 确定是否成功缓存 Objective-C
if ([interstitial isReady]) {
[interstitial presentFromRootViewController:RootViewController];
}
Swift
if (interstitial?.isReady() ?? false) {
interstitial?.present(fromRootViewController: RootViewController)
}
RootViewController
可以通过以下方式获取
Objective-C
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
Swift
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
- (void)interstitialDidDismissScreen:(SAInterstitial * _Nonnull)interstitial
中重新加载广告,如下所示
2.3.6 建议在 Objective-C
[self->interstitial load];
Swift
self.interstitial?.load();
RewardedVideoAd
2.4 集成 RewardedVideoAd
,代码如下
2.4.1 初始化 Objective-C
SARewardedVideo *rewardedVideo = [[SARewardedVideo alloc] initWithUnitId:@"Your unitId" andExtraData:nil];
Swift
var rewardedVideo: SARewardedVideo? = SARewardedVideo(unitId: "Your unitId", andExtraData: nil)
"您的单位ID"需要从后台配置中获取。
RewardedVideoAd
代理,代码如下
2.4.2 设置 Objective-C
rewardedVideo.delegate = self;
Swift
rewardedVideo?.setDelegate(self)
RewardedVideoAd
,代码如下
2.4.3 加载 Objective-C
[rewardedVideo load];
Swift
rewardedVideo?.load()
每次广告播放后,您需要调用'load'方法来重新加载新广告。您不能再次播放缓存的广告。
SARewardedVideoDelegate
2.4.4 使用以下代码实现 Objective-C
//Ad is successfully played, returning the extraData passed in when the ad was created.
- (void)rewardedVideo:(SARewardedVideo * _Nonnull)rewardedVideo finishWithExtraData:(SAExtraData * _Nullable)extraData {
NSLog(@"rewardedVideo finishWithExtraData");
}
//Ad is failed to play
- (void)rewardedVideo:(SARewardedVideo * _Nonnull)rewardedVideo finishWithError:(NSError * _Nonnull)error {
NSLog(@"rewardedVideo finishWithError");
}
//Ad server verification is completed, returning verify to YES, the server verifies successfully.
- (void)rewardedVideoServerRewardDidSucceed:(SARewardedVideo * _Nonnull)rewardedVideo withVerify:(BOOL)verify {
NSLog(@"rewardedVideoServerRewardDidSucceed %d", verify ? 1 : 0);
}
//Ad server verification failed
- (void)rewardedVideoServerRewardDidFail:(SARewardedVideo * _Nonnull)rewardedVideo {
NSLog(@"rewardedVideoServerRewardDidFail");
}
//Ad is loaded successfully
- (void)rewardedVideoDidLoad:(SARewardedVideo * _Nonnull)rewardedVideo {
NSLog(@"rewardedVideoDidLoad");
}
//Ad is failed to load
- (void)rewardedVideo:(SARewardedVideo * _Nonnull)rewardedVideo didFailToLoadWithError:(NSError * _Nonnull)error {
NSLog(@"rewardedVideo didFailToLoadWithError %ld", (long)[error code]);
}
//Ad is about to show
- (void)rewardedVideoWillPresentScreen:(SARewardedVideo * _Nonnull)rewardedVideo {
NSLog(@"rewardedVideoWillPresentScreen");
}
//Ad has been shown
- (void)rewardedVideoDidPresentScreen:(SARewardedVideo * _Nonnull)rewardedVideo {
NSLog(@"rewardedVideoDidPresentScreen");
}
//Ad is about to close
- (void)rewardedVideoWillDismissScreen:(SARewardedVideo * _Nonnull)rewardedVideo {
NSLog(@"rewardedVideoWillDismissScreen");
}
//Ad is closed
- (void)rewardedVideoDidDismissScreen:(SARewardedVideo * _Nonnull)rewardedVideo {
NSLog(@"rewardedVideoDidDismissScreen");
}
//User clicks on the ad
- (void)rewardedVideoDidClick:(SARewardedVideo * _Nonnull)rewardedVideo {
NSLog(@"rewardedVideoDidClick");
}
Swift
//The ad is successfully played, returning the extraData passed in when the ad was created.
func rewardedVideo(_ rewardedVideo: SARewardedVideo, finishWith extraData: SAExtraData?) {
print("rewardedVideo finishWithExtraData")
}
//Ad is failed to play
func rewardedVideo(_ rewardedVideo: SARewardedVideo, finishWithError error: Error) {
print("rewardedVideo finishWithError")
}
//Ad server verification is completed, returning verify to true, the server verifies successfully.
func rewardedVideoServerRewardDidSucceed(_ rewardedVideo: SARewardedVideo, withVerify verify: Bool) {
print("rewardedVideoServerRewardDidSucceed \(verify)")
}
//Ad server verification failed
func rewardedVideoServerRewardDidFail(_ rewardedVideo: SARewardedVideo) {
print("rewardedVideoServerRewardDidFail")
}
//Ad is loaded successfully
func rewardedVideoDidLoad(_ rewardedVideo: SARewardedVideo) {
print("rewardedVideoDidLoad")
}
//Ad is failed to load
func rewardedVideo(_ rewardedVideo: SARewardedVideo, didFailToLoadWithError error: Error) {
print("rewardedVideo didFailToLoadWithError \((error as NSError).code)")
}
//Ad is about to show
func rewardedVideoWillPresentScreen(_ rewardedVideo: SARewardedVideo) {
print("rewardedVideoWillPresentScreen")
}
//Ad has been shown
func rewardedVideoDidPresentScreen(_ rewardedVideo: SARewardedVideo) {
print("rewardedVideoDidPresentScreen")
}
//Ad is about to close
func rewardedVideoWillDismissScreen(_ rewardedVideo: SARewardedVideo) {
print("rewardedVideoWillDismissScreen")
}
//Ad is closed
func rewardedVideoDidDismissScreen(_ rewardedVideo: SARewardedVideo) {
print("rewardedVideoDidDismissScreen")
}
//User clicks on the ad
func rewardedVideoDidClick(_ rewardedVideo: SARewardedVideo) {
print("rewardedVideoDidClick")
}
SARewardedVideo
。如果缓存成功,播放 SARewardedVideo
。以下为代码:
2.4.5 确定是否成功缓存了 Objective-C
if ([rewardedVideo isReady]) {
[rewardedVideo presentFromRootViewController:RootViewController];
}
Swift
if (rewardedVideo?.isReady() ?? false) {
rewardedVideo?.present(fromRootViewController: RootViewController)
}
RootViewController
可以通过以下方式获取
Objective-C
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
Swift
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
- (void)rewardedVideo:(SARewardedVideo * _Nonnull)rewardedVideo finishWithExtraData:(SAExtraData * _Nullable)extraData
被调用,成功状态可在此处理。
2.4.6 在广告成功播放后,以下接口
- (void)rewardedVideoDidDismissScreen:(SARewardedVideo * _Nonnull)rewardedVideo
中重新加载广告,以下为代码
2.4.7 建议在接口 Objective-C
[self->rewardedVideo load];
Swift
self.rewardedVideo?.load()
SAExtraData
(可选)
2.5 设置初始化SARewardedVideo
时,您可以设置SAExtraData
数据。在- (void)rewardedVideo:(SARewardedVideo * _Nonnull)rewardedVideo finishWithExtraData:(SAExtraData * _Nullable)reward
中,服务端会把SAExtraData
数据返回给客户端。数据结构如下
Objective-C
NSString* type;//Type of video reward
NSInteger amount;//Number of video rewards
NSString* data;//Additional postback information
Swift
var type: String!//Type of video reward
var amount: Int//Number of video rewards
var data: String!//Additional postback information
创建和使用SAExtraData
的代码如下
Objective-C
SAExtraData* extraData = [[SAExtraData alloc] init];
[extraData setType:@"Skill"];
[extraData setAmount:1];
[extraData setData:@"ExtraData"];
SARewardedVideo *rewardedVideo = [[SARewardedVideo alloc] initWithUnitId:@"Your unitId" andExtraData:extraData];
Swift
let extraData = SAExtraData()
extraData?.type = "Skill"
extraData?.amount = 1
extraData?.data = "ExtraData"
var rewardedVideo: SARewardedVideo? = SARewardedVideo(unitId: "Your unitId", andExtraData: extraData)
SAExtraData
可以在每次load
之前重置
Objective-C
[rewardedVideo setExtraData:extraData];
[rewardedVideo load];
Swift
rewardedVideo?.setExtraData(extraData)
rewardedVideo?.load()
2.6 广告错误码
常见错误码如下
SAErrorCodeNoAdError = 0, //Parsed data has no ads
SAErrorCodeNetError = 1, //Network request failed
SAErrorCodeParseError = 2, //Parsing failure
SAErrorCodeParamEroor = 3, //Parameter error
SAErrorCodeTimeout = 4, //Time out
SAErrorCodeSuccess = 5, //Success
SAErrorCodeNoAd = 6, //No ads
SAErrorCodeContentType = 7, //Http conent_type error
SAErrorCodeRequestPBError = 8, //Http request pb error
SAErrorCodeAppEmpty = 9, //Request app can't be empty
SAErrorCodeWapEmpty = 10, //Request wap cannot be empty
SAErrorCodeAdSlotEmpty = 11, //Missing ad unit description
SAErrorCodeAdSlotSizeEmpty = 12, //Ad unit size is illegal
SAErrorCodeAdSlotIdError = 13, //Ad unit ID is illegal
SAErrorCodeAdCountError = 14, //Requested ad number error
SAErrorCodeSysError = 15, //Ad server error
SAErrorCodeSizeError = 16, //The material size is not filled or the material size is larger than 10000
SAErrorCodeUnionAdSiteIdError = 17, //The media is empty or not running
SAErrorCodeUnionRequestInvalidError = 18, //App status is invalid
SAErrorCodeUnionAppSiteRelError = 19, //App ID error
SAErrorCodeBundleNameError = 20, //Bundle name error
2.7 网络错误码
常见错误码如下
SAHttpErrorCodeRequestError = -1, //Network request return value is abnormal
SAHttpErrorCodeDataLengthError = -2, //Network request return data length is abnormal
SAHttpErrorCodeUnexpectedError = -3, //Data parsing has an unknown error
SAHttpErrorCodeDataEmptyError = -4, //Data is empty
SAHttpErrorCodeDecodeError = -5, //Data decoding failed
SAHttpErrorCodeDecryptError = -6, //Data decryption failed
SAHttpErrorCodeServerConfigError = 1001, //Server data configuration is abnormal
SAHttpErrorCodeServerDecodeError = 1002, //Server decryption failed
SAHttpErrorCodeRequestDataEmptyError = 1003,//Server request data is empty
SAHttpErrorCodeWrongAppIdError = 1004, //Wrong App ID
SAHttpErrorCodeServerExceptionError = 9001, //Server unknown error