I am currently working on my game and I have decided to enable multiplayer via GameCenter in the Game to allow users to play their friend. I have followed a tutorial by RayWinderLinch, but ran into a problem.
My problem is that when I load up the GKMatchMakingViewController and hit the big Play Now button on both devices it will find each other (which is meant to happen) and under the set game center user name it will say Ready.
This means that GameCenter has found each player and is ready to start the match which it should, but in my case the match never begins. It is stuck on a loop that says Starting Game... and nothing happens. It appears that the
func matchmakerViewController(viewController: GKMatchmakerViewController!, didFindMatch theMatch: GKMatch!)
and the
func match(theMatch: GKMatch!, player playerID: String!, didChangeState state: GKPlayerConnectionState)
method's are never ran. I am completely lost on what is going on. I have tried this many times over and over to fix the problem but nothing worked. I will attach an image that show's the screen of the application where my problem persists and I will also attach the code I am using.
I am using a framework based of of the GameKitHelper.h In the
mentioned tutorial above. It is written in swift and is called
GCHelper
Code
The code for GCHelper can be found using the GitHub link mention earlier
I have cut out code that is unnecessary for this problem
class GameScene : SKScene, GameKitHelper, MultiplayerNetworkingProtocol {
override func didMoveToView () {
GCHelper().authenticateLocalUser() //Authenticate GameCenter User
println("\n \n \n Authenticating local user \n \n \n")
}
func startMultiplayer () {
var vc = self.view?.window?.rootViewController
GameKitHelper().findMatchWithMinPlayers(2, maxPlayers: 2, viewController: vc!, delegate: self); //Find match and load GKMatchMakerViewController
}
func matchStarted() {
//Delegate method
println("match started")
}
func matchEnded() {
//Delegate method
println("match ended")
}
func match(match: GKMatch, didReceiveData: NSData, fromPlayer: String){
//Delegate Method
println("Did receive data")
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == multiplayer //SKSpriteNode {
//User clicked on multiplayer button, launch multiplayer now!
println("Loading multiplayer")
startMultiplayer()
}
}
Image
UPDATE
I have noticed that when I test using my iPhone and the simulator, on the iPhone the status will go from Ready to Disconnected but on the simulator the status is still Ready and then I will get the following message in the console for the iPhone
Warning matchmakerViewController:didFindMatch: delegate method not implemented`
Even though it is implemented in the GCHelper.swift file. This does not happen when I test on my iPhone and iPad Mini it just keeps on saying Starting Game...
Any help will be appreciated.
Prerequisites
Both players must be in the same environment (Sandbox for testing)
The authenticationChanged in GCHelper.swift must not be private. You may have to remove that keyword.
There are a few delegates involved, and in your example, there are a few competing protocols. My recommendation is to create a new App using minimalistic code to track down the startMultiplayer issue.
Gamekit Multi Player Step by Step Tutorial using GCHelper
Create a new project (Xcode > File > New > Project... > Single View Application > ... > Create) using the very same Product Name & Organization Name as your game, so that it matches both App Bundle Identifier and iTunes Game Center parameters. This will allow you to run tests without overhead.
Use this Podfile:
platform :ios, '8.0'
use_frameworks!
target 'SO-31699439' do
pod 'GCHelper'
end
Use a GCHelperDelegate
Create a UIViewController with just the bare minimum (a Start Multiplayer button), and connect it to this action:
#IBAction func startMultiplayerAction(_ sender: AnyObject) {
GCHelper.sharedInstance.findMatchWithMinPlayers(
2,
maxPlayers: 2,
viewController: self,
delegate: self);
}
Here is the crux: the delegate you pass must adopt GCHelperDelegate. It does not have to be the same class, but in your example above it is, and the present rule was not respected. For this example, ViewController adopts GCHelperDelegate:
import UIKit
import GCHelper
import GameKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
GCHelper.sharedInstance.authenticateLocalUser()
}
}
Implement required GCHelperDelegate methods in an extension
Since ViewController adopts GCHelperDelegate, the three methods below must be in that same class, and will be invoked:
extension ViewController: GCHelperDelegate {
func matchStarted() {
print("matchStarted")
}
func match(_ match: GKMatch, didReceiveData: Data, fromPlayer: String) {
print("match:\(match) didReceiveData: fromPlayer:\(fromPlayer)")
}
func matchEnded() {
print("matchEnded")
}
}
Execution
Tested: built, linked, ran, successful match.
Launch app, tap Start Multiplayer button, tap Play Now on both devices (or iPhone Simulator + real device).
Log:
Authenticating local user...
Authentication changed: player not authenticated
Ready to start match!
Found player: SandboxPlayer
matchStarted
► Find this solution on GitHub and additional details on Swift Recipes.
Related
Many questions on this topic, but most are so old the answers no longer apply due to both Swift and iOS having evolved so much since they were posted. The few that look helpful at first glance don't actually tell me anything I haven't already figured out, and haven't done anything to help me fix my incarnation of the problem, despite appearing to be nearly identical to the problem I'm having.
Aside from AVAudioPlayerDidFinishPlaying never being called (And therefore leaving "other sounds" being made by the device "ducked" until the app is totally shut down) the code below functions exactly as expected, in both Simulator and on a physical iPhone 7 Plus running iOS 13.3. If it makes a difference, I'm coding to Swift 5.1, targeting iOS 13.3, in Xcode 11.3, on a rig that runs Catalina (10.15.2).
Here's the "broken" code:
import Foundation
import AVFoundation
class NoiseMaker: NSObject, AVAudioPlayerDelegate {
var ourSession = AVAudioSession()
var ourPlayer = AVAudioPlayer()
func playSound(sound: String) {
print("Entered NoiseMaker.playSound(sound: \(sound))")
let theURL: URL = Bundle.main.url(forResource: sound, withExtension: "mp3")!
try!ourSession.setCategory(AVAudioSession.Category.ambient, options: AVAudioSession.CategoryOptions.duckOthers)
try!ourSession.setActive(true)
ourPlayer.delegate = self
ourPlayer.prepareToPlay()
do {
ourPlayer = try AVAudioPlayer(contentsOf: theURL)
ourPlayer.play()
} catch let theError as NSError {
print("Couldn't load \(sound).mp3 - Error \(theError)")
}
}
// MARK: - AVAudioPlayer delegate methods
// Never gets called - Other sound sources remain "ducked" until the app is completely shut down.
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
print("Arrived in audioPlayerDidFinishPlaying")
try!ourSession.setActive(false)
}
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
print("Decoding error from AVAudioPlayer: \(error!.localizedDescription)")
}
}
As noted, everything works as expected - EXCEPT for my audioPlayerDidFinishPlaying method never being called, meaning I never get a chance to "unduck" other sounds that might be playing. (although everything "unducks" as soon as the app is completely shut down)
Why? How am I screwing up what seems to be a nearly trivial task?
I see the problem, you set self as the delegate but then you change the reference to a new instance of AVAudioPlayer
You should set the delegate after creating new instance in here
ourPlayer = try AVAudioPlayer(contentsOf: theURL)
ourPlayer.delegate = self
ourPlayer.play()
So I have a an app that uses interstitial ads. Specifically it is a a SpriteKit game written with Swift.
I have code setup that when the user presses the replay button from the game over scene an ad appears and then it changes back to the game scene to replay the game. Now where I am running into problems the scene changes while the interstitial ad is being displayed, sometimes this doesn't happen fast enough and user can tap the restart button again, causing the game to crash.
Is there a way to freeze the screen and ignore any taps while the ad is being called? And also to only have the scene change after the ad is dismissed?
The code when the restart button is pressed;
if restartButton.contains(pointOfTouch) {
score = 0
ballMovementSpeed = 2
displayAd()
delay(2.0) {
self.restartScene()
}
I am a bit confused as to where the interstitialDelegate is placed. I am trying to implement func interstitialDidDismissScreen(ad: GADInterstitial!) {} to trigger a change back to my game scene and nothing happens when I dismiss the ad.
I have tried placing it in override func didMove(to view: SKScene){} as well as when the restart button is pressed and still won't work. This is how i have the ad being called
fun loadAndShow() {
myAd = GADInterstitial()
let requestI = GADRequest()
myAd.setAdUnitID("adID")
requestI.testDevices = [kGADSimulatorID, "test device"]
myAd.delegate = self
myAd.load(requestI)
}
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
if (self.myAd.isReady) {
myAd.present(fromRootViewController: self)
}
}
you can use delegate methods of admob so when interstitial is going to be shown you can remove the restart button or put a condition so that it would not work when ad is shown. Also to pause the game is also important if it is running using isPaused bool.
https://developers.google.com/admob/ios/ad-events
The WatchKit reference seems to make no mention about it. Have I missed something? Or is it really not possible to implement a shake gesture in an Apple Watch application?
The following is a typical example of a shake gesture implementation on iOS:
// MARK: Gestures
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
if(event.subtype == UIEventSubtype.MotionShake) {
// do something
}
}
No, it is not possible to do anything having to do with UIEvents in WatchKit right now, with the current solution's "remoted UI" approach where you mostly just get to tell the watch how to use the pre-arranged UI from the storyboard and react to actions like tapping a button or a table row. There will be support for a lot more code running on the watch later this year, according to Apple.
Update: Native apps are now possible for watchOS 2. This functionality may be present.
This is now possible in Watch OS2 with CMMotionManager a part of CoreMotion.
You can have this workaround.
let motionManager = CMMotionManager()
if (motionManager.accelerometerAvailable) {
let handler:CMAccelerometerHandler = {(data: CMAccelerometerData?, error: NSError?) -> Void in
if (data!.acceleration.z > 0) {
// User did shake
}
}
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: handler)
}
How Roger say, an workaround is use the CoreMotion.
You can use coreMotion to get movement.
I build this simple wrapper on my Github.
I am developing an app that communicates between the watch and the iOS parent app. It sends data from the WatchKit extension to the parent application by opening it. I understand that openParentApplication:reply, when called, opens the iPhone application from the Apple Watch. After that, application:handleWatchKitExtension:reply is called in the app's delegate.
From there you can open a notification to the view controller with:
NSNotificationCenter.defaultCenter().postNotificationName(aName: String, object anObject: AnyObject?)
"aName" is what you can open up in the view conroller with:
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("handleWatchKitNotification:"),
name: "WatchKitSaysHello",
object: nil)
Here is my code for my interface controller in my WatchKit extension:
//
// InterfaceController.swift
// SendColors WatchKit Extension
//
// Created by Tommy on 12/30/14.
// Copyright (c) 2014 Tommy. All rights reserved.
//
import WatchKit
import Foundation
class InterfaceController: WKInterfaceController {
var ypo = false
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
#IBOutlet weak var redButton: WKInterfaceButton!
#IBOutlet weak var greenButton: WKInterfaceButton!
#IBOutlet weak var blueButton: WKInterfaceButton!
#IBAction func onRedButtonClick() {
if ypo {
openParentAppWithColor("Yellow")
}
else {
openParentAppWithColor("Red")
}
}
#IBOutlet weak var moreButton: WKInterfaceButton!
#IBAction func moreButtonClick() {
if !ypo {
ypo = true
redButton.setTitle("Yellow")
redButton.setColor(UIColor.yellowColor())
greenButton.setTitle("Purple")
greenButton.setColor(UIColor.purpleColor())
blueButton.setTitle("Orange")
blueButton.setColor(UIColor.orangeColor())
moreButton.setTitle("Back")
}
else {
ypo = false
redButton.setTitle("Red")
redButton.setColor(UIColor.redColor())
greenButton.setTitle("Green")
greenButton.setColor(UIColor.greenColor())
blueButton.setTitle("Blue")
blueButton.setColor(UIColor.blueColor())
moreButton.setTitle("More...")
}
}
#IBAction func onGreenButtonClick() {
if ypo {
openParentAppWithColor("Purple")
}
else {
openParentAppWithColor("Green")
}
}
#IBAction func onBlueButtonClick() {
if ypo {
openParentAppWithColor("Orange")
}
else {
openParentAppWithColor("Blue")
}
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
private func openParentAppWithColor(color: String) {
if ["color": color] != nil {
if !WKInterfaceController.openParentApplication(["color": color], reply: { (reply, error) -> Void in
println(reply)
}) {
println("ERROR")
}
}
}
}
My problem is that say for instance I clicked the red button on the watch simulator. The action would be called and in that action it calls openParentApplicationWithColor("Red") which would call WKInterfaceController.openParentApplication(["color": color], reply: { (reply, error) -> Void in }) What this is supposed to do is open the parent app on the simulator. It opens it in the background. I, therefore open it manually. When I open the app manually, the background is completely black. I suspect the problem is that I did not enable App Groups in the Capabilities tab. In order to do that, you need to be in the developer program. Should I join it to do this, or is there another problem? I am using Xcode 6.2 Beta 3. Thank you all in advance!
I have tested and can confirm that openParentApplication:reply: does not require App Groups to be enabled.
I understand that openParentApplication:reply, when called, opens the iPhone application from the Apple Watch.
This method opens the iPhone app in the background. In previous betas of Xcode 6.2 the app did open in the foreground, but this was not the intended behaviour and is not how the WatchKit Extension-iPhone app communication will work in the shipping version.
You can still launch the iPhone app in the foreground manually, but this isn't necessary unless you want to test the use case of both being used at once. At least with the API currently available, the iPhone app it can't be launched programmatically by the watch app to the foreground, and the watch app can't be launched programmatically by the iPhone app.
Once the app launches, you've reported the screen is still black when you expect it to change colour based on the message you've sent it. Is application:handleWatchKitExtension:reply: being called in the iPhone app? You will know it is definitely being called if you are receiving the reply block back for execution in your WatchKit Extension, which should be outputting something from your println(reply). If so, then the issue is with the code in the iPhone app to do something with the message you've passed to it. It would be useful to see your implementation of that method in your AppDelegate of the iPhone app. (Note if that method failed to call the reply block, it might still be receiving the message and you wouldn't get the reply, but then you would be seeing an error message about the reply not being received.)
Note that you won't see NSLog messages or get breakpoints in Xcode when running the Watch extension initially, but you can select Debug > Attach to process > [select your iPhone app under 'Likely targets'] and then you will get the logs and breakpoints of the iPhone app instead of the Watch app, while still being able to use the Watch app in the watch simulator. Most useful for debugging.
No, you no need to make group enable to use ole parent application. you can make a request to open parent IOS application and waiting a reply from IOS App without setting the group app. You only need to setting the group app when you want to share data between watchkit app and IOS app using NSUserDefauts with suite name.
I am using Xcode 6.2 Beta 5. Select Debug > Attach to process > [select your iPhone app under likely targets is not working. I see the NSLogs for Watch extension but not in iPhone app delegate and view controller of iPhone app.
I'm currently making a 2-player strategy board game in Swift and need to connect two iPads over local WiFi or Bluetooth. No matter what I've tried today, I can't get them to detect each other (I've tried over local WiFi and Bluetooth).
Here is my authorization code which runs in the UIViewController when my app first launches (which always returns "Self local player is authenticated." along with the ID:
private func authenticateLocalPlayer() {
var localPlayer = getLocalPlayer()
// If Apple were doing their job right, this is what the proper code should look like:
// var localPlayer = GKLocalPlayer.localPlayer()
if ( !localPlayer.authenticated ) {
localPlayer.authenticateHandler = { (viewController : UIViewController!, error : NSError!) -> Void in
NSLog("Error: \(error)")
if viewController != nil {
// Authenticated?
self.presentViewController(viewController, animated: true, completion: nil)
NSLog("viewController is not nil")
} else if (localPlayer.authenticated == true) {
NSLog("Self local player is authenticated.")
NSLog("My name is \(localPlayer.playerID)")
} else {
NSLog("Not authenticated")
NSLog("Player is \(localPlayer.playerID)")
}
}
} else {
NSLog("Player is already authenticated!")
}
}
and here is my code to detect nearby devices in a separate UIViewController:
override func viewDidLoad() {
devicesLabel.text = "Waiting for devices..."
searchForDevices()
NSLog("Ran searchForDevices()")
}
private func searchForDevices() {
GKMatchmaker.sharedMatchmaker().startBrowsingForNearbyPlayersWithHandler() {
var status = $1 ? "true" : "false"
self.devicesLabel.text = "Reachability changed for player \($0) with status: \(status)"
}
}
No matter what I do with my two iPads (both are model iPad 3), neither one ever sees the other. Am I calling startBrowsingForNearbyPlayersWithHandler correctly?
Also notice that in the authorization code above, I'm using the Objective-C workaround recommended by this post: Game Center not authenticating using Swift, since the "Swift way" of doing that didn't work for me either.
I also ran Spelltower across both devices over local WiFi, so it looks like the hardware is functioning properly. Any idea what could be going wrong here?
I decided to abandon developing this through Game Center and to use Multipeer Connectivity instead.
You are not registering a class to receive invitation updates. You need to register a class and implement methods conforming to the protocol for GKLocalPlayerListener. See my response in this post (it is in Objective-C, but the same concept applies):
Some startBrowsingForNearbyPlayersWithReachableHandler questions