Game Center multiplayer over 3G/WWAN - getLocalConnectionData timed out - iphone

I have a multiplayer game that uses GameCenter for networking. Network games over wifi using GKMatch work perfectly, but over 3G they never connect. My
-[GKMatchmaker findMatchForRequest:
withCompletionHandler:]
completion handler block is invoked with an error code 503, which isn't a GKErrorDomainCode according to that header. Instead it appears to be an HTTP error code.
Here's my code:
//GKLocalPlayer is already authenticated at this point
_matchRequest = [[[GKMatchRequest alloc] init] autorelease];
[_matchRequest setMinPlayers: 2];
[_matchRequest setMaxPlayers: 2];
GKMatchmaker *matchmaker = [GKMatchmaker sharedMatchmaker];
[matchmaker findMatchForRequest: _matchRequest
withCompletionHandler:
^(GKMatch *match, NSError *error) {
if (error)
{
if ([error code] != GKErrorCancelled)
{
dispatch_async(dispatch_get_main_queue(), ^{
[[[[UIAlertView alloc] initWithTitle:
NSLocalizedString(#"Can't find match.", #"Alert title for when automatching failed")
message: [error localizedDescription]
delegate: nil
cancelButtonTitle: NSLocalizedString(#"OK", #"Button text for OK button")
otherButtonTitles: nil] autorelease] show];
});
}
else
{
NSLog(#"Canceled :(");
}
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
// do some main-thread stuff specific to my app
_match = [match retain];
[_match setDelegate: self];
});
}
} ];
}
SNJGKLocalPlayerManager just logs the player into GameCenter. Typical output from this would be a UIAlertView saying "The operation could not be completed. getLocalConnectionData timed out" from the line in the first dispatch_async block. If I use NSLog to output the error code, it's 503.
If you've gotten GameCenter multiplayer to work over 3G and don't want to pore over my code, feel free to share your working code and I'll try to find where mine goes wrong! Thanks!

Related

FBWebDialogs presentRequestsDialogModallyWithSession is not working?

This code is not generating notification on friend's end.But sending successully generating no error.I am building a app for Game url on friend to invite them to play.i have written below code to send url.it's working fine and not generating no error but user is not getting any request for App.
//Fbwebdialog generating no error
[FBWebDialogs presentRequestsDialogModallyWithSession:[FBSession activeSession]
message:[NSString stringWithFormat:#"I just smashed friends! Can you beat it?"]
title:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// Case A: Error launching the dialog or sending request.
NSLog(#"Some errorr: %#", [error description]);
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:#"Invitiation Sending Failed" message:#"Unable to send inviation at this Moment, please make sure your are connected with internet" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alrt show];
} else {
if (![resultURL query]) {
return ;
NSLog(#"User canceled request.");
} else {
NSDictionary *params = [self parseURLParams:[resultURL query]];
NSMutableArray *recipientIDs = [[NSMutableArray alloc] init];
for (NSString *paramKey in params){
if ([paramKey hasPrefix:#"to["]){
[recipientIDs addObject:[params objectForKey:paramKey]];
}
}
if ([params objectForKey:#"request"]){
NSLog(#"Request ID: %#", [params objectForKey:#"request"]);
NSArray *requestIDs=[params objectForKey:#"request"];
}
if ([recipientIDs count] > 0){
//[self showMessage:#"Sent request successfully."];
//NSLog(#"Recipient ID(s): %#", recipientIDs);
// punGameViewController *pun=[[punGameViewController alloc]initWithNibName:#"punGameViewController" bundle:nil];
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Successfully send" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alrt show];
// [self.navigationController pushViewController:pun animated:YES];
}
}
}
}];
//end of code here
check it find the reason not generating notification.
I found my answer to this question on a previously answered S/O question. Basically, you need to set the App Store Id and Bundle Id in the Facebook App settings in order for invites to get sent. You can find that info: here
Notifications sent by your app is only received on mobile devices if your app is a game. If your app is not a game, notifications are still delivered to the App Center on the desktop.

Does not receive any "Request" after sending out from iOS Facebook SDK

I'm following these two tutorials in Facebook developer website
https://developers.facebook.com/docs/tutorials/ios-sdk-games/requests/
https://developers.facebook.com/docs/howtos/send-requests-using-ios-sdk/#step2
These are my codes:
[FBWebDialogs presentRequestsDialogModallyWithSession:nil
message:[NSString stringWithFormat:#"I just smashed %u friends! Can you beat it?", score]
title: nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
{
if (error)
{
// Case A: Error launching the dialog or sending request.
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: #"Facebook" message: error.description delegate: nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
else
{
if (result == (FBWebDialogResultDialogCompleted))
{
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if ([urlParams valueForKey: #"request"])
{
// User clicked the Share button
NSLog(#"Send");
}
}
}
}];
Problem is after I send out the request, my friend told me that he doesn't receive any notification/request in FB Notification Center site. Does anyone know why?
These are the things I've done:
I've setup properly in info.plist and the Facebook App.
I'm not using Sandbox mode.
My current application is able to log in and post to wall.
I've publish_actions permission.
I did not received any error or warning.
I found my solution here:
Facebook App Requests aren't shown on iOS devices?
It is because I didn't set the iPhone App ID and iPad App ID
Use following method, it will help you :
-(void)openFacebookAuthentication
{
NSArray *permission = [NSArray arrayWithObjects:kFBEmailPermission,kFBUserPhotosPermission, nil];
FBSession *session = [[FBSession alloc] initWithPermissions:permission];
[FBSession setActiveSession: [[FBSession alloc] initWithPermissions:permission] ];
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
switch (status) {
case FBSessionStateOpen:
[self getMyData];
break;
case FBSessionStateClosedLoginFailed: {
// prefer to keep decls near to their use
// unpack the error code and reason in order to compute cancel bool
NSString *errorCode = [[error userInfo] objectForKey:FBErrorLoginFailedOriginalErrorCode];
NSString *errorReason = [[error userInfo] objectForKey:FBErrorLoginFailedReason];
BOOL userDidCancel = !errorCode && (!errorReason || [errorReason isEqualToString:FBErrorLoginFailedReasonInlineCancelledValue]);
//Added by Rigel Networks July 2, 2013 to solve Facebook Cancel button popup issue
if(error.code == 2 && ![errorReason isEqualToString:#"com.facebook.sdk:UserLoginCancelled"]) {
UIAlertView *errorMessage = [[UIAlertView alloc] initWithTitle:kFBAlertTitle
message:kFBAuthenticationErrorMessage
delegate:nil
cancelButtonTitle:kOk
otherButtonTitles:nil];
[errorMessage performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
errorMessage = nil;
}
}
break;
// presently extension, log-out and invalidation are being implemented in the Facebook class
default:
break; // so we do nothing in response to those state transitions
}
}];
permission = nil;
}

Speed up the response retriever from server in iphone

I am using following code to get results from server
NSString *queryString = #"MyString"
NSString *response = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString] encoding:NSUTF8StringEncoding error:&err];
NSLog(#"%#",response);
if (err != nil)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Error"
message: #"An error has occurred. Kindly check your internet connection"
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[indicator stopAnimating];
}
else
{
//BLABLA
}
The problem with this code is ,If server shows lag and it takes lets say 3 seconds to get this response
NSString *response = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString]
For 3 seconds my iPhone screen is jammed. How can i make it run in background so that it won't slow down or jam the mobile
Regards
What you are doing is sending HTTP request from the main thread. that will jam up the UI as you said. You need to spawn a background thread and make a request to your server, when the response comes back then you need to update the UI from the main thread. This is a common pattern in UI coding.
__block__ NSString *response;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
//your server url and request. data comes back in this background thread
response; = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString] encoding:NSUTF8StringEncoding error:&err];
dispatch_async(dispatch_get_main_queue(), ^{
//update main thread here.
NSLog(#"%#",response);
if (err != nil)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Error"
message: #"An error has occurred."
delegate: self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[indicator stopAnimating];
}
});
});
You can also use performSelectorInBackground:withObject: to spawn a new thread, then the performed selector is responsible for setting up the new thread's autorelease pool, run loop and other configuration details – see "Using NSObject to Spawn a Thread" in Apple's Threading Programming Guide.
You'd probably be better off using Grand Central Dispatch as I posted above. GCD is a newer technology, and is more efficient in terms of memory overhead and lines of code.
You could use ASIHTTPRequest which is my favorite for getting and sending information.

xcode webview error not working

This web view failed error showing the alert popup even when it is loading the website. I believe I need to delay this method in order for it to work.What will be the best method for doing this?
- (void)webView:(UIWebView *)webViewfail didFailLoadWithError:(NSError *)error
{
if([webViewfail isEqual:webview]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Connection Failed" message:#"Check your Internet connection before refreshing."
delegate:webview
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
Here is how I am loading the website
- (void)viewDidLoad
{
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.blabla.com"]]];
}
The problem is most likely an error -999 which generally happens when something from the webpage does not load correctly or a user tries to navigate back while the page is still loading. After some research here's what I found and used to keep the NetworkAlert from poping up every time but still popping up when there is no network.
-(void)webView:(UIWebView *)webBlog didFailLoadWithError:(NSError *)error{
if ([error code] != -999) {
NSLog(#"Could not load the dumb webPage");
//show error alert, etc.
[self showNoNetworkAlert];
}else{
NSLog(#"Could not load the dumb web page...just might blame user!");
}
}
- (void) showNoNetworkAlert{
UIAlertView *baseAlert = [[UIAlertView alloc]
initWithTitle:#"No Network" message:#"A network connection is required. Please verify your network settings and try again."
delegate:nil cancelButtonTitle:nil
otherButtonTitles:#"Dismiss", nil];
[baseAlert show];
}
Hope this helps someone...

NSStreamEventErrorOccurred triggered even if the network is pretty strong signal

I have implemented the NSStream delegate. I have implemented the same as Witap Application
In that, I have implemented handleEvent delegate
- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode{
switch(eventCode) {
case NSStreamEventOpenCompleted:
{
[tcpServer release];
tcpServer = nil;
break;
}
case NSStreamEventHasBytesAvailable:
{
//done my stuff here
}
case NSStreamEventErrorOccurred:
{
UIAlertView *wifiLostAlert = [[UIAlertView alloc] initWithTitle:#"Wifi
connection error" message:#"" delegate:nil cancelButtonTitle:#"Continue" otherButtonTitles:nil];
[wifiLostAlert show];
[wifiLostAlert release];
wifiLostAlert = nil;
}
}
In my client site, they reported an issue as
"Multiple time when we was either trying to send a message to the
learner or synching devices she got the following message: “Wifi
connection error.
Same happend at 2 different networks and it doesnot recover quickly
even if the network is pretty strong signal."
Unfortunately, I am unable to reproduce this issue in my site and it's working fine in another client site too!!.
Any clue's regarding the issue. Any help on this is appreciated.
Thank you.
I would recommend you supply the client with an updated version that displays more information about what error is actually occurring.
You can get more information about the error using:
NSError* error = [stream streamError];
You can find more information about NSError at NSError Class Reference.
Something like this might work in your case:
NSString* errorMessage = [NSString stringWithFormat:#"%# (Code = %d")",
[error localizedDescription],
[error code]];
Then change your UIAlertView to this:
UIAlertView *wifiLostAlert = [[UIAlertView alloc]
initWithTitle:#"Stream Error"
message:errorMessage
delegate:nil
cancelButtonTitle:#"Continue"
otherButtonTitles:nil];
This won't solve the problem but will give both you and your client more information about the root cause is.
For example, you may find the error is "Connection refused." which would point to a problem not with WiFi signal strength but in the server-side software.
Good luck!