在游戏中我们经常会为游戏添加广告,但是有时候cocos2dx 添加admob广告会出现错误,那么我们怎样才能解决cocos2dx 添加admob广告错误呢?请看下面。
依照官方教程添加文件及其 frameWork后 发现运行报错 错误如下
Undefined symbols for architecture i386:"_OBJC_CLASS_$_ASIdentifierManager", referenced from:
objc-class-ref in libGoogleAdMobAds.a(GADIdentifierUtilities.o)
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_SKStoreProductViewController", referenced from:
objc-class-ref in RevMobAds(RevMobStoreController.o)
"_SKStoreProductParameterITunesItemIdentifier", referenced from:
-[RevMobStoreController openStoreWithITunesItemId:] in RevMobAds(RevMobStoreController.o)
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
最终通过添加 frameWork搞定 添加了 StoreKit.framework AdSupport.framework CoreData.framework .
运行继续报错
ld: duplicate symbol _OBJC_CLASS_$_AppDelegate in /Users/AbhilashReddy/Library/Developer/Xcode/DerivedData/santabantaa-bykvybsbvqshqshfqxdlsxiqhehc/Build/Intermediates/santabantaa.build/Debug-iphonesimulator/santabantaa.build/Objects-normal/i386/AppDelegate-C7A14BE43C5E8C81.o and /Users/AbhilashReddy/Library/Developer/Xcode/DerivedData/santabantaa-bykvybsbvqshqshfqxdlsxiqhehc/Build/Intermediates/santabantaa.build/Debug-iphonesimulator/santabantaa.build/Objects-normal/i386/AppDelegate-A37C3709371E4892.o for architecture i386clang: error: linker command failed with exit code 1 (use -v to see invocation)
原来很是2的把demo也给弄进来了,删除demo的那个文件夹Add-ons 就好了再次报错 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GADObjectPrivate changeState:]: unrecognized selector sent to instance 0x95c17d0'
解决方案 add -ObjC to the Other Linker Flags of your application target's build setting。
加了个广告 他妹的把所有能碰到的问题都碰到了 真是技术不纯熟害死人啊。~~~PS:要弄个全屏的Admob 看了下SDK 似乎没有 就自己动手写
[[GADBannerView alloc] initWithFrame:self.view.bounds]; 结果报错 errors = "Ad size will not fit on screen"
但是改为 [[GADBannerView alloc] initWithFrame:CGRectMake(0,0,320,480)]; 就可以。 但是同时写了下半屏的CGRect,发现半屏的 frame 显示的不完全,左右被压缩得很厉害,造成了只显示了一个icon和一个按钮。不知道怎么解决。
先加入以上这么些,然后在 AppController.mm里面
- [plain] view plaincopy
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- // Add the view controller's view to the window and display.
- window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
- EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
- pixelFormat: kEAGLColorFormatRGBA8
- depthFormat: GL_DEPTH_COMPONENT16
- preserveBackbuffer: NO
- sharegroup: nil
- multiSampling: NO
- numberOfSamples:0 ];
- // Use RootViewController manage EAGLView
- viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
- viewController.wantsFullScreenLayout = YES;
- viewController.view = __glView;
- //------------------- Add Admob
- GADBannerView * bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
- bannerView_.rootViewController = viewController;
- bannerView_.adUnitID = @"a1517648ecd9c44";
- GADRequest *request = [GADRequest request];
- request.testing = NO;
- // request.testDevices = [NSArray arrayWithObjects:
- //
- // GAD_SIMULATOR_ID,
- //
- // @"YOU IPAD IDF",
- //
- // nil];
- //----------------------------
- // Set RootViewController to window
- if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
- {
- // warning: addSubView doesn't work on iOS6
- [window addSubview: viewController.view];
- }
- else
- {
- // use this method on ios6
- [window setRootViewController:viewController];
- }
- [viewController.view addSubview: bannerView_];
- [viewController.view bringSubviewToFront:bannerView_];
- [window makeKeyAndVisible];
- [[UIApplication sharedApplication] setStatusBarHidden: YES];
- [bannerView_ loadRequest: request];
- cocos2d::CCApplication::sharedApplication()->run();
- return YES;
- }
广告显示之后,需要在不同的Scene切换广告的位置
实现方式为:
定义一个OC类:
- [plain] view plaincopy
- #import <Foundation/Foundation.h>
- #import <UIKit/UIKit.h>
- @interface MyAdManager : NSObject
- {
- }
- +(void)showAddAtTop;
- +(void)showAddAtBottom;
- @end
- [plain] view plaincopy
- #import "MyAdManager.h"
- #import "myoc.h"
- @implementation MyAdManager
- + (void)showAddAtTop {
- if (view && size.width > 0) {
- [view setFrame:CGRectMake(0, -13, 320, 50)];
- }
- }
- + (void)showAddAtBottom {
- if (view && size.width > 0) {
- [view setFrame:CGRectMake(0, size.height - 50, 320, 50)];
- }
- }
- @end
- 添加一个全局的 UIView* view,和CGSize size放置到myoc.h
- [plain] view plaincopy
- #import <Foundation/Foundation.h>
- #import <UIKit/UIKit.h>
- UIView* view;
- CGSize size;
- #endif
为了不修改需要添加广告Scene的文件后缀(方便Android编译)
添加一个中间类 AdBridge
- [plain] view plaincopy
- #include <iostream>
- class AdBridge{
- public:
- static void showAdAtTop();
- static void showAdAtBottom();
- };
- [plain] view plaincopy
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- #include "MyAdManager.h"
- #endif
- void AdBridge::showAdAtTop(){
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- [MyAdManager showAddAtTop];
- #endif
- }
- void AdBridge::showAdAtBottom(){
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- [MyAdManager showAddAtBottom];
- #endif
- }
再将其加到到需要广告的Scene