I am trying to integrate Apple's game center into my application. I can successfully post scores to the leader board, and show the leader board, but the problem comes when I try to dismiss the leader board modal view. I've followed apple's code direction from the Game Kit Programming Guide ([url]http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html[/url]).
My code is as follows for Game Center:
-(BOOL)isGameCenterAvailable{
// Check for presence of GKLocalPlayer class.
BOOL localPlayerClassAvailable = (NSClassFromString(#"GKLocalPlayer")) != nil;
// The device must be running iOS 4.1 or later.
NSString *reqSysVer = #"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
return (localPlayerClassAvailable && osVersionSupported);
}
- (void) authenticateLocalPlayer
{
if([self isGameCenterAvailable]){
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
if (localPlayer.isAuthenticated)
{
// Perform additional tasks for the authenticated player.
}
}];
}
}
- (void) reportScore: (int64_t) score forCategory: (NSString*) category
{
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil)
{
// handle the reporting error
}
}];
}
- (void) showLeaderboard
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil)
{
leaderboardController.leaderboardDelegate = self;
[self presentModalViewController: leaderboardController animated: YES];
}
//[leaderboardController release];
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
if([self modalViewController] != nil){
[self dismissModalViewControllerAnimated:YES];
}
}
-(IBAction)show{
[self showLeaderboard];
}
-(IBAction)submit{
[self reportScore:9 forCategory:kLeaderboardID];
}
Xcode tells me the problem line is [self dismissModalViewControllerAnimated:YES]; it says I'm getting bad access, which I know means I'm trying to access a bad pointer, but I don't see why anything wouldn't be invalid. Self reports that it has a modalviewcontroller. I've tried all sorts of variants, and I'm completely baffled as to why it is giving me errors.
Any help or suggestions would be greatly appreciated.
Thanks in advance!
I just had a very similar issue on my App. I discovered that it was not related to the ModalViewController itself but the view controller displaying it.
If you profile the app using the zombies option in the Profiler, you will be able to see that something is being released that should not be (Most likely a UIImage or UIView). You should be able to track down the function where the zombied object was allocated to find the real object causing the trouble.
I am supposing that the reason the error shows when the ModalViewController is dissmissed is that various view elements are called to redraw or refresh after the dialog goes away and then something gets accessed that was released when it shouldn't have been.
Hope this helps.
Related
So i need to write a QR reader for an iphone application. Usually frameworks like ZXING and ZBAr allow you to download an application and then read the barcode. I want to integrate it within the application itself. So basically i want to be able to tap a button and have it reading the QR code. Is this possible? If so are there any proper documentation on this? I tried using ZXing but when i link binaries libZXingWidget.a is unavailable. Also the documentation is not enough to know how to integrate it within the application. So let me know.
Here's code to setup ZBar, make sure to add the SDK to your project and link your library.
-
(void)viewDidLoad
{
[ZBarReaderView class];
readerView.readerDelegate = self;
readerView.tracksSymbols = NO;
//CHOOSE CAMERA
if (some setting isEqual to CameraRear) {
readerView.device = [self backFacingCameraIfAvailable];
}
else {
readerView.device = [self frontFacingCameraIfAvailable];
}
[self relocateReaderPopover:[self interfaceOrientation]];
[readerView start];
}
-(AVCaptureDevice *)frontFacingCameraIfAvailable
{
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices)
{
if (device.position == AVCaptureDevicePositionFront)
{
captureDevice = device;
break;
}
}
// couldn't find one on the front, so just get the default video device.
if ( ! captureDevice)
{
captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return captureDevice;
}
-(AVCaptureDevice *)backFacingCameraIfAvailable
{
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices)
{
if (device.position == AVCaptureDevicePositionBack)
{
captureDevice = device;
break;
}
}
// couldn't find one on the front, so just get the default video device.
if ( ! captureDevice)
{
captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return captureDevice;
}
-(void)relocateReaderPopover:(UIInterfaceOrientation)toInterfaceOrientation{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
readerView.previewTransform = CGAffineTransformMakeRotation(M_PI_2);
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
readerView.previewTransform = CGAffineTransformMakeRotation(-M_PI_2);
} else if (toInterfaceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
readerView.previewTransform = CGAffineTransformMakeRotation(M_PI);
} else {
readerView.previewTransform = CGAffineTransformIdentity;
}
}
This is a step-by-step tutorial on how to add an integrated QR code reader to your iphone app. Look at this example ZXing project if you need to know how to use it.
I have managed to integrate ZXING into my test project. I have done this a little time ago so I might not remember all the problems that I faced and solved.
Into my workspace folder I have copied ZXing-2.0 folder. Inside I have only left folders: cpp, docs and iphone. To my workspace (containing the test project) I have added ZXingWidget project from the folder ZXing-2.0. This allowed me to add to linking setting libZXingWidget.a.
When I build my test project, XCode detects dependency and builds the widget first and then builds the test project and links it against libZXingWidget.a.
Here's a simple view controller that I have implemented to display inside camera view able to detect QR code.
//
// MyVC.m
//
//
#import "MyVC.h"
#import "QRCodeReader.h"
#interface MyVC () {
ZXingWidgetController *_widController;
}
#end
#implementation MyVC
#synthesize labelResultString;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_widController = [[ZXingWidgetController alloc] initWithDelegate:(id<ZXingDelegate>)self showCancel:YES OneDMode:NO];
QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
//[qrcodeReader release];
_widController.readers = readers;
//[readers release];
//NSBundle *mainBundle = [NSBundle mainBundle];
//_widController.soundToPlay = [NSURL fileURLWithPath:[mainBundle pathForResource:#"beep-beep" ofType:#"aiff"] isDirectory:NO];
_widController.overlayView.displayedMessage = #"";
_widController.wantsFullScreenLayout = NO;
//[self presentModalViewController:_widController animated:NO];
_widController.view.frame = CGRectMake(10, 10, 300, 300);//self.view.frame;
_widController.view.autoresizingMask = UIViewAutoresizingNone;
_widController.overlayView.frame = CGRectMake(0, 0, 300, 300);
_widController.overlayView.cropRect = CGRectMake(20, 20, 260, 260);
[self.view addSubview:_widController.view];
//[_widController release];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.labelResultString = nil;
}
- (void)dealloc {
self.labelResultString = nil;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_widController viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_widController viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_widController viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[_widController viewDidDisappear:animated];
}
#pragma mark - ZXingDelegateMethods
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
self.labelResultString.text = result;
}
- (void)zxingControllerDidCancel:(ZXingWidgetController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
#end
Once you have ZXing set up correctly the code to get a QR reader is literally this easy:
Make sure to import these two guys for QR:
#import "ZXingWidgetController.h"
#import "QRCodeReader.h"
Then in your controller you will set up the code reader as follows:
ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];
QRCodeReader *qrcodeReader = [[QRCodeReader alloc] init];
NSSet *readers = [[NSSet alloc] initWithObjects:qrcodeReader,nil];
widController.readers = readers;
[self presentModalViewController:widController animated:YES];
You basically set up the controller that handles the code reading (ZXingWidgetController) and then give it a set of all the types of code readers you want (here I just used QRCodeReader.) Lastly, you just present it as a modal view controller.
Then you will need to implement the <ZXingDelegate> and the following delegate functions:
- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result
{
[self dismissModalViewControllerAnimated:YES];
NSLog(#"%#",result); //Simple NSString result.
}
- (void)zxingControllerDidCancel:(ZXingWidgetController*)controller
{
[self dismissModalViewControllerAnimated:YES];
NSLog(#"Cancelled");
}
The hardest part is just setting it up. I found this tutorial to be the most helpful (Sorry that I am using a link): How to install ZXing in Xcode 4 The comments are also helpful if you have any problems.
I'm trying to implement invitations with Game Center and there's one thing that i don't understand. Ok, i've sent an invitation from one device to another. Then i have an UIAlertView on receiver which asks me i would like to accept or decline the invitation. when i accept it it is handled like this:
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite)
{
// Insert application-specific code here to clean up any games in progress.
if (acceptedInvite)
{
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
}
else if (playersToInvite)
{
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 4;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
}
};
Well, that's great, but what next? the sender device is obviously waiting for some standard type of response, cause it also shows an alert telling me that there's some invitations not answered yet if i tap "Play now".
So how do i accept an invitation? What kind of data (and how) should i send back? And what exactly should i do on the receiver's side? Should game start instantly after tapping "Accept" or i should dismiss the AlertView first and then tap "Play now"?
Ray Wenderlich's tutorial says that i should choose second way but when dismiss the alert and tap "Play now" it turns out the sender device is still waiting for response and is not aware that i have already accepted the invitation. if i tap "Play now" at this moment then, as i said above, it shows an alert which says that the application is waiting for the response. So if you've ever done that then please explain me what should i do. Thanks!
I register for invites as soon as the game is loaded and call the
delegate when an invite is received
So inviteReceived calls the match maker for dismissing the game
center controller and creating the match
And finally, when the match is being found, the method
connectionStatusChanged takes care of presenting all the game's
views and players and stuff
Here is the code:
I register for invites as soon as the game is loaded and call the delegate when an invite is received:
- (void)registerInvites {
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
self.pendingInvite = acceptedInvite;
self.pendingPlayersToInvite = playersToInvite;
[delegate inviteReceived];
};
}
So inviteReceived calls the match maker for dismissing the game center controller and creating the match:
- (void)inviteReceived {
[[GCMultiplayerHelper sharedInstance] findMatchWithMinPlayers:2 maxPlayers:2 viewController:(UIViewController*)[self.superview nextResponder] delegate:self];
}
- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers viewController:(UIViewController *)viewController delegate:(id<GCMultiplayerHelperDelegate>)theDelegate {
if (!gameCenterAvailable) return;
matchStarted = NO;
self.match = nil;
self.presentingViewController = viewController;
delegate = theDelegate;
[presentingViewController dismissModalViewControllerAnimated:YES];
GKMatchmakerViewController *mmvc;
if (pendingInvite != nil) {
mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:pendingInvite] autorelease];
} else {
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
request.playersToInvite = pendingPlayersToInvite;
mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
}
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
self.pendingInvite = nil;
self.pendingPlayersToInvite = nil;
}
And finally, when the match is been found, the method connectionStatusChanged takes care of presenting all the game's views, players and starting the match:
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)theMatch
{
self.match = theMatch;
match.delegate = self;
if (!matchStarted && match.expectedPlayerCount == 0) {
NSLog(#"Ready to start match! - didFindMatch");
[presentingViewController dismissModalViewControllerAnimated:YES];
[self.delegate connectionStatusChanged:CONNECTIONSUCCESS];
}
}
I've successfully implemented an online game center match following Ray's tutorials. The answer to you question is: You don't have to send anything to the inviting device. When you call the line:GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];, the matchMakerVController handles the connection . However, you should write an invitation handler ASAP, preferably in the authentication changed method. See mine:
-(void) authenticationChanged {
if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) {
NSLog(#"Authentication changed: player authenticated.");
userAuthenticated = TRUE;
[self sendUnsentScores];
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite){
NSLog(#"Invite");
if([AppDelegate mainMenuController].presentedViewController!=nil) {
[[AppDelegate mainMenuController] dismissViewControllerAnimated:NO completion:^{
}];
} // if we're not on the main menu, or another game is going on.
// this would be easier to do if you were using a navigation controller
// where you'd just push the multiplayer menu etc.
self.pendingInvite = acceptedInvite;
self.pendingPlayersToInvite = playersToInvite;
[[AppDelegate mainMenuController] presentViewController:[AppDelegate mainMenuController].multiGameMenu animated:NO completion:^{ // push the multiplayer menu
[[AppDelegate mainMenuController].multiGameMenu duel:nil];
}];
};
}
and here is the duel method if you're interested. Very messy code but deal with it :)
- (IBAction)duel:(id)sender {
NSLog(#"duel");
if (presentingMenu.multiGameController==nil || presentingMenu.multiGame.gameInProgress==NO) {
presentingMenu.multiGame=nil;
presentingMenu.multiGameController=nil;
MultiViewController *mvc = [[MultiViewController alloc] init]; //create game VC
presentingMenu.multiGameController = mvc; //presenting menu is just the main menu VC
// it holds this menu, and the game
// objects.
}
if (presentingMenu.multiGame == nil) {
presentingMenu.multiGame = [[MultiGame alloc] // similarly create the game object
initWithViewController:presentingMenu.multiGameController];
//they both have pointers to each other (A loose, bad MVC).
presentingMenu.multiGameController.game = presentingMenu.multiGame;
[presentingMenu.multiGame startGame];
}
if (presentingMenu.multiGameController.gameState==0) { //new game
presentingMenu.multiGameController.game = presentingMenu.multiGame;
[[GCHelper sharedInstance] findMatchWithMinPlayers:2 maxPlayers:2 viewController:self delegate:presentingMenu.multiGame]; // the GC magic happens here - it know about the invite.
} else {
[self presentViewController:presentingMenu.multiGameController animated:YES completion:^{
}];
}
}
inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) is deprecated now. See the new way to register for invite notifications.
GKMatchMaker invite handler deprecated
I am trying to add High Scores in my game, and I don't know why i can't present the Leader boards. I can connect the user and send data. But can't see it.
My Game its in Cocos2d. But my MENU isn't when the user click on Play CCDirector Start
My main windows structure its:
-Navigation Contoller
- Menu -Ui View controller
- Navigation Item
I try to do it like a navigation and push it and crash. Also add it as subview. And crash. My leader boards code
- (IBAction)showLeader:(id)sender {
if ([self isGameCenterAvailable]) {
GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
if (leaderboardController != nil) {
leaderboardController.leaderboardDelegate = self;
//[self presentModalViewController:leaderboardController animated:YES];
[self.navigationController pushViewController:leaderboardController animated:YES];
}
}
}
-(BOOL)isGameCenterAvailable {
// Check for presence of GKLocalPlayer API.
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);
}
What could i do?
And another question. Could i put my own effect to present the leader bords scores? Or apple will reject it?
I am trying to do a small framework. So i don't need to codify allot in the Menu Game etc..
Thanks :D
I found how to do this.
menu2Game = [[Menu2Game alloc] initWithNibName:#"Menu2Game" bundle:nil];
menu2Game.wantsFullScreenLayout = YES;
[self.view addSubview:menu2Game.view];
[menu2Game presentModalViewController:leaderboardController animated:YES];
menu2Game its a clean View Controller. Maybe I can create a view controller temporally here. But I need to save it somewhere so i could dismiss it.
It work. But i am trying to do it in a landscape
Im having a strange problem that only occurs on the iPad version of my game. When I bring up the GameCenter leaderboard, it appears like normal. No issues there. But when you tap the close button the leader board disappears and the cocos2d scene goes black. No errors, nothing. Nothing I do can bring the screen back. Ive tried resuming the director and even tried some solutions on a similar thread.
Here is my code in the cocos2d scene. GCController is just a subclass of the RootViewController, nothing special:
tempVC = [[GCController alloc] init];
GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
if (leaderboardController != nil)
{
leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardController.category = [NSString stringWithFormat:#"%#%#", [self cleanString:selectedSong], #"TotalHD"];
leaderboardController.leaderboardDelegate = self;
[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];
[tempVC presentModalViewController:leaderboardController animated:YES];
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[tempVC dismissModalViewControllerAnimated:YES];
[tempVC.view.superview removeFromSuperview];
[tempVC release];
}
Anyone have any ideas? If I can provide any more information let me know. Thanks.
You are not providing enough code. I see no fault in the code you are showing at the moment. Your most likely cause of a black screen (depending on how you do things) is that timers have been interrupted and you may need to reset them with
- (void) onEnter;
or
- (void) onEnterTransitionDidFinish;
If that isn't it then post more code and I will check back. We need to see when the view is presented and what it should return to.
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[tempVC dismissModalViewControllerAnimated:YES];
**[tempVC.view removeFromSuperview];**
}
I want to add Game center for my project but now I get some stuck and can't find any solution for my problem by Google :(
When I call method showleaderboard in my project, leaderboard appear and success to load my score in gamecenter but it don't receive touch (this screen look like freeze).
This is my code:
-(void) ShowLeaderBoardCategory:(NSString *)my_category
{
GKLeaderboardViewController * leaderboardController = [[GKLeaderboardViewController alloc] init];
if(leaderboardController != nil)
{
leaderboardController.category = my_category;
leaderboardController.leaderboardDelegate = mySubView;
[mySubView presentModalViewController: leaderboardController animated: YES];
[glView addSubview:mySubView.view];
}
[leaderboardController release];
}
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[mySubView dismissModalViewControllerAnimated:YES];
[mySubView release];
[viewController.view removeFromSuperview];
[viewController release];
}
mySubView is interface I define:
#interface MyUIView : UIViewController<GKLeaderboardViewControllerDelegate>
......
and used it:
MyUIView *mySubView;
Please, tell me what wrong in my code? :((
What reason can there be to stop receiving cocos2d events?
Thanks for reading and hope your hint.
[viewController.view.superview removeFromSuperview];
worked for me.
The problem is in your removal method: where viewController refers to the leaderboard:
[mySubView dismissModalViewControllerAnimated:YES]; //this removes the modally presnted leaderboard.
[mySubView removeFromSuperview]; //this should show up the glView