HOME > GameCenterのGKLocalPlayerでログイン
GameCenterのGKLocalPlayerでログイン認証
GameCenterにログインします。AppDelegate.swiftにログイン処理を実装すれば、非アクティブから復帰した時も自動で再ログインします。
参考(英語): IOS Game Center GKLocalPlayerListener
サンプル構文
//GameCenterにログインします。
let localPlayer = GKLocalPlayer();
let localPlayer.authenticateHandler = {(viewController, error) -> Void in
if ((viewController) != nil) {
println("ログイン確認処理:失敗-ログイン画面を表示");
self.presentViewController(viewController, animated: true, completion: nil);
}else{
println("ログイン確認処理:成功");
println(error);
if (error == nil){
println("ログイン認証:成功");
}else{
println("ログイン認証:失敗");
}
}
}
※注意①:上記ログイン処理はどこでも記載できますが、アプリが非アクティブになるとログアウトしてしまいます。再度アクティブした場合に自動でログインさせる 場合は、AppDelegate.swiftのfunc application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) 内に上記処理を記載して、変数をクラス変数として永続化させてください。
GKLocalPlayerUtil例文
GKLocalPlayerUtil.swiftをダウンロードする。(ご自由にご利用ください)
import UIKit
import GameKit
struct GKLocalPlayerUtil {
static var localPlayer:GKLocalPlayer = GKLocalPlayer();
static func login(target:UIViewController){
self.localPlayer = GKLocalPlayer();
self.localPlayer.authenticateHandler = {(viewController, error) -> Void in
if ((viewController) != nil) {
println("ログイン確認処理:失敗-ログイン画面を表示");
target.presentViewController(viewController, animated: true, completion: nil);
}else{
println("ログイン確認処理:成功");
if (error == nil){
println("ログイン認証:成功");
}else{
println("ログイン認証:失敗");
}
}
}
}
}
//利用方法(AppDelegate.swiftに実装した例)
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
let targetViewController:UIViewController = ログイン画面を表示させたいビューコントローラー
GKLocalPlayerUtil.login(targetViewController);
}
※ Callbackをログイン結果の正否で実装したい場合は以下を参考にしてください。How to implement Objective-C callback handler in SWIFT?
↓こんな記事もありますよ!
![]() | CLLocationManagerで現在位置を取得あっさりしたシンプルなサンプルがなかったので省略型です。 |
![]() | presentViewControllerで画面遷移下からビョーン下からビョーンとUIViewControllerを出す方法を記載します。この処理も多用していたのですがこのHPになかったので追記しました。 Navigationと連動させた横へシャーと移動するスライドの関数(pop)とは違うので注意です。 |
![]() | UITabBarControllerスマホ画面下にタブが出てきて画面を切り替えできるUITabBarViewControllerクラスです。UITabBarItemの記事を追加しましたがUITabbarの記事がなかったので追加しました。(※目のチカチカにご注意ください。) |
Tweet
![]() |
|||
|