0
votes

I have an app which has Game Center Leaderboards, and an Admob banner included. The ads are live and when I run the app on an actual device through Xcode 8 it run fine. But when I launch an emulator from Xcode 8 the app loads but after a few seconds I have the error:

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
        reason: 'Application tried to present modal view controller on itself. Presenting controller is <GKHostedAuthenticateViewController: 0x7f818d749310>.'

I am currently setting up my banner ad in my view controller like so:

    mybanner.adUnitID = "XXX";
    super.viewDidLoad();

    mybanner.rootViewController = self;
    mybanner.delegate = self;
    let request = GADRequest();
    mybanner.load(request);

    //view.addConstraint(NSLayoutConstraint(item: mybanner, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0));
    //view.addConstraint(NSLayoutConstraint(item: mybanner, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0));

    NotificationCenter.default.addObserver(self, selector: #selector(self.showBannerAd), name: NSNotification.Name(rawValue: "showbannerad"), object: nil);
    NotificationCenter.default.addObserver(self, selector: #selector(self.hideBannerAd), name: NSNotification.Name(rawValue: "hidebannerad"), object: nil);

And calling the notifications to show/hide the banner from the scene classes as follows:

  NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showbannerad"), object: nil);

I am initializing my Game Center object in its own class and calling its init method from the start screen the init for the Game Center Controller is as follows:

func authPlayer(scene: SKScene) {
    let localPlayer = GKLocalPlayer.localPlayer();

    localPlayer.authenticateHandler = {
        (view, error) in
        if view != nil {
            view!.present(view!, animated: true, completion: nil);
        }else {
            print(GKLocalPlayer.localPlayer().isAuthenticated);
        }
    }
}

  /// In the GamvViewController
  func showBannerAd() {
    if mybanner.isHidden == true {
        mybanner.isHidden = false;
    }
}

Its strange that it works on an actual device but not in an emulator. If you would like more details let me know. Thanks.

1
what is mybanner? why you set it's rootViewController to itself?? it seems to be the problem here. - Codus
Show the funcs that you're calling with your notifications. - Daniel Storm
My banner is an Admob banner. And its in the GameViewController class. - Keith Drake Waggoner

1 Answers

2
votes

In the localPlayer.authenticateHandler,there is a view,what is the class?Is it GKHostedAuthenticateViewController?.If it is,that is the problem. The error message is clear,some ViewController call the present it self.