I have a browser webgame
ported to iOS using UIWebView
(storyboard, not xib). The app is already published and works relatively well. My challenge now is to implement GameCenter
properly so I can get the local player's playerID and then have a trustable unique id of the user. That's the only feature I really need from GameCenter
.
The GameCenter
functionality is actually implemented and working well, but for a reason I still couldn't understand I can't make the setAuthenticateHandler, located in a different class file, to access the UIebView
so I can load an URL passing the playerId (among other parameters) via POST.
The WebViewController.m
has a method who loads an URL in the UIWebView
. The setAuthenticateHandler
do call this method, but the UIWebView
is null and of course it's not possible to load the URL. If I call the method inside WebViewController.m
the URL is loaded correctly.
I was using ARC and then changed to MRC according to https://stackoverflow.com/a/14613361/3063226, but the behavior didn't change, the UIWebView
is still null when GameCenter
setAuthenticateHandler
calls the Load_URL
method.
Any help is very welcome. I have tried and researched a LOT for days before coming here to ask a question, I'm stuck!
GCHelper.m
- (void)authenticateLocalUser {
if (!gameCenterAvailable) return;
NSLog(@"Authenticating local user...");
if (![GKLocalPlayer localPlayer].isAuthenticated) {
[[GKLocalPlayer localPlayer] setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
if (!error && [GKLocalPlayer localPlayer].playerID != nil)
{
NSLog(@"Player logged in GameCenter!");
NSLog([NSString stringWithFormat: @"1111 (GCHelper) playerID: [%@]", [GKLocalPlayer localPlayer].playerID);
// --
WebViewController *yourClassInstance = [[WebViewController alloc] init];
[yourClassInstance Load_URL:true]; // this is not working as expected!!
// -
}
else
{
NSLog(error.localizedDescription);
}
})];
} else {
NSLog(@"Already authenticated!");
}
}
WebViewController.h
#import <UIKit/UIKit.h>
@interface WebViewController : UIViewController <UIWebViewDelegate>
-(void) Load_URL:(BOOL)Nativa;
@property (strong, nonatomic) IBOutlet UIWebView *PagPrincipal;
@end
WebViewController.m
#import "WebViewController.h"
#import "NSData+AESCrypt.h"
#import "NSString+AESCrypt.h"
#import "NSString+URL_Encode.h"
#import "GCHelper.h"
@interface WebViewController ()
-(void) Load_URL:(BOOL)Nativa;
@end
-(void) Load_URL :(BOOL)Nativa
{
NSURL *url = [NSURL URLWithString: @"http://www.webgameurl.com"];
NSString *body = [NSString stringWithFormat: @"iac=%@", [self URL_Codigo :Nativa :vip_aux]];
NSMutableURLRequest *requestObj = [[NSMutableURLRequest alloc]initWithURL: url];
[requestObj setHTTPMethod: @"POST"];
[requestObj setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
// HERE!! This returns null when called from GCHelper.m, but is ok when called from WebViewController.m !!
NSLog([NSString stringWithFormat:@"Webview >>> >>> >>> [%@]", _PagPrincipal.description]);
_PagPrincipal.delegate = self;
[_PagPrincipal loadRequest:requestObj];
This is it. The _PagPrincipal
is the UIWebView
itself, it's a storyboard. I'm not an iOS Objective-C specialist, so for sure there are things I just don't master. Using Xcode 6.1.1, the app is designed for iOS8+. Testing in a real iPad Mini 1 (non-retina) and iPhone 5.