在iOS平台上如何使用MoPub显示AdMob原生广告

5
我正在将MoPub SDK集成到Google AdMob网络上,用于调解广告。经过实施自己的customEvent和Adapter后,我可以显示广告,但不能使其处理自己的点击事件。也就是说,当我点击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




`
1个回答

0

如果您正在使用自定义UI来展示AdMob广告,则会有一个按钮用于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];
        }
    }
}

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

希望这有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接