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?
↓こんな記事もありますよ!
![]() 2021-04-22 14:54:53 | かっこいいカルーセル厳選3点!WebGLを使った渋い画像スライダー。
昨日カッコいいスライダーを探す旅にでていました。
よく使われるswiper など、シンプルで使いやすくデザインも良いのですが、何かもの足りない、、、。
そこで調べていくと、webglを使... |
![]() 2021-04-20 14:46:43 | Twillioとは何??skywayとは何が違うのか。webrtcを使う際に判断する情報。
先日webrtc開発で「twillio」といった名前を聞きました。
以前自分でskywayを使ってwebrtcを試していた際に調べた時には、無かったような気もするんだけと、KDDIが絡んでいる... |
![]() 2021-04-19 13:58:21 | 先日久々にアップデートした妊娠週刊パパのAndroid版も。アカウント移管したいんだけど、注文ID見当たらないよ。
先日の妊娠、出産、育児関連のアプリ「妊娠週刊パパ」をAndroid studioで再リリースしようと思ってるんだけど、コンパイル失敗してエミュレータで動かせない。
多分sdkとかapiのバージ... |
Tweet
![]() |
|||
|