在Cocos2dx v3 iOS中集成Admob

时间:2014-11-24 07:48:01

标签: ios cocos2d-x cocos2d-x-3.0

我正在cocos2dx v3中集成广告。我已经尝试了2个教程,但我收到错误..我的应用程序总是在运行时崩溃。 Link 1

Link 2

这是我得到的错误

enter image description here

修改

enter image description here

代码

    #import "BannerViewController.h"
#import "GADBannerView.h"

@interface BannerViewController () <GADBannerViewDelegate>

@end

@implementation BannerViewController {
    GADBannerView *_bannerView;
    UIViewController *_contentController;
    Boolean _bannerLoaded;
}

- (instancetype)initWithContentViewController:(UIViewController *)contentController
{
    self = [super init];
    if (self != nil) {

        _bannerView = [[GADBannerView alloc] initWithAdSize: kGADAdSizeSmartBannerPortrait];
        _bannerView.adUnitID = @"ca-app-pub-1907540021489164/5304609731"; //REPLACE WITH YOUR OWN PUBLISHER ID
        _bannerView.delegate = self;
        _contentController = contentController;
        _bannerLoaded = NO;

    }
    return self;
}


- (void)loadView
{
    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self addChildViewController:_contentController];
    [contentView addSubview:_contentController.view];
    [_contentController didMoveToParentViewController:self];
    [contentView addSubview:_bannerView];
    self.view = contentView;
}

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return [_contentController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#endif

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [_contentController preferredInterfaceOrientationForPresentation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [_contentController supportedInterfaceOrientations];
}

// For animation
- (void)viewDidLayoutSubviews
{
    CGRect contentFrame = self.view.bounds;
    CGRect bannerFrame = CGRectZero;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
    bannerFrame = _bannerView.frame;
#else
    bannerFrame.size = [_bannerView sizeThatFits:contentFrame.size];
#endif

    bannerFrame.origin.x = (contentFrame.size.width - bannerFrame.size.width) / 2;

    if (_bannerLoaded) {
        //contentFrame.size.height -= bannerFrame.size.height;
        bannerFrame.origin.y = contentFrame.size.height - bannerFrame.size.height;
    } else {
        bannerFrame.origin.y = contentFrame.size.height;
    }

    _contentController.view.frame = contentFrame;
    _bannerView.frame = bannerFrame;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    _bannerView.rootViewController = self;
    [self.view addSubview:_bannerView];

    GADRequest *request = [GADRequest request];
    [_bannerView loadRequest:request];
}

- (void)adViewDidReceiveAd:(GADBannerView *)bannerView
{
    NSLog(@"adViewDidReceiveAd");
    _bannerLoaded = YES;
    [UIView animateWithDuration:0.25 animations:^{
        [self.view setNeedsLayout];
        [self.view layoutIfNeeded];
    }];
}

- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
    NSLog(@"adView didFailToReceiveAdWithError");
    _bannerLoaded = NO;
    [UIView animateWithDuration:0.25 animations:^{
        [self.view setNeedsLayout];
        [self.view layoutIfNeeded];
    }];
}

- (void) hideBanner{
    [_bannerView removeFromSuperview];
}

- (void) showBanner{
    [self.view addSubview:_bannerView];

    GADRequest *request = [GADRequest request];
    [_bannerView loadRequest:request];

    NSLog(@"Show Bannerr");
}

- (void)dealloc {
    _bannerView.delegate = nil;
    [_bannerView release];
    [super dealloc];
}

@end

在AppController中

_bannerViewController = [[BannerViewController alloc] initWithContentViewController:_viewController];

错误

2014-11-24 15:00:44.329 demococos2d iOS[4205:138334] <Google> Category methods are not loaded. Make sure you link the Google Mobile Ads library using one of the -ObjC, -force_load, or -all_load linker flags. See https://developers.google.com/mobile-ads-sdk/docs/#ios for more information.
2014-11-24 15:00:44.856 demococos2d iOS[4205:138334] +[NSDecimalNumber gad_negativeOne]: unrecognized selector sent to class 0x1074e9618
2014-11-24 15:00:44.864 demococos2d iOS[4205:138334] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSDecimalNumber gad_negativeOne]: unrecognized selector sent to class 0x1074e9618'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001086e53f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010804dbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001086ec40d +[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001086447fc ___forwarding___ + 988
    4   CoreFoundation                      0x0000000108644398 _CF_forwarding_prep_0 + 120
    5   demococos2d iOS                     0x0000000104c529b8 -[GADStatisticDictionary decrementNumberForKey:shouldNotifyDelegate:] + 58
    6   demococos2d iOS                     0x0000000104c49a33 -[GADApplication init] + 461
    7   demococos2d iOS                     0x0000000104c4982f __35+[GADApplication sharedApplication]_block_invoke + 38
    8   libdispatch.dylib                   0x00000001099c77f4 _dispatch_client_callout + 8
    9   libdispatch.dylib                   0x00000001099b436d dispatch_once_f + 565
    10  demococos2d iOS                     0x0000000104c49807 +[GADApplication sharedApplication] + 102
    11  demococos2d iOS                     0x0000000104c45b0c +[GADSlot initialize] + 64
    12  libobjc.A.dylib                     0x000000010804e4d6 _class_initialize + 648
    13  libobjc.A.dylib                     0x00000001080576e1 lookUpImpOrForward + 351
    14  libobjc.A.dylib                     0x00000001080640d3 objc_msgSend + 211
    15  demococos2d iOS                     0x0000000104c43ca8 -[GADBannerView commonInitWithAdSize:] + 40
    16  demococos2d iOS                     0x0000000104c43eda -[GADBannerView initWithFrame:adSize:] + 177
    17  demococos2d iOS                     0x0000000104c440cb -[GADBannerView initWithAdSize:] + 142
    18  demococos2d iOS                     0x000000010474fec4 -[BannerViewController initWithContentViewController:] + 164
    19  demococos2d iOS                     0x0000000104750cc5 -[AppController application:didFinishLaunchingWithOptions:] + 757
    20  UIKit                               0x00000001057715a5 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 234
    21  UIKit                               0x00000001057720ec -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2463
    22  UIKit                               0x0000000105774e5c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
    23  UIKit                               0x0000000105773d22 -[UIApplication workspaceDidEndTransaction:] + 179
    24  FrontBoardServices                  0x000000010b15d2a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
    25  CoreFoundation                      0x000000010861aabc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    26  CoreFoundation                      0x0000000108610805 __CFRunLoopDoBlocks + 341
    27  CoreFoundation                      0x00000001086105c5 __CFRunLoopRun + 2389
    28  CoreFoundation                      0x000000010860fa06 CFRunLoopRunSpecific + 470
    29  UIKit                               0x0000000105773799 -[UIApplication _run] + 413
    30  UIKit                               0x0000000105776550 UIApplicationMain + 1282
    31  demococos2d iOS                     0x000000010475149f main + 111
    32  libdyld.dylib                       0x00000001099fc145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我还在其他链接器标记中添加了-ObjC

enter image description here

2 个答案:

答案 0 :(得分:2)

链接器选项为-ObjC,而不是-objc。案例很重要。

编辑:您的设置也处于错误的级别(有关当前有效的选项,请参阅左侧)。

答案 1 :(得分:0)

希望我没有迟到。你能否打开异常断点,看看它突破了哪一行。从那里开始会更容易。希望您还添加了admob运行所需的所有必要框架。如果没有,请检查这些框架。

  

AdSupport       AudioToolbox       AVFoundation       CoreGraphics中       CoreMedia       CoreTelephony       EventKit       EventKitUI       MessageUI       StoreKit       SystemConfiguration

了解更多信息。到这里。 https://developers.google.com/admob/ios/quick-start