问题
I have added Facebook audience network in my iOS App for native ads integration. Everything is working fine but there are only testing ads.
I have added testDeviceKey for displaying ads if I try to remove this then no ads are displaying that is also making me confused. My code is -
@implementation AdFeedsView
FBNativeAd *nativeAd;
static NSString *adPlacementId = @"xxxxxxxxx";
- (void)showNativeAd:(UIViewController *)vc
{
nativeAd = [[FBNativeAd alloc] initWithPlacementID:adPlacementId];
nativeAd.delegate = self;
NSLog(@"isTestMode1: %d",[FBAdSettings isTestMode]);
NSString *testDeviceKey = [FBAdSettings testDeviceHash];
if (testDeviceKey) {
[FBAdSettings addTestDevice:testDeviceKey];
}
[nativeAd loadAd];
}
So I just want to ask that what changes should I need to do in my code so that we can see proper ads other than testing ads in my live app?
回答1:
Finally I find the solution. Actually I have added the code
NSString *testDeviceKey = [FBAdSettings testDeviceHash];
if (testDeviceKey) {
[FBAdSettings addTestDevice:testDeviceKey];
}
that is making all the active devices as testing devices hence there are only testing ads. I have removed it in the live version and included the code
[FBAdSettings clearTestDevices];
We can also do
if ([FBAdSettings isTestMode]) {
[FBAdSettings clearTestDevice:[FBAdSettings testDeviceHash]];
}
I have a wrong perception about testmode. I think that it will be false when my app become live but actually it just tell that the device has been added as testdevice or not.
回答2:
Swift 3
This code will make sure that your test ads show on testflight or the simulator/device and also will make sure when you release to the app store that your ads will be shown as LIVE ads and the test devices will be cleared.
If you have your ads in many places you can put this code in the AppDelegate
import FBAudienceNetwork
private func facebookAdsControl() {
#if RELEASE
self.clearTestDevicesForFacebookAds()
#else
self.addTestDevicesForFacebookAds()
#endif
}
///remove for live mode
private func addTestDevicesForFacebookAds(){
let key = FBAdSettings.testDeviceHash()
FBAdSettings.setLogLevel(FBAdLogLevel.Log)
FBAdSettings.addTestDevice(key)
}
///add for live mode
private func clearTestDevicesForFacebookAds() {
FBAdSettings.clearTestDevices()
}
call the: facebookAdsControl()
in the didFinishLaunchingWithOptions
You can also call this code in the code where you have the ads but make sure you call it before adView.load()
In order to use the macro you have to add the flags to your Swift Flags in the Build Settings
I hope this helps someone as the Facebook Docs are not very clear as I have found out.
来源:https://stackoverflow.com/questions/41053912/how-to-make-the-facebook-ads-live