HelloGameKit WatchKit Extension/PlayersInterfaceController.swift
/* |
Copyright (C) 2016 Apple Inc. All Rights Reserved. |
See LICENSE.txt for this sample’s licensing information |
Abstract: |
PlayersInterfaceController is a ListWithButtinInterfaceController subclass that specialize to loading recent players. It adopts the ListDataSource protocol and extends GKPlayer with the ListItem protocol |
*/ |
import WatchKit |
import Foundation |
import GameKit |
extension GKPlayer: ListItem { |
// MARK: ListItem Properties |
var title: String { |
return alias ?? "<no nickname>" |
} |
var detail: String { |
return displayName ?? "<no displayName>" |
} |
} |
class PlayersInterfaceController: ListWithButtonInterfaceController, ListDataSource { |
// MARK: WKInterfaceController |
override func awake(withContext context: Any?) { |
dataSource = self |
super.awake(withContext: context) |
} |
// MARK: ListDataSource methods |
func buttonTitle() -> String { |
return "Auto Match" |
} |
func loadItems(completionHandler: @escaping (([ListItem]) -> Void)) { |
guard loadingState != .loading else { return } |
loadingState = .loading |
GKLocalPlayer.localPlayer().loadRecentPlayers { [unowned self] (players, error) in |
if let items = players { |
let result = items.sorted { (p1, p2) -> Bool in |
guard let displayName1 = p1.displayName, let displayName2 = p2.displayName else { return false } |
return displayName1 < displayName2 |
} |
completionHandler(result) |
} |
else { |
// No players were found. |
completionHandler([]) |
} |
self.loadingState = error == nil ? .complete: .failed |
} |
} |
override func didSelect(item: ListItem?) { |
let player = item as? GKPlayer |
if let controller = presentingController { |
loadingState = .exiting |
controller.didSelect(player: player) |
} |
} |
} |
Copyright © 2016 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2016-10-27