51

I have installed the AdMob SDK 6.2.0 (Xcode 4.5 (4G182) and I am testing on an iPhone 4S and the simulator). I followed the tutorial and just want to get AdMob showing a banner once. But it crashes on the last line:

self.bannerView = [[GADBannerView alloc]
               initWithFrame:CGRectMake(0.0,
                                        self.view.frame.size.height -
                                        GAD_SIZE_320x50.height,
                                        GAD_SIZE_320x50.width,
                                        GAD_SIZE_320x50.height)];

self.bannerView.delegate = self;
self.bannerView.adUnitID = @"xREMOVEDBYMEx";
self.bannerView.rootViewController = self;
[self.view addSubview:self.bannerView];
GADRequest * request = [GADRequest request];
request.testing = YES;
[self.bannerView loadRequest:request];

The crash is

2012-09-28 09:03:58.268 NewProject[1467:c07] -[GADObjectPrivate changeState:]: unrecognized selector sent to instance 0x95c17d0 2012-09-28 09:03:58.276 NewProject[1467:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GADObjectPrivate changeState:]: unrecognized selector sent to instance 0x95c17d0'

This looks like a bug in the library to me but I think thats very unlikely. Any thoughts?

EDIT: The exact same code works in the example projects. Is there something I can do wrong with my project file that would result in that error?

david
  • 3,553
  • 4
  • 28
  • 39

4 Answers4

171

You need to add -ObjC to the Other Linker Flags of your application target's build setting:

  1. Click the blue top-level project icon in XCode
  2. Choose your target and go to Build Settings
  3. Under Other Linker Flags add -ObjC for both Release and Debug

Source: https://developers.google.com/mobile-ads-sdk/docs/admob/mediation#ios-linker

Also be sure to do a clean and rebuild

Community
  • 1
  • 1
bart
  • 1,734
  • 1
  • 11
  • 2
  • 1
    my project won't compile with -all_load flag, but it seems to work without crash now after added -ObjC. Could you explain this? – Kent Nguyen Sep 29 '12 at 11:19
  • @bart: As a small christmas present I've updated this answer to contain the necessary information from the link and undeleted it. The next time please do it on your own though. – ThiefMaster Dec 23 '12 at 23:56
  • Why is it necessary to do so? – Martin Reichl Jan 15 '13 at 18:41
  • cocos2d projects seem to start with this flag. – OMGPOP Jun 30 '13 at 14:26
  • My God, project worked without this flag for years. And suddenly started to crash. WHY??? Why do I need this flag??? Any explanation? – Valeriy Van Jul 05 '13 at 12:31
  • I tried this in my project but it didn't work. My app still crashes. Any idea? – Manthan Jul 19 '13 at 13:01
  • It's not a solutions if u use Deployment iOS target 6 and more. It's will crash on compiling with facebook framework. – Dmitry Nelepov Aug 30 '13 at 17:34
  • FYI Does not work for iOS 10 and above on Xcode 8 as far as I know. My project already has that flag and I installed Firebase and Admob through Cocoapods, still get error. – Hedylove Sep 28 '16 at 05:20
  • Ignore that comment. ^ my error was that the UIView object was not set to the GAD class. – Hedylove Sep 30 '16 at 05:05
4

Best solution for me is use -force_load flag instead -ObjC enter image description here

Dmitry Nelepov
  • 7,246
  • 8
  • 53
  • 74
3

If you guys are still having trouble, make sure it's -ObjC and not -Objc. Geez, spent weeks trying to figure out why.

ninjaneer
  • 6,951
  • 8
  • 60
  • 104
0

For those of you who tried adding the Obj-c linker flag and still got the same "unrecognized selector sent to instance" error when trying to load an ad I found a fix. This is for the newest XCode (version 8.1).

For reference:

  • I added Google AdMob using cocoapods.
  • I used storyboard to make an outlet connection to my viewcontroller and GADBannerView was NOT available as a class so I wrote it in like most people did.
  • On one of the stackoverflow answers that I saw the custom class for the bannerview was set to GADBannerView, which I assume would happen automatically if it was an option in the dropdown list when you added the outlet. For me it wasn't since I had to type it in myself.

No custom class specified screengrab

Looking at the dropdown I saw that GADBannerView was available as an option and selected it.

GADBannerView class available

After selecting it, the app ran without a hitch, no need to add the linker flag (mine was blank by default) or anything.

Hope this helps someone else!

Vrezh Gulyan
  • 218
  • 3
  • 6