GameCenter - log player out - iphone

I've got an iPhone game that I've just added GameCenter support to, and everything seems to be working ok. But, as someone who doesn't use GameCenter, I've added an option so that users can turn it off. The only problem is that once the GKLocalPlayer is signed into GameCenter, I can't see a way to sign them out - which means that if the user is signed in, then disables GameCenter support, my app won't use it, but the GK library still signs the user back in every time my app comes back to foreground, until the app is terminated. I don't want this to happen!
Is there any way to log the local player out of GameCenter, or at least stop the GK framework from logging you back in every time your app becomes active?

There's no way to log out of game center from within the app. The best solution is to add a boolean flag indicating the user's login status:
BOOL gameCenterOn = [[NSUserDefaults standardUserDefaults] boolForKey:#"gameCenterOn"];
Default it to YES, and just set it to NO if the user opts out of game center in your app.
You'll also have to check the value of that flag before processing any game center requests. (Including the [GKLocalPlayer localPlayer] authenticateWithCompletionHandler: calls in the app delegate.)

Related

Firebase - onDisconnectSetValue() fires when side button pressed

I'm trying to track when a user's app is about to terminate and send something to firebase when it happens. I looked through the Offline Capabilities documentation and tried using ".info/connected" as well as .onDisconnectSetValue().
I've had success with .onDisconnectSetValue(), but if the iPhone's side button is pressed and the iPhone goes to sleep, it fires and says the user is inactive (and I can't set another value until the user brings the app to the foreground again).
I tried moving .onDisconnectSetValue() to applicationWillTerminate() so it only catches terminations but it doesn't fire when I close the app (I assume because it can't in time)
Is there anything with Firebase that could set a value with the specific instance of the user closing the app?
The Firebase client can detect whether the app is online, with .info/connected and the app can tell the server to perform a specific write once it has gone offline with onDisconnect.
Firebase has no built in functionality for writing to the database when the user is about to close the app. That's something you're more likely to find on the iOS level, such as shown in iOS - detect when application exits and Which Event When i close app in iOS?. Typical lifecycle events on iOS are applicationWillTerminate, and applicationWillResignActive.

How to display an alert when user shake the iPhone?(This should be work even user is not using the app)

I want to fire a method(Display alert) in my app when user is shaking the iphone.This should be work even if user is not using the app.How can I do it ?
If your app isn't running in the foreground you can't access the accelerometer. You can't do what you're planning to do with iOS, at least not unless you jailbreak.

GKLocalPlayer isAuthenticated property always return NO

i use to check if a local player authenticated with Game Center with Block of code
if ([[GKLocalPlayer localPlayer] isAuthenticated])
{
NSLog(#"authenticated");
}
this code runs when i first login with GameCenter. When i kill my app and start again it returns NO (Player is not authenticated)
Do i have to login each time when i start the app?
Is there any other solution?
You have to authenticate localPlayer every time your app becomes active but the user only has to input his user/password info the first time. After that it "remembers" who the user is.
Note that the authentication method in iOS 5 is not the same as iOS 6.
Yes, you have to call authenticatedPlayer every time your app starts. First time iOS will prompt the user for GameCenter permission. Don't worry to call login, once the user select GameCenter allowed, it will be login automatically authenticated.

Can a multitasking app receive a notification when the device unlocks the screen even if the app is in the background?

My app needs to perform a calculation every time the device unlocks. It's an app which the user launches intentionally and then enters background, similar like a pasteboard manager.
How can it receive a notification when the device unlocks while the app is in the background? Is this possible? The calculation is not heavy. It just reads and writes a value from/to NSUserDefaults.
With the current SDK and public API there is no way to tell when the user unlocks the screen, even if your app is in the background...

Can I check for Game Center authentication status outside my app?

I want to be able to know if the user running my app is connected to GameCenter (through GameCenter app or through other app), when i'm first running my app.
I found out that if I check the boolean:
[GKLocalPlayer localPlayer].authenticated)
it returns false. I guess one thing that might fix this is running at startup this:
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)
However, in case the user is not connected this brings the game center pop up which requests an existing account or creating a new one.
So my question is: is there a way to know my user connected GC outside of my app while my app was down, without popping up the above alert in case he is not connected ?
Thanks!!
You can do it on iOS 6.0 or higher:
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
if (viewController == nil && error == nil) {
NSLog(#"Here, you know that the user has already signed to Game Center, whether in through your app or not.");
}
};
It seems to me that Game Center authenticates the user within each different application and not globally through iOS. So, you cannot check the authentication status of the user without using the authenticateWithCompletionHandler: method.
By the way, I think that you should revise this User Interface mechanism. It will bring you to a couple of problems. Game Center authentication will use a global account (first of all the App Store account) shared amongst all apps, and you should rely on Game Center to handle authentication. Supposing that you can do what you are looking for (and I think that's not possible), if you are already logged in through a 3rd party app, you will be able to log to GC even in yours; but if you are not logged in outside your app, you will not be able to use GC in your app.
This could be a problem for you because if the user has a valid account, and he currently has logged out of the App Store from the Settings app, he will not be able to log into GC for your app (since you do not want to show the user the log in alert). You will have to rely on 3rd party apps to enable all the GC features. So, GC will become useless, at this point.
Is this really what you want from your app? Why enabling GC if the user will probably be not able to use it efficiently?