No touchEvents after returning from Game Center on iPhone/iPod

Integrating GameCenter to OF iOS apps seems pretty straight for the most part. I’ve done it before on an iPad only app with no issues. However, for some reason on iPod/iPhone (not iPad), after I go to gameCenter and come back, I can’t get touchEvents on iPod/iPhone.

I feel like this must be something simple and I’ve seen it work in other iPhone OF games (Bit Pilot, Super Hex, etc), but I can’t seem to find the magic line of code I’m missing. Here’s the code (I feel like the issue must be in leaderboardViewControllerDidFinish):

  
#import <Foundation/Foundation.h>  
  
#include "ofxGameCenter.h"  
#include "ofxiPhoneExtras.h"  
#include "ofMain.h"  
#include "testApp.h"  
  
@implementation Game_CenterViewController  
  
-(UIDeviceOrientation)interfaceOrientation  
{  
    UIDeviceOrientation orient = UIDeviceOrientationLandscapeRight;  
      
    if([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationLandscapeLeft){  
        orient = UIDeviceOrientationLandscapeLeft;  
    }  
      
    return orient;  
}  
  
- (void) show:(NSString*)category  
{  
	GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];  
      
    //  
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;  
    //  
    if(orientation == UIInterfaceOrientationLandscapeLeft){  
        leaderboardController.view.transform = CGAffineTransformMakeRotation (90 / 180 * M_PI);  
    } else {  
        leaderboardController.view.transform = CGAffineTransformMakeRotation (0 / 180 * M_PI);  
    }  
      
	if (leaderboardController != NULL)  
	{  
		leaderboardController.category = category;  
		leaderboardController.timeScope = GKLeaderboardTimeScopeWeek;  
		leaderboardController.leaderboardDelegate = self;  
        //        leaderboardController ;  
          
        [ofxiPhoneGetUIWindow() addSubview:self.view];  
          
        [self presentModalViewController: leaderboardController animated: YES ];  
          
	}  
}  
  
  
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController  
{  
	[self dismissModalViewControllerAnimated: YES];  
	[viewController release];  
      
    //    [self dismissViewControllerAnimated:YES completion:nil];  
//    [self.view removeFromSuperview];  
//    [ofxiPhoneGetUIWindow() becomeFirstResponder];  
//      
////	ofRegisterTouchEvents(ofGetAppPtr());  
//      
////    testApp* app = ((testApp*)ofGetAppPtr());  
//    //app->changeScreen(app->gameScreen);  
//      
////    app->setup();  
//      
//    //ofRegisterTouchEvents(<#ListenerClass *listener#>);  
}  
  
@end  
  
ofxGameCenter::ofxGameCenter(){  
    init = false;  
    gameCenterViewController = [[Game_CenterViewController alloc] init];  
}  
  
ofxGameCenter::~ ofxGameCenter(){  
}  
  
void ofxGameCenter::setup(){  
	isAuthenticated=false;  
	if (isAvailable()) {  
		authenticateLocalPlayer();  
        ofLog(OF_LOG_NOTICE, ofToString(isAuthenticated));  
	}  
    init = true;  
}  
  
bool ofxGameCenter::isAvailable(){  
    Class gcClass = (NSClassFromString(@"GKLocalPlayer"));  
    // The device must be running running iOS 4.1 or later.  
    NSString *reqSysVer = @"4.1";  
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];  
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);  
    return (gcClass && osVersionSupported);  
}  
  
  
  
void ofxGameCenter::authenticateLocalPlayer(){  
    [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {  
		if (error == nil)  
        {  
			// Insert code here to handle a successful authentication.  
			isAuthenticated=true;  
		}  
		else  
		{  
			// Your application can process the error parameter to report the error to the player.  
            NSLog([error description]);  
			isAuthenticated=false;  
		}  
	}];  
}  
  
void ofxGameCenter::show(string _category){  
      
//    if(gameCenterViewController == nil){  
//        gameCenterViewController = [[Game_CenterViewController alloc] init];  
//    }  
      
    if(isAuthenticated)  
        [gameCenterViewController show:ofxStringToNSString(_category)];  
    else {  
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Game Center Unavailable"  
                                                            message:@"Please ensure you have a Game Center account setup and have internet connectivity."  
                                                           delegate:ofxiPhoneGetUIWindow() cancelButtonTitle:@"OK"otherButtonTitles:nil];  
        [alertView show];  
    }  
}  
  
void ofxGameCenter::reportScore(string _category, int _score){  
	if (isAvailable()) {  
		int64_t score = static_cast<int64_t>(_score);  
		NSString * category = ofxStringToNSString(_category);  
		GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];  
		scoreReporter.value = score;  
		[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {  
			if (error != nil){  
                NSLog([error description]);  
				// handle the reporting error  
			}  
              
            NSLog(@"REPORTED: ");  
            ofLog(OF_LOG_NOTICE, "SCORE: " + ofToString(scoreReporter.value));  
            ofLog(OF_LOG_NOTICE, "ID: " + ofxNSStringToString(scoreReporter.category));  
		}];  
	}  
}  
void ofxGameCenter::reportAchievement(string _ach, float percent){  
	if (isAvailable()) {  
		NSString * identifier = [NSString stringWithUTF8String: _ach.c_str()];  
        GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];  
        if (achievement)  
        {  
            achievement.percentComplete = percent;  
            [achievement reportAchievementWithCompletionHandler:^(NSError *error)  
             {  
                 if (error != nil){  
                 }  
             }];  
        }  
	}  
}  

I’ve left in a bunch of comments in “leaderboardViewControllerDidFinish” to show some of the various (failed) attempts to solve this issue. Anyone know what’s missing here?

Thanks in advance!
Matt

The incomparable prisonJohn came up with the solution. Looks like I was making the LeaderBoard a subview of another view, then dismissing the leaderboard but not the subview. Code below:

ofxGameCenter.h:

  
#ifndef GAME_CENTER_H_  
#define GAME_CENTER_H_  
  
  
#include "ofMain.h"  
#include <GameKit/GameKit.h>  
  
#import <UIKit/UIKit.h>  
#import "GameCenterManager.h"  
  
//#import "GKLeaderboardViewController-LandscapeOnly.h"  
  
//@interface GKLeaderboardViewController(LandscapeOnly)  
//  
//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;  
//  
//@end  
  
@interface GKLeaderboardViewController (LandscapeOnly)  
- (BOOL)interfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;  
@end  
  
@interface Game_CenterViewController : NSObject <UIActionSheetDelegate, GKLeaderboardViewControllerDelegate, GKAchievementViewControllerDelegate, GameCenterManagerDelegate>  
  
@property(nonatomic, retain) NSMutableDictionary *achievementsDictionary;  
  
//- (void) loadAchievements;  
//- (GKAchievement*) getAchievementForIdentifier: (NSString*) identifier;  
//- (void) reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent;  
- (void) show:(NSString*)category;  
  
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;  
  
@end  
  
class ofxGameCenter {  
public:  
	ofxGameCenter();  
	~ ofxGameCenter();  
	  
    bool init;  
      
	void setup();  
      
	bool isAvailable();  
	void authenticateLocalPlayer();  
      
	void reportScore(string _category, int score);  
	void reportAchievement(string _ach, float percent);  
      
	bool isAuthenticated;  
      
    void show(string _category);  
      
    Game_CenterViewController* gameCenterViewController;  
};  
  
#endif /*GAME_CENTER_H_*/  

ofxGameCenter.mm

  
#import <Foundation/Foundation.h>  
  
#include "ofxGameCenter.h"  
#include "ofxiPhoneExtras.h"  
#include "ofMain.h"  
#include "testApp.h"  
  
@implementation Game_CenterViewController  
  
-(UIDeviceOrientation)interfaceOrientation  
{  
    UIDeviceOrientation orient = UIDeviceOrientationLandscapeRight;  
      
    if([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationLandscapeLeft){  
        orient = UIDeviceOrientationLandscapeLeft;  
    }  
      
    return orient;  
}  
  
- (void) show:(NSString*)category  
{  
      
	GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];  
      
    //  
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;  
    //  
    if(orientation == UIInterfaceOrientationLandscapeLeft){  
        leaderboardController.view.transform = CGAffineTransformMakeRotation (90 / 180 * M_PI);  
    } else {  
        leaderboardController.view.transform = CGAffineTransformMakeRotation (0 / 180 * M_PI);  
    }  
      
	if (leaderboardController != NULL)  
	{  
		leaderboardController.category = category;  
		leaderboardController.timeScope = GKLeaderboardTimeScopeWeek;  
		leaderboardController.leaderboardDelegate = self;  
        //        leaderboardController ;  
          
        //        [ofxiPhoneGetUIWindow() addSubview:self.view];  
          
        [ofxiPhoneGetViewController() presentViewController:leaderboardController  
                                                   animated:YES  
                                                 completion:nil];  
          
        //        [self presentModalViewController: leaderboardController animated: YES ];  
          
	}  
}  
  
  
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController  
{  
    //    [self dismissViewControllerAnimated:NO completion:nil];  
    [ofxiPhoneGetViewController() dismissViewControllerAnimated:YES  
                                                     completion:^{  
                                                         [viewController release];  
                                                         [ofxiPhoneGetUIWindow() becomeFirstResponder];  
                                                     }];  
      
      
      
    //    [self dismissViewControllerAnimated:YES completion:nil];  
    //    [self.view removeFromSuperview];  
    //  
    ////	ofRegisterTouchEvents(ofGetAppPtr());  
    //  
    ////    testApp* app = ((testApp*)ofGetAppPtr());  
    //    //app->changeScreen(app->gameScreen);  
    //  
    ////    app->setup();  
    //  
    //    //ofRegisterTouchEvents(<#ListenerClass *listener#>);  
}  
  
@end  
  
ofxGameCenter::ofxGameCenter(){  
    init = false;  
    gameCenterViewController = [[Game_CenterViewController alloc] init];  
}  
  
ofxGameCenter::~ ofxGameCenter(){  
}  
  
void ofxGameCenter::setup(){  
	isAuthenticated=false;  
	if (isAvailable()) {  
		authenticateLocalPlayer();  
        ofLog(OF_LOG_NOTICE, ofToString(isAuthenticated));  
	}  
    init = true;  
}  
  
bool ofxGameCenter::isAvailable(){  
    Class gcClass = (NSClassFromString(@"GKLocalPlayer"));  
    // The device must be running running iOS 4.1 or later.  
    NSString *reqSysVer = @"4.1";  
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];  
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);  
    return (gcClass && osVersionSupported);  
}  
  
  
  
void ofxGameCenter::authenticateLocalPlayer(){  
    [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {  
		if (error == nil)  
        {  
			// Insert code here to handle a successful authentication.  
			isAuthenticated=true;  
		}  
		else  
		{  
			// Your application can process the error parameter to report the error to the player.  
            NSLog([error description]);  
			isAuthenticated=false;  
		}  
	}];  
}  
  
void ofxGameCenter::show(string _category){  
      
    //    if(gameCenterViewController == nil){  
    //        gameCenterViewController = [[Game_CenterViewController alloc] init];  
    //    }  
      
    if(isAuthenticated)  
        [gameCenterViewController show:ofxStringToNSString(_category)];  
    else {  
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Game Center Unavailable"  
                                                            message:@"Please ensure you have a Game Center account setup and have internet connectivity."  
                                                           delegate:ofxiPhoneGetUIWindow() cancelButtonTitle:@"OK"otherButtonTitles:nil];  
        [alertView show];  
    }  
}  
  
void ofxGameCenter::reportScore(string _category, int _score){  
	if (isAvailable()) {  
		int64_t score = static_cast<int64_t>(_score);  
		NSString * category = ofxStringToNSString(_category);  
		GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];  
		scoreReporter.value = score;  
		[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {  
			if (error != nil){  
                NSLog([error description]);  
				// handle the reporting error  
			}  
              
            NSLog(@"REPORTED: ");  
            ofLog(OF_LOG_NOTICE, "SCORE: " + ofToString(scoreReporter.value));  
            ofLog(OF_LOG_NOTICE, "ID: " + ofxNSStringToString(scoreReporter.category));  
		}];  
	}  
}  
void ofxGameCenter::reportAchievement(string _ach, float percent){  
	if (isAvailable()) {  
		NSString * identifier = [NSString stringWithUTF8String: _ach.c_str()];  
        GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];  
        if (achievement)  
        {  
            achievement.percentComplete = percent;  
            [achievement reportAchievementWithCompletionHandler:^(NSError *error)  
             {  
                 if (error != nil){  
                 }  
             }];  
        }  
	}  
}