【问题标题】:Show AdMob Native Ads with MoPub (iOS)使用 MoPub (iOS) 显示 AdMob 原生广告
【发布时间】:2016-09-06 16:11:47
【问题描述】:

我正在集成 MoPub SDK 以调解来自 Google AdMob 网络的广告。在实现我自己的 customEvent 和 Adapter 后,我可以让 AD 显示,但我无法让 AD 自行处理点击事件。就像当我点击 AdMob 原生广告时一样,它不会引导我到任何地方。使用 Facebook 和 Flurry 的 CustomEvent 和 Adapter 时,会自动处理点击。有人对这个问题有任何经验吗?

提前致谢。代码如下:

MPGoogleAdMobCustomEvent

@interface MPGoogleAdMobCustomEvent()
@property(nonatomic, strong)GADAdLoader *loader;
@end

@implementation MPGoogleAdMobCustomEvent

- (void)requestAdWithCustomEventInfo:(NSDictionary *)info
{
    MPLogInfo(@"MOPUB: requesting AdMob Native Ad");

    NSString *adUnitID = [info objectForKey:@"adUnitID"];

    if (!adUnitID) {

        [self.delegate nativeCustomEvent:self didFailToLoadAdWithError:MPNativeAdNSErrorForInvalidAdServerResponse(@"MOPUB: No AdUnitID from GoogleAdMob")];

        return;
    }

    self.loader = [[GADAdLoader alloc] initWithAdUnitID:adUnitID rootViewController:nil  adTypes:@[kGADAdLoaderAdTypeNativeContent] options:nil];
    self.loader.delegate = self;
    GADRequest *request = [GADRequest request];

#if (TARGET_OS_SIMULATOR)

    request.testDevices = @[ kGADSimulatorID ];

#endif

    CLLocation *location = [[CLLocationManager alloc] init].location;
    if (location) {
        [request setLocationWithLatitude:location.coordinate.latitude
                               longitude:location.coordinate.longitude
                                accuracy:location.horizontalAccuracy];
    }
    request.requestAgent = @"MoPub";
    [self.loader loadRequest:request];
}

- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeContentAd:(GADNativeContentAd *)nativeContentAd
{
    MPLogDebug(@"MOPUB: Did receive nativeAd");

    MPGoogleAdMobNativeAdAdapter *adapter = [[MPGoogleAdMobNativeAdAdapter alloc] initWithGADNativeContentAd:nativeContentAd];
    adapter.url = nativeContentAd.advertiser;
    MPNativeAd *interfaceAd = [[MPNativeAd alloc] initWithAdAdapter:adapter];

    NSMutableArray *imageArray = [NSMutableArray array];

    for (GADNativeAdImage *images in nativeContentAd.images) {

        [imageArray addObject:images.imageURL];

    }


    [super precacheImagesWithURLs:imageArray completionBlock:^(NSArray *errors) {

        if ([errors count]) {
            [self.delegate nativeCustomEvent:self didFailToLoadAdWithError:errors[0]];
        } else {
            [self.delegate nativeCustomEvent:self didLoadAd:interfaceAd];
        }

    }];
}

- (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(GADRequestError *)error
{
    MPLogDebug(@"MOPUB: AdMob ad failed to load with error (customEvent): %@", error.description);
    [self.delegate nativeCustomEvent:self didFailToLoadAdWithError:error];
}

@end

MPGoogleAdMobNativeAdAdapter

@interface MPGoogleAdMobNativeAdAdapter()<GADNativeAdDelegate>
@property(nonatomic, strong)NSDictionary *properties;
@end

@implementation MPGoogleAdMobNativeAdAdapter

- (instancetype)initWithGADNativeContentAd:(GADNativeContentAd *)contentAD
{
    self = [super init];
    if (self) {
        self.contentAd = contentAD;
        self.contentAd.delegate = self;
        self.properties = [self convertAssetsToProperties:contentAD];
    }
    return self;
}

- (NSDictionary *)convertAssetsToProperties:(GADNativeContentAd *)adNative
{
    self.contentAd = adNative;
    NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
    if (adNative.headline) {
        dictionary[kAdTitleKey] = adNative.headline;
    }
    if (adNative.body) {
        dictionary[kAdTextKey] = adNative.body;
    }
    if (adNative.images[0]) {
        dictionary[kAdMainImageKey] = ((GADNativeAdImage *)adNative.images[0]).imageURL.absoluteString;
    }
    if (adNative.callToAction) {
        dictionary[kAdCTATextKey] = adNative.callToAction;
    }
    return [dictionary copy];
}

#pragma mark MPNativeAdAdapter
- (NSTimeInterval)requiredSecondsForImpression
{
    return 0.0;
}

- (NSURL *)defaultActionURL
{
    return nil;
}

- (BOOL)enableThirdPartyClickTracking
{
    return YES;
}


- (void)willAttachToView:(UIView *)view
{
    self.contentAd.rootViewController = [self.delegate viewControllerForPresentingModalView];
}

- (void)didDetachFromView:(UIView *)view
{
    self.contentAd.rootViewController = nil;
}

#pragma mark GADNativeAdDelegate

- (void)nativeAdWillPresentScreen:(GADNativeAd *)nativeAd
{
    if ([self.delegate respondsToSelector:@selector(nativeAdWillPresentModalForAdapter:)]) {
        [self.delegate nativeAdWillPresentModalForAdapter:self];
    }
}

- (void)nativeAdDidDismissScreen:(GADNativeAd *)nativeAd
{
    if ([self.delegate respondsToSelector:@selector(nativeAdDidDismissModalForAdapter:)]) {
        [self.delegate nativeAdDidDismissModalForAdapter:self];
    }
}

- (void)nativeAdWillLeaveApplication:(GADNativeAd *)nativeAd
{
    if ([self.delegate respondsToSelector:@selector(nativeAdWillLeaveApplicationFromAdapter:)]) {
        [self.delegate nativeAdWillLeaveApplicationFromAdapter:self];
    }
}

@end




`

【问题讨论】:

    标签: ios objective-c admob ads mopub


    【解决方案1】:

    如果您有 AdMob 广告的自定义 UI,那么将有一个按钮用于 callToAction 部分。

    首先你需要添加一个选择器来检测点击动作,为那个按钮添加选择器

    [callToActionButton addTarget:self action:@selector(adCalled:) forControlEvents:UIControlEventTouchUpInside];
    

    之后实现 adCalled 方法以获取点击并进一步调用该方法,下面是代码供您参考 下面是我用来从我的收藏视图中获取广告对象的示例,然后我正在重定向它。

    - (void)adCalled:(id)sender
    {
        CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:mainCollectionView];   // Get the button position
        NSIndexPath *indexPath = [collectionView indexPathForItemAtPoint:buttonPosition];    // Get the index path of button so that I can retrieve the correct ad object
        id selectedAd = [adArray objectAtIndex:indexPath.row];
        if ([selectedAd isKindOfClass:[GADNativeContentAd class]]) {
            NSString *url = [selectedAd valueForKey:@"googleClickTrackingURLString"];
            NSLog(@"URL is :%@", url);
            NSURL *googleUrl = [NSURL URLWithString:url];
            if ([[UIApplication sharedApplication] canOpenURL: googleUrl]) {
                [[UIApplication sharedApplication] openURL:googleUrl];
            }
        }
    }
    

    使用这个我可以使用谷歌跟踪网址打开链接。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多