在Cocos2dx v3中集成Admob iOS
问题描述:
我正在将广告集成到cocos2dx v3中。我已经尝试了2个教程,但我收到错误..我的应用程序在运行时总是崩溃。 Link 1在Cocos2dx v3中集成Admob iOS
这是我得到了什么错误
编辑
代码
#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
答
希望我不迟到晚会。你可以打开异常断点,看看它断了哪一行。从那里走容易。希望你还添加了admob运行所需的所有必要框架。如果不是,请检查这些框架。
AdSupport AudioToolbox AVFoundation CoreGraphics CoreMedia CoreTelephony EventKit EventKitUI MessageUI StoreKit SystemConfiguration
欲了解更多信息。到这里。 https://developers.google.com/admob/ios/quick-start
似乎您忘记设置教程中提到的-Objc链接器标志http://www.apusstudio.com/2014/04/cocos2d-x-with-admob-iad-mediation-in.html – 2014-11-24 09:11:42
已添加-objc链接器标志。 – Zohaib 2014-11-24 09:43:26
在问题中发布代码,将错误发布为文本不是图像 – LearnCocos2D 2014-11-24 09:56:55