Skip to content

Commit

Permalink
Updated for use with Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcook committed Sep 24, 2016
1 parent ae24b5f commit 8c379b6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
7 changes: 7 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions GCHelper.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Pod::Spec.new do |s|
s.name = 'GCHelper'
s.version = '0.3.2'
s.version = '0.4'
s.summary = 'A lightweight helper for GameKit, written in Swift'
s.homepage = 'https://github.com/jackcook/GCHelper'
s.license = 'MIT'
s.author = { 'Jack Cook' => '[email protected]' }
s.source = { :git => 'https://github.com/jackcook/GCHelper.git', :tag => '0.3.2' }
s.source = { :git => 'https://github.com/jackcook/GCHelper.git', :tag => '0.4' }
s.social_media_url = 'https://twitter.com/jackcook36'

s.ios.deployment_target = '8.0'
Expand Down
30 changes: 17 additions & 13 deletions Pod/Classes/GCHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ public class GCHelper: NSObject, GKMatchmakerViewControllerDelegate, GKGameCente
fileprivate var playersDict = [String: AnyObject]()
fileprivate weak var presentingViewController: UIViewController!

fileprivate var authenticated = false
fileprivate var authenticated = false {
didSet {
print("Authentication changed: player\(authenticated ? " " : " not ")authenticated")
}
}

fileprivate var matchStarted = false

/// The shared instance of GCHelper, allowing you to access the same instance across all uses of the library.
Expand All @@ -66,22 +71,20 @@ public class GCHelper: NSObject, GKMatchmakerViewControllerDelegate, GKGameCente
NotificationCenter.default.addObserver(self, selector: #selector(GCHelper.authenticationChanged), name: NSNotification.Name(rawValue: GKPlayerAuthenticationDidChangeNotificationName), object: nil)
}

// MARK: Internal functions
// MARK: Private functions

internal func authenticationChanged() {
@objc fileprivate func authenticationChanged() {
if GKLocalPlayer.localPlayer().isAuthenticated && !authenticated {
print("Authentication changed: player authenticated")
authenticated = true
} else {
print("Authentication changed: player not authenticated")
authenticated = false
}
}

fileprivate func lookupPlayers() {
let playerIDs = match.players.map { $0.playerID } as! [String]

GKPlayer.loadPlayers(forIdentifiers: playerIDs) { (players, error) -> Void in
GKPlayer.loadPlayers(forIdentifiers: playerIDs) { (players, error) in
guard error == nil else {
print("Error retrieving player info: \(error?.localizedDescription)")
self.matchStarted = false
Expand Down Expand Up @@ -110,6 +113,7 @@ public class GCHelper: NSObject, GKMatchmakerViewControllerDelegate, GKGameCente
/// Authenticates the user with their Game Center account if possible
public func authenticateLocalUser() {
print("Authenticating local user...")

if GKLocalPlayer.localPlayer().isAuthenticated == false {
GKLocalPlayer.localPlayer().authenticateHandler = { (view, error) in
guard error == nil else {
Expand Down Expand Up @@ -162,12 +166,12 @@ public class GCHelper: NSObject, GKMatchmakerViewControllerDelegate, GKGameCente
achievement.percentComplete = percent
achievement.showsCompletionBanner = banner

GKAchievement.report([achievement], withCompletionHandler: { (error) -> Void in
GKAchievement.report([achievement]) { (error) in
guard error == nil else {
print("Error in reporting achievements: \(error)")
return
}
})
}
}
}

Expand All @@ -177,7 +181,7 @@ public class GCHelper: NSObject, GKMatchmakerViewControllerDelegate, GKGameCente
:param: completion An optional completion block that fires after all achievements have been retrieved
*/
public func loadAllAchivements(_ completion: (() -> Void)? = nil) {
GKAchievement.loadAchievements { (achievements, error) -> Void in
GKAchievement.loadAchievements { (achievements, error) in
guard error == nil, let achievements = achievements else {
print("Error in loading achievements: \(error)")
return
Expand All @@ -198,7 +202,7 @@ public class GCHelper: NSObject, GKMatchmakerViewControllerDelegate, GKGameCente
:param: identifier A string that matches the identifier string used to create an achievement in iTunes Connect.
*/
public func achievementIsCompleted(_ identifier: String) -> Bool{
public func achievementIsCompleted(_ identifier: String) -> Bool {
if let achievement = achievements[identifier] {
return achievement.percentComplete == 100
}
Expand All @@ -210,7 +214,7 @@ public class GCHelper: NSObject, GKMatchmakerViewControllerDelegate, GKGameCente
Resets all achievements that have been reported to GameKit.
*/
public func resetAllAchievements() {
GKAchievement.resetAchievements { (error) -> Void in
GKAchievement.resetAchievements { (error) in
guard error == nil else {
print("Error resetting achievements: \(error)")
return
Expand All @@ -227,12 +231,12 @@ public class GCHelper: NSObject, GKMatchmakerViewControllerDelegate, GKGameCente
public func reportLeaderboardIdentifier(_ identifier: String, score: Int) {
let scoreObject = GKScore(leaderboardIdentifier: identifier)
scoreObject.value = Int64(score)
GKScore.report([scoreObject], withCompletionHandler: { (error) -> Void in
GKScore.report([scoreObject]) { (error) in
guard error == nil else {
print("Error in reporting leaderboard scores: \(error)")
return
}
})
}
}

/**
Expand Down

0 comments on commit 8c379b6

Please sign in to comment.