Gamecenter "Welcome"-Banner not showing up - ios

I have a problem with Gamecenter. Actually I don't know if its really a problem.
After authenticating my localPlayer the "Welcome"-Banner does not show up.
Why is that? Is that a sign that something is wrong?
I wrote my first app with Swift and SpriteKit with Gamecenter integration. When I open my app, Gamecenter authenticates my localPlayer just fine.
GKLocalPlayer.localPlayer().authenticated // is true
Opening up the Leaderboard works fine too. If I log out of Gamecenter and open my App again it asks me to log in with the view Gamecenter provides.
Showing a GKNotification is also no problem. Pops up right away.
This is my code to authenticate the player:
func authenticateLocalPlayer(){
var localPlayer = GKLocalPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if ((viewController) != nil) {
self.presentViewController(viewController, animated: true, completion: nil)
}else{
println("(GameCenter) Player authenticated: \(GKLocalPlayer.localPlayer().authenticated)")
}
}
}
What am I missing?

I'm using this to authenticate with Game Center, and I had never a problem with it (always a welcome banner):
func authenticateLocalPlayer() {
let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler =
{ (viewController : UIViewController?, error : NSError?) -> Void in
if viewController != nil
{
self.presentViewController(viewController!, animated:true, completion: nil)
}
else
{
if localPlayer.authenticated
{
print("Player authenticated")
self.gameCenterEnabled = true
isAuthenticated = true
localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler
{ (leaderboardIdentifier, error) -> Void in
if error != nil
{
print("error", appendNewline: false)
}
else
{
self.leaderboardIdentifier = leaderboardIdentifier
print("\(self.leaderboardIdentifier)")
}
}
} else {
print("Not able to authenticate")
self.gameCenterEnabled = false
if error != nil
{
print("\(error!.description)")
}
else
{
print("error is nil")
}
}
}
}
}

Related

iOS 12: ReplayKit is broken

I have been using ReplayKit for all past updates, but now with iOS 12 my recordings sometimes work, sometimes don't... but usually they don't. Most of the time when I stop the recording this is what I get:
a completely black screen.
This hasn't happened to me before and it is extremely frustrating. This is how I use ReplayKit to record the screen:
import ReplayKit
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, RPPreviewViewControllerDelegate {
func startRecording() {
func start() {
guard RPScreenRecorder.shared().isAvailable else {
print("Recording is not available at this time.")
return
}
RPScreenRecorder.shared().isMicrophoneEnabled = micToggle
RPScreenRecorder.shared().startRecording { [unowned self] (error) in
guard error == nil else {
print("There was an error starting the recording.")
return
}
print("Started Recording Successfully")
isRecording = true
}
}
DispatchQueue.main.async {
start()
}
}
func stopRecording() {
func stop() {
RPScreenRecorder.shared().stopRecording { [unowned self] (preview, error) in
print("Stopped recording")
guard preview != nil else {
print("Preview controller is not available.")
return
}
onGoingScene = true
preview?.previewControllerDelegate = self
self.present(preview!, animated: true, completion: nil)
print("presented")
isRecording = false
}
}
DispatchQueue.main.async {
stop()
}
}
func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
previewController.dismiss(animated: true, completion: nil)
RPScreenRecorder.shared().discardRecording {
print("discarded")
}
}
When it works, all the print statements are printed, but when the black screen appears the last print statement is "presented".
I am absolutely desperate for some help because I have no idea how to get around this. ANY help would be much appreciated.
THANKS
Edit:
I just realised that I am using an 'AVCaptureVideoPreviewLayer` if that may be the issue. If so, what's the fix?

How to display Leaderboard Sets in iOS

I was trying to get this to work all last night but it wouldnt. Can anyone help?
I use the following code to display a leaderboard:
gameCenterViewController.leaderboardIdentifier = leaderboardId
This works fine for normal leaderboards, but fails to load any leaderboard sets, when I use the leaderboardSetId. Can you link directly to leaderboard sets and if so how do you do this?
Thanks.
Please do refer below code and cross verify your configuration.
Hope this would help you.
import GameKit
class YourViewController: UIViewController, GKGameCenterControllerDelegate {
var gcEnabled = Bool() // Stores if the user has Game Center enabled
var gcDefaultLeaderBoard = String() // Stores the default leaderboardID
override func viewDidLoad() {
super.viewDidLoad()
self.authenticateLocalPlayer()
}
func authenticateLocalPlayer()
{
let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(ViewController, error) -> Void in
if((ViewController) != nil) {
// 1 Show login if player is not logged in
self.presentViewController(ViewController!, animated: true, completion: nil)
} else if (localPlayer.authenticated) {
// 2 Player is already euthenticated & logged in, load game center
self.gcEnabled = true
// Get the default leaderboard ID
localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifer: String?, error: NSError?) -> Void in
if error != nil {
print(error)
} else {
self.gcDefaultLeaderBoard = leaderboardIdentifer!
}
})
} else {
// 3 Game center is not enabled on the users device
self.gcEnabled = false
print("Local player could not be authenticated, disabling game center")
print(error)
}
}
}
#IBAction func clickToLeaderBoard(sender: UIButton) {
let gcVC: GKGameCenterViewController = GKGameCenterViewController()
gcVC.gameCenterDelegate = self
gcVC.viewState = GKGameCenterViewControllerState.Leaderboards
gcVC.leaderboardIdentifier = "YourLeaderboardId"
self.presentViewController(gcVC, animated: true, completion: nil)
}
func saveScoreOnGameCenter()
{
let leaderboardID = "YourLeaderboardId"
let sScore = GKScore(leaderboardIdentifier: leaderboardID)
sScore.value = Int64(10)
GKScore.reportScores([sScore], withCompletionHandler: { (error: NSError?) -> Void in
if error != nil {
print(error!.localizedDescription)
} else {
print("Score submitted")
}
})
}
func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController)
{
self.dismissViewControllerAnimated(true, completion: nil)
}
}
Update
Also please do check your leaderboard configuration from back end.
Some good posts are here.
http://www.appcoda.com/ios-game-kit-framework/
https://developer.apple.com/game-center/
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/GameKit_Guide/GameCenterOverview/GameCenterOverview.html
Having ensured that you have authenticated the local player and your leaderboards set in iTunes Connect, you will need to report your scores to GameCenter ensuring you use 64bit Int.
Now you can add this to display the leaderboard for the local player:
func showGKGameCenterViewController(_ viewController: UIViewController) {
if gameCenterIsDisabled {
print("Local player is not authenticated")
}
let gameCenterViewController = GKGameCenterViewController()
gameCenterViewController.delegate = self
gameCenterViewController.viewState = GKGameCenterViewControllerState.default
viewController.presentViewController(gameCenterViewController, animated: true, completion: nil)
}

Swift GameCenter not behaving properly

I have 2 problems I am experiencing with GameCenter.
If a player is not signed into GameCenter, after if gives you the alert saying you are not signed in, I am no longer able to click any buttons I have on my view.
If a player is signed into GameCenter, after accessing the Leaderboards, pressing “done” will not dismiss the leaderboard view.
Here is my code that relates to GameCenter:
GameViewController:
func authPlayer() { // Gets called in ViewDidLoad
let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {
(view, error) in
if view != nil {
self.presentViewController(view!, animated: true, completion: nil)
}
else {
print(GKLocalPlayer.localPlayer().authenticated)
}
}
}
GameScene:
if gameCenterBtn.containsPoint(location) { // In touches began for touch in touches
saveHighScore(highLevel)
let viewController = self.view!.window?.rootViewController
let gcvc = GKGameCenterViewController()
viewController?.presentViewController(gcvc, animated: true, completion: nil)
}
func saveHighScore(number : Int) {
if GKLocalPlayer.localPlayer().authenticated {
let scoreReporter = GKScore(leaderboardIdentifier: "myLeaderBoardID")
scoreReporter.value = Int64(number)
let scoreArray : [GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray, withCompletionHandler: nil)
}
}
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
Any and all help would be appreciated
1) Not sure this will help, but your login code is not quite correct. If there is no login view controller doesnt necessarily mean the player is signed in. You are also not handling the optional error. Try this instead.
localPlayer.authenticateHandler = { [unowned self] (viewController, error) in // will handle login changes also
if let error = error {
print(error.localizedDescription)
return
}
if let viewController = viewController {
self.presentViewController(viewController, animated: true, completion: nil)
}
else if self.localPlayer.authenticated {
print("Player authenticated")
}
else {
print("Player not authenticated")
}
}
I am not sure why your gameViewCntroller will not respond. Are you not just loading the first SKScene in your gameViewController.
Could you describe further, maybe with some code, of what does not work afterwards.
2) Its not dismissing the screen because you did not set the delegate.
You code where you are creating the Game Center viewController should look like this
let viewController = self.view?.window?.rootViewController
let gcvc = GKGameCenterViewController()
gcvc.gameCenterDelegate = self // YOU FORGOT THIS LINE
viewController?.presentViewController(gcvc, animated: true, completion: nil)

is it possible in iOS get Game Center id or account?

I wan't use iOS GameCenter id to my own server id.
like google plus.
Is there any method to get game center id or account?
I have searched in internet this method. but it's return nil.
let player = GKLocalPlayer.localPlayer()
player.playerID, player.displayName
playerID is nil and displayName is Me
I want uniqe key is it possible?
func authenticateLocalPlayer() {
var localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in
if viewController != nil {
self.presentViewController(viewController, animated: true, completion: nil)
}
else {
if localPlayer.authenticated {
self.gameCenterEnabled = true
localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifier : String!, error : NSError!) -> Void in
if error != nil {
println(error.localizedDescription)
} else {
self.leaderboardIdentifier = leaderboardIdentifier
***above is your Game Center id***
}
})
} else {
self.gameCenterEnabled = false
}
}
}
}
In above code there self.leaderboardIdentifier is game centre id

Game Center log-in window never appearing

The problem here is that the player is not getting authenticated and I'm not getting the log-in window for game center so I really need to get this fixed as soon as possible.
func authenticateLocalPlayer() {
var localPLayer = GKLocalPlayer.localPlayer()
localPLayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in
if viewController != nil {
self.presentViewController(viewController, animated: true, completion: nil)
} else {
if localPLayer.authenticated {
self.gameCenterEnabled = true
localPLayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifier : String!, error : NSError!) -> Void in
if error != nil {
println(error.localizedDescription)
} else {
self.leaderboardIdentifier = leaderboardIdentifier
}
})
} else {
self.gameCenterEnabled = false
}
}
}
}

Resources