didChangeState matchstarted - match

help ! all,
When "Match started! Expected Player Count:1"
but can not call "didChangeState"
my code is :
===============================
-(IBAction)play{
GKMatchRequest *request = [[GKMatchRequest alloc]init];
request.minPlayers = min;
request.maxPlayers = max;
mmvc = [[GKMatchmakerViewController alloc]initWithMatchRequest:request];
mmvc.matchmakerDelegate =self;
[self presentModalViewController:mmvc animated:YES];
}
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match {
[self dismissModalViewControllerAnimated:YES];
self.myMatch = match;
self.myMatch.delegate = self;
NSLog(#"Match started! Expected Player Count:%d %#",match.expectedPlayerCount, match.playerIDs);
}
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state
{ NSLog(#"OK!"); }
====================
it can not give me "OK!"
but i can see "Match started! Expected Player Count:1"
Could you help me?

I had the same issue and I think the problem was in apple servers.
Because I did nothing and after half an hour past it started to work again.
Without any troubles.

Related

iOS GameCenter Matchmaker not working

I’m trying to make a custom matchmakingview using a matchmaker. The code below is used to find a match.
When i run this on two different devices with different Game Center accounts, both will get a match but none will connect to the match. They will just get stuck in the while loop in infinity and never get out. Have i missed something, do you need to call something to actually connect to the match?
- (void) findMatch{
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = nil;
NSLog(#"Start searching!");
[matchmaker findMatchForRequest:request
withCompletionHandler:^(GKMatch *match, NSError *error)
{
if (error) {
// Print the error
NSLog(#"%#", error.localizedDescription);
}
else if (match != nil)
{
curMatch = match;
curMatch.delegate = self;
NSLog(#"Expected: %i", match.expectedPlayerCount);
while (match.expectedPlayerCount != 0){
NSLog(#"PLayers: %i", curMatch.playerIDs.count);
}
NSLog(#"Start match!");
}
}];
You should not be using a while loop to wait for expectedPlayerCount to reach 0, instead implement the GKMatchDelegate method:
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state {
if (!self.matchStarted && match.expectedPlayerCount == 0) {
self.matchStarted = YES;
//Now you should start your match.
}
}

EXC_BAD_ACCESS (code=2, address=0x3)

Hi I am getting the following error from this code:
EXC_BAD_ACCESS (code=2, address=0x3)
When I press play I am basically looped back CFRetain.
I can't figure out what's the problem with this.
XCode is pointing specifically at this line:
[NSDictionary dictionaryWithObjectsAndKeys:[self getCorrectName:oldController], #"ViewController", sec, #"duration", nil];
I checked both values of the dictionary and they seem to check out.
- (NSString *)getCorrectName:(UIViewController *)viewController {
if (viewController.class == [UINavigationController class]) {
UIViewController *vc = [viewController.childViewControllers objectAtIndex:0];
return NSStringFromClass(vc.class);
} else {
return NSStringFromClass(viewController.class);
}
}
# pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tbController shouldSelectViewController:(UIViewController *)viewController {
// Tracking which controller will be clicked
[[Mixpanel sharedInstance] track:#"tab_clicked"
properties:[NSDictionary dictionaryWithObjectsAndKeys:
[self getCorrectName:viewController], #"ViewController", nil]];
// Tracking how long was spent on the last controller
UIViewController *oldController = [self.childViewControllers objectAtIndex:self.selectedIndex];
if (viewController != oldController) {
NSTimeInterval secondsBetween = [self.start timeIntervalSinceNow];
NSInteger sec = -1 * (secondsBetween + 0.5); // round up and down
if (sec > 0) {
NSLog(#"Changing controllers from %#, %d seconds", [self getCorrectName:oldController], sec);
[NSDictionary dictionaryWithObjectsAndKeys:[self getCorrectName:oldController], #"ViewController", sec, #"duration", nil];
/*
[[Mixpanel sharedInstance] track:#"tab_viewed"
properties:[NSDictionary dictionaryWithObjectsAndKeys:
[self getCorrectName:oldController], #"ViewController", sec, #"duration",
nil]];
*/
self.start = [NSDate date];
}
}
return YES;
}
Any ideas?
NSInteger is NOT a class.
Use NSNumber

Issues regarding gamecenter+ios

I am not knowing anything about gamecentre.I am working on the COCO application
I had gone through gamecenter guide and get able to integrate the game center in my application.
1)
Now the i am able enter the score in the gamecentre through my application.
By doing this:-
- (void) reportScore: (int64_t) score forCategory: (NSString*) category
{
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler: ^(NSError *error)
{
[self callDelegateOnMainThread: #selector(scoreReported:) withArg: NULL error: error];
}];
}
I is working properly uptil yesterday.But now if i tried to update the score it is not updating.Please any one know the reason y the score is not updating in the game center
I had tried to search out but not get anything.
2)I want to send the current latitude & longitude of one player to another.
I came to know that it is possible by GKMATCH
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 1;
request.maxPlayers = 1;
[[GKMatchmaker sharedMatchmaker] findMatchForRequest:request
withCompletionHandler:^(GKMatch *match, NSError *error) {
if (error || !match) {
// handle the error
}
else if (match != nil){
// match found
}}];
/*GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 1;
request.maxPlayers = 1;
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request];
mmvc.matchmakerDelegate = self;
currentMatch.delegate=self;
[self presentModalViewController:mmvc animated:YES];*/
//[mmvc release];
}
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state {
switch (state){
case GKPlayerStateConnected:
if (match.expectedPlayerCount == 0) {
// start the match
}
break;
case GKPlayerStateDisconnected:
// remove the player from the match, notify other players, etc
break;
}
}
- (void)matchmakerViewControllerWasCancelled:(GKMatchmakerViewController *)viewController{}
// Matchmaking has failed with an error
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFailWithError:(NSError *)error{
}
// A peer-to-peer match has been found, the game should start
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match{
}
// Players have been found for a server-hosted game, the game should start
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindPlayers:(NSArray *)playerIDs{
}
// An invited player has accepted a hosted invite. Apps should connect through the hosting server and then update the player's connected state (using setConnected:forHostedPlayer:)
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didReceiveAcceptFromHostedPlayer:(NSString *)playerID {
}
- (void) sendPosition
{
NSError *error;
NSString* str = #"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
// NSData *packet = [NSData dataWithBytes:&msg length:sizeof(PositionPacket)];
[currentMatch sendDataToAllPlayers: data withDataMode: GKMatchSendDataUnreliable error:&error];
if (error != nil)
{
// Handle the erro.r
}
}
But i dont know why but delegate method of GKMatchmakerViewController is not called i had set the delegate but still and even i ama not able to get GKMATCH (here currentMatch).
So due to this
[currentMatch sendDataToAllPlayers: data withDataMode: GKMatchSendDataUnreliable error:&error];
this method is not working.
as CurrentMatch refrence is 0X0.
Please help me out.How do i send and recive data.
M i doing anything wrong?OR is there any thing else to be done.
Please help me.
Thanks

While using next and previous functionality in AudioStreamer, two or more songs starts together

Here is the code for next and previous button
- (IBAction)nextPressed:(id)sender
{
Previousbutton.enabled=YES;
Nextbutton.enabled = NO;
NSLog(#"TOTAL TRACK:%d",TotalTrackCount);
// TrackCount = TotalTrackCount+1;
if(selectedIndex+1 < TotalTrackCount)
{
[streamer stop];
[self performSelector:#selector(destroyStreamer) ] ;
//player flag
player=#"stop";
int new_index;
new_index=selectedIndex+1;
selectedIndex=new_index;
str_AudioUrl=[array_audioUrl objectAtIndex:selectedIndex];
userPrefs = [NSUserDefaults standardUserDefaults];
[userPrefs setInteger:new_index forKey:#"Index"];
str_AudioUrl=[array_audioUrl objectAtIndex:new_index];
[self performSelector:#selector(createStreamer) ] ;
//[self setButtonImage:[UIImage imageNamed:#"loadingbutton.png"]];
[streamer start];
NSString *newtitle=[array_AudioName objectAtIndex:new_index];
self.title=newtitle;
NSLog(#"selected index next==%d",selectedIndex);
[self performSelector:#selector(enableNext) withObject:nil afterDelay:2.0];
}
else {
Nextbutton.enabled=NO;
}
}
- (void) enableNext
{
Nextbutton.enabled = YES;
}
//it will play next sone from table (next index)
- (IBAction)previousPressed:(id)sender
{
Nextbutton.enabled = YES;
Previousbutton.enabled = NO;
NSLog(#"selected index previous==%d",selectedIndex);
if(selectedIndex >0)
{
[streamer stop];
//player flag
player=#"stop";
[self performSelector:#selector(destroyStreamer) ] ;
int new_index;
new_index=selectedIndex-1;
selectedIndex=new_index;
str_AudioUrl=[array_audioUrl objectAtIndex:selectedIndex];
userPrefs = [NSUserDefaults standardUserDefaults];
[userPrefs setInteger:new_index forKey:#"Index"];
str_AudioUrl=[array_audioUrl objectAtIndex:new_index];
[self performSelector:#selector(createStreamer)] ;
[self setButtonImage:[UIImage imageNamed:#"loadingbutton.png"]];
[streamer start];
NSLog(#"selected index previous2==%d",selectedIndex);
NSString *newtitle=[array_AudioName objectAtIndex:new_index];
self.title=newtitle;
[self performSelector:#selector(enablePrevious) withObject:nil afterDelay:2.0];
}
else {
Previousbutton.enabled = NO;
}
}
- (void) enablePrevious
{
Previousbutton.enabled = YES;
}
AudioStreamer class has a bug related to Pause during streaming. So a nice post is given on gitHub to solve this problem.

Why can't my devices find each other using this Game Kit connection code?

I'm trying to connect two devices over Bluetooth using Game Kit. The first device is running an application with the following code inside it:
-(void)connect
{
GKPeerPickerController* picker;
picker = [[GKPeerPickerController alloc] init];
picker.delegate = self;
//picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[picker show];
}
- (void)viewDidLoad {
[super viewDidLoad];
peerStatus = kServer;
gamePacketNumber = 0;
currentSession = nil;
gamePeerId = nil;
NSString *uid = [[UIDevice currentDevice] uniqueIdentifier];
gameUniqueID = [uid hash];
[self connect];
}
- (void)peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:(GKPeerPickerConnectionType)type {
// GKSessionModeClient
// GKSessionModeServer
[self.currentSession initWithSessionID:kTankSessionID displayName:nil sessionMode:GKSessionModeClient];
currentSession.available = YES;
currentSession.delegate = self;
}
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session
{
self.gamePeerId= peerID;
//self.currentSession = session;
self.currentSession.delegate = self;
[self.currentSession setDataReceiveHandler: self withContext:nil];
// Remove the picker.
picker.delegate = nil;
[picker dismiss];
[picker autorelease];
// Start your game
}
- (void)peerPickerControllerDidCancel: (GKPeerPickerController *)picker
{
NSLog(#"peerPickerControllerDidCancel");
picker.delegate = nil;
[picker autorelease];
}
- (void)session: (GKSession*)session peer:(NSString*)peerID didChangeState:(GKPeerConnectionState)state
{
NSLog(#"finding and connecting to others sessions");
if(state == GKPeerStateAvailable){
NSLog(#"aviable and ready to conect");
[session connectToPeer:peerID withTimeout:60];
session.available =NO; //put YES si volem multiconexions sino es tancarà la sesio en conectarse 2
}
}
- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID{
NSLog(#"lisetn for others sessions");
NSError *error=nil;
if(amAcceptingConnetions){
if(![session acceptConnectionFromPeer:peerID error:&error]){
//Handler error
}
}else{
[session denyConnectionFromPeer:peerID];
}
}
The second device is running another application that uses the same code as above, only with the following piece changed:
[self.currentSession initWithSessionID:kTankSessionID displayName:nil sessionMode:GKSessionModeServer];
The normal window appears on both to indicate that they are searching for other devices, but the two devices don't recognize each other. What could be wrong here?
You need to allocate currentSession if it's not already being allocated. Perhaps try changing:
[self.currentSession initWithSessionID:kTankSessionID displayName:nil sessionMode:GKSessionModeClient];
To this:
self.currentSession = [[GKSession alloc] initWithSessionID:kTankSessionID displayName:nil sesssionMode:GKSessionModeClient];
You may want to also add a check to see if the session is already allocated, and release it if it is before you create a new one. I noticed some improvements in performance and reliability if you reset the session every time your devices try to connect, instead of reusing the same session.
You have to import PeerPickerController along with GameKit/Gamekit.h also. If you want to know more,Please refer this link.
or
http://www.devx.com/wireless/Article/43502/0/page/1