Platforms to show: All Mac Windows Linux Cross-Platform
/MacFrameworks/Game Center/GameKit Authenticate Player
Required plugins for this example: MBS MacFrameworks Plugin, MBS MacBase Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/Game Center/GameKit Authenticate Player
This example is the version from Tue, 16th Sep 2019.
Project "GameKit Authenticate Player.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Clear"
Const kFileQuit = "Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
//ADD your own external method to confirm there is a valid
//Monkeybread Xojo Plugins license
//HERE
//Check if game center available in this OS X version
Dim major, minor, bug As Integer
If System.Gestalt("sys1", major) Then
If System.Gestalt("sys2", minor) Then
If System.Gestalt("sys3", bug) Then
if major>=10 AND minor>=8 then
//Game Center introduced in OS X 10.8 (Mountain Lion)
pBoolGameCenterIsAvailable=true
if bug>=2 then
//Some new features were added in OS X 10.8.2
pBoolVersion1082=true
end if
else
Window1.Log "Max OS X v" + Str(major) + "." + Str(minor) + "." + Str(bug)+" does not "_
+"support Game Center. Please upgrade to run this game."
end if
End If
End If
End If
End EventHandler
Property pBoolGameCenterIsAvailable As Boolean
Property pBoolVersion1082 As Boolean
End Class
Class Window1 Inherits Window
Control Listbox1 Inherits Listbox
ControlInstance Listbox1 Inherits Listbox
End Control
Control bbAuthenticate1082 Inherits BevelButton
ControlInstance bbAuthenticate1082 Inherits BevelButton
EventHandler Sub Action()
//This authentication method not implemented until OS X 10.8.2
if NOT App.pBoolVersion1082 then
Log("This OS X system version does not support this authentication method.")
else
//Your game will pick one authentication method and always use it
//so this test is unecessary in your final project
if pBool108Used then
//But this test app allows use of both methods so this check is used
//because there is some unknown interaction between the two methods
Log("You have already used old authenticate method during this test. "_
+"Restart app to use new authenticate method.")
else
ButtonAction("1082")
end if
end if
End EventHandler
End Control
Control pbAuthenticate108 Inherits PushButton
ControlInstance pbAuthenticate108 Inherits PushButton
EventHandler Sub Action()
//Your game app will pick one authentication method and always use it
//so this flag is unecessary
//But this test app allows use of both authenticate methods so this flag is used
//because there is some unknown interaction between the two methods
pBool108Used=true
ButtonAction("108")
End EventHandler
End Control
EventHandler Sub Open()
//Many Game Center operations are asynchronous
//The class MyGameKitMBS will receive many asynchronous events for your game to use
//Property pGameKit is the location this app uses for receiving those events
pGameKit = new MyGameKitMBS
End EventHandler
Sub AuthenticateLocalPlayer108(localPlayer as GKLocalPlayerMBS)
////////////// Apple's Xcode OS X 10.8 authentication code example
'- (void) authenticateLocalPlayer
' {
' GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
' [localPlayer authenticateWithCompletionHandler:^(NSError *error) {
' if (localPlayer.isAuthenticated)
' {
// Player was successfully authenticated.
// Perform additional tasks for the authenticated player.
' }
' }];
' }
////////////////
//Shared instance of the local player provided as method parameter
'localPlayer = GKLocalPlayerMBS.localPlayer
//NOTE: This method of authentication is deprecated
localPlayer.authenticate
//balance of code is in MyGameKitMBS.authenticateCompleted event handler
End Sub
Sub AuthenticateLocalPlayer1082(localPlayer as GKLocalPlayerMBS)
//////////////// Apple's Xcode OS X 10.8.2 authenticate code example
'(void) authenticateLocalPlayer
' {
' GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
' localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
' if (viewController != nil)
' {
//showAuthenticationDialogWhenReasonable: is an example method name. Create your own method that displays an authentication view when appropriate for your app.
' [self showAuthenticationDialogWhenReasonable: viewController];
' }
' else if (localPlayer.isAuthenticated)
' {
//authenticatedPlayer: is an example method name. Create your own method that is called after the loacal player is authenticated.
' [self authenticatedPlayer: localPlayer];
' }
' else
' {
' [self disableGameCenter];
' }
' }];
' }
////////////////
//Shared instance of the local player provided as method parameter
'localPlayer = GKLocalPlayerMBS.localPlayer
// old code
'localPlayer.authenticate
// new code
localPlayer.SetAuthenticateHandler
//balance of code is in MyGameKitMBS.authenticateHandler event handler
End Sub
Function BeginAuthenticate(localPlayer as GKLocalPlayerMBS) As Boolean
if localPlayer=nil then
Log("Local player is nil at start of authentication; cannot proceed")
return False
end if
if localPlayer.playerID<>"" then
//Check if local player is already authenticated
if localPlayer.isAuthenticated then
Log("Local player "+localPlayer.playerID+" is already authenticated.")
//if already authenticated no need to proceed
return false
else
Log("Start to authenticate local player "+localPlayer.playerID)
//proceed with authenticating player
return true
end if
else
Log("No local player ID at start of authentication process")
//proceed with authentication even though no player ID at this time
return true
end if
End Function
Sub ButtonAction(buttonName as string)
dim localPlayer as GKLocalPlayerMBS
//Retrieves the shared instance of the local player
localPlayer = GKLocalPlayerMBS.localPlayer
//check and report local player status at beginning of authenticate
Dim OK as boolean =BeginAuthenticate(localPlayer)
if OK then
// run authenticate
if buttonName="1082" then
AuthenticateLocalPlayer1082(localPlayer)
else
AuthenticateLocalPlayer108(localPlayer)
end if
end if
End Sub
Sub Check(localPlayer as GKLocalPlayerMBS)
// isAuthenticated will tell if things went well.
if localPlayer<>nil then
Log "alias: "+localPlayer.alias
Log "isAuthenticated: "+str(localPlayer.isAuthenticated)
Log "isFriend: "+str(localPlayer.isFriend)
Log "isUnderage: "+str(localPlayer.isUnderage)
Log "playerID: "+localPlayer.playerID
end if
End Sub
Sub Log(s as string)
System.DebugLog s
Listbox1.AddRow s
End Sub
Property pBool108Used As Boolean
Property pGameKit As MyGameKitMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Cut"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Select All"
End MenuBar
Class MyGameKitMBS Inherits GameKitMBS
EventHandler Sub PlayerAuthenticationDidChange(player as GKPlayerMBS)
'- (void)authenticationChanged {
' if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) {
' NSLog(@"Authentication changed: player authenticated.");
' userAuthenticated = TRUE;
' } else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {
' NSLog(@"Authentication changed: player not authenticated");
' userAuthenticated = FALSE;
' }
'}
////////////////////
Log("playerAuthenticationDidChange event")
if GKLocalPlayerMBS(player).isAuthenticated AND NOT pBoolUserAuthenticated then
pBoolUserAuthenticated=true
elseif NOT GKLocalPlayerMBS(player).isAuthenticated AND pBoolUserAuthenticated then
pBoolUserAuthenticated=false
end if
End EventHandler
EventHandler Sub authenticateCompleted(localPlayer as GKLocalPlayerMBS, error as NSErrorMBS, tag as variant)
// here we get result from authentication method 10.8
// isAuthenticated will tell if things went well.
// Also error gives details
if localPlayer <>nil then
if localPlayer.isAuthenticated then
Log("authenticateCompleted event. Method 10.8 authenticated local player")
else
Log("authenticateCompleted event. But local player is NOT authenticated")
end if
//report on local player parameters
window1.Check(localPlayer)
else
Log("authenticateCompleted event. But local player still nil")
end if
if error<>Nil then
Log "authenticateCompleted event. Error msg: "+error.localizedDescription
end if
End EventHandler
EventHandler Sub authenticateHandler(LocalPlayer as GKLocalPlayerMBS, viewController as NSViewControllerMBS, error as NSErrorMBS, tag as variant, viewControllerHandle as integer)
//////////////// Apple's Xcode OS X 10.8.2 authenticate code example
'(void) authenticateLocalPlayer
' {
' GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
' localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
' if (viewController != nil)
' {
//showAuthenticationDialogWhenReasonable: is an example method name. Create your own method that displays an authentication view when appropriate for your app.
' [self showAuthenticationDialogWhenReasonable: viewController];
' }
' else if (localPlayer.isAuthenticated)
' {
//authenticatedPlayer: is an example method name. Create your own method that is called after the local player is authenticated.
' [self authenticatedPlayer: localPlayer];
' }
' else
' {
' [self disableGameCenter];
' }
' }];
' }
////////////////
dim dialogController as GKDialogControllerMBS
Log("authenticateHandler started")
if (viewController<>nil) then
//this should indicate that there is no localPlayer
//dialog allows player to sign in
Log("authenticateHandler asking player to sign in")
if dialogController = nil then
dialogController = new GKDialogControllerMBS
end if
call dialogController.presentViewController viewController
elseif LocalPlayer.isAuthenticated then
Log("authenticateHandler reports authenticated by new 10.8.2 method")
window1.Check(LocalPlayer)
end if
if error<>nil then
Log("authenticateHandler found error: "+error.LocalizedDescription)
end if
End EventHandler
Sub Log(s as string)
System.DebugLog s
window1.Listbox1.AddRow s
End Sub
Property pBoolUserAuthenticated As Boolean
End Class
End Project
The items on this page are in the following plugins: MBS MacFrameworks Plugin.