canDisplayBannerAds = YES causes Sprite Kit app to crash

心不动则不痛 提交于 2019-12-13 02:49:53

问题


Working on my first game and trying to implement ads in to it.

I've declared the following in my viewController.h

#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>
#import <iAd/iAd.h>

@interface ViewController : UIViewController <ADBannerViewDelegate>

And in my implementation file I've Got the following (used because I need landscape layout)

- (void)viewWillLayoutSubviews
{


        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"Result"];
    [super viewWillLayoutSubviews];
    NSError *error;
    NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:@"background-music-aac" withExtension:@"caf"];
    self.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
    self.backgroundMusicPlayer.numberOfLoops = -1;
    [self.backgroundMusicPlayer prepareToPlay];
    [self.backgroundMusicPlayer play];

    // Configure the view.

    SKView * skView = (SKView *)self.view;
    if (!skView.scene) {
      skView.showsFPS = YES;
      skView.showsNodeCount = YES;

      // Create and configure the scene.
      SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
      scene.scaleMode = SKSceneScaleModeAspectFill;

      // Present the scene.
      [skView presentScene:scene];
        self.canDisplayBannerAds = YES;

    }
}

However when I run the app instantly crashes (log below) - if I change self.canDisplayBannerAdds = NO; the app runs fine - I have a feeling this is ridiculously simple but I can't see it for looking - anyone got any ideas?

2014-02-13 22:40:20.605 SpriteKitSimpleGame[2812:70b] -[UIView scene]: unrecognized selector sent to instance 0xcb354c0
2014-02-13 22:40:20.608 SpriteKitSimpleGame[2812:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView scene]: unrecognized selector sent to instance 0xcb354c0'
*** First throw call stack:
(
    0   CoreFoundation                      0x019fa5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0177d8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01a97903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x019ea90b ___forwarding___ + 1019
    4   CoreFoundation                      0x019ea4ee _CF_forwarding_prep_0 + 14
    5   SpriteKitSimpleGame                 0x00008169 -[ViewController viewWillLayoutSubviews] + 729
    6   UIKit                               0x0043321a -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 278
    7   libobjc.A.dylib                     0x0178f81f -[NSObject performSelector:withObject:] + 70
    8   QuartzCore                          0x0232e2ea -[CALayer layoutSublayers] + 148
    9   QuartzCore                          0x023220d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    10  QuartzCore                          0x0232e235 -[CALayer layoutIfNeeded] + 160
    11  UIKit                               0x004ee613 -[UIViewController window:setupWithInterfaceOrientation:] + 304
    12  UIKit                               0x0040d177 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
    13  UIKit                               0x0040bd16 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
    14  UIKit                               0x0040bbe8 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
    15  UIKit                               0x0040bc70 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
    16  UIKit                               0x0040ad0a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
    17  UIKit                               0x0040ac6c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
    18  UIKit                               0x0040b9c3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
    19  UIKit                               0x0040efb6 -[UIWindow setDelegate:] + 449
    20  UIKit                               0x004e0737 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
    21  UIKit                               0x00404c1c -[UIWindow addRootViewControllerViewIfPossible] + 609
    22  UIKit                               0x00404d97 -[UIWindow _setHidden:forced:] + 312
    23  UIKit                               0x0040502d -[UIWindow _orderFrontWithoutMakingKey] + 49
    24  UIKit                               0x0040f89a -[UIWindow makeKeyAndVisible] + 65
    25  UIKit                               0x003c2cd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
    26  UIKit                               0x003c73a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    27  UIKit                               0x003db87c -[UIApplication handleEvent:withNewEvent:] + 3447
    28  UIKit                               0x003dbde9 -[UIApplication sendEvent:] + 85
    29  UIKit                               0x003c9025 _UIApplicationHandleEvent + 736
    30  GraphicsServices                    0x027b12f6 _PurpleEventCallback + 776
    31  GraphicsServices                    0x027b0e01 PurpleEventCallback + 46
    32  CoreFoundation                      0x01975d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    33  CoreFoundation                      0x01975a9b __CFRunLoopDoSource1 + 523
    34  CoreFoundation                      0x019a077c __CFRunLoopRun + 2156
    35  CoreFoundation                      0x0199fac3 CFRunLoopRunSpecific + 467
    36  CoreFoundation                      0x0199f8db CFRunLoopRunInMode + 123
    37  UIKit                               0x003c6add -[UIApplication _run] + 840
    38  UIKit                               0x003c8d3b UIApplicationMain + 1225
    39  SpriteKitSimpleGame                 0x0000979d main + 141
    40  libdyld.dylib                       0x0310570d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

回答1:


When canDisplayBannerAdds is set to YES, you need to use originalContentView on your controller to get hold of your view.

Try using the following code to get hold of your view:

SKView * skView = self.originalContentView;


来源:https://stackoverflow.com/questions/21767155/candisplaybannerads-yes-causes-sprite-kit-app-to-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!