Search code examples
swiftgamekitios8gksession

Whats the correct signature of receiveData:fromPeer:inSession:context: in swift?


im trying to rewrite my GameKit multiplayer game (local) in swift and have problems with some missing documentation for the language. I want to receive data from another peer so i set the dataReceiveHandler for my GKSession like this:

session.setDataReceiveHandler(self, withContext: nil)

in apples documentation it says that the dataReceiveHandler hast to implement a method with this signature:

SEL = -receiveData:fromPeer:inSession:context:

In the objective-c documentation is an example of how the signature should look like:

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context;

if i try to rewrite this method in swift it looks like this:

func receiveData(data: NSData, fromPeer peer: String, inSession session: GKSession, withContext context: CMutableVoidPointer) {
    println("received data: \(data)")
}

and gives me this error when i receive a message:

Domain=com.apple.gamekit.GKSessionErrorDomain Code=30500 "Invalid parameter for -setDataReceiveHandler:withContext:" UserInfo=0x178462840 {NSLocalizedDescription=Invalid parameter for -setDataReceiveHandler:withContext:, NSLocalizedFailureReason=The handler does not respond to the correct selector.}

this means my method has not the correct signature. But what is the correct one?


Solution

  • Sorry that i have to edit this again but i have the actual answer now:

    DONT USE GKSESSION! IT IS DEPRECATED SINCE iOS7!

    Use:

    MultipeerConnectivity.framework