sooon
sooon

Reputation: 4888

Swift - MultipeerConnectivity Type does not conform to protocol

I am trying out this tutorial and having some problem with the code:

class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {

I got an error:

Type 'MPCManager' does not comform to protocol 'MCSessionDelegate'

Type 'MPCManager' does not comform to protocol 'MCNearbyServiceBrowserDelegate'

Type 'MPCManager' does not comform to protocol 'MCNearbyServiceAdvertiserDelegate'

But I download the sample file it did not get the same error. Which part had I missed out?

Upvotes: 3

Views: 2495

Answers (3)

Sylvent
Sylvent

Reputation: 66

You need to implement the protocols of the delegates. You can find it out when typing functions in xcode.

Upvotes: 1

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71852

You have to add all the required delegate methods of that protocol.

I have added all the required method into below code:

import Foundation
import MultipeerConnectivity


class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {

    //Type 'MPCManager' does not comform to protocol 'MCSessionDelegate'
    // Remote peer changed state
    func session(session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState){

    }

    // Received data from remote peer
    func session(session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!){

    }

    // Received a byte stream from remote peer
    func session(session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!){

    }

    // Start receiving a resource from remote peer
    func session(session: MCSession!, didStartReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, withProgress progress: NSProgress!){

    }

    // Finished receiving a resource from remote peer and saved the content in a temporary location - the app is responsible for moving the file to a permanent location within its sandbox
    func session(session: MCSession!, didFinishReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, atURL localURL: NSURL!, withError error: NSError!){

    }


    //Type 'MPCManager' does not comform to protocol 'MCNearbyServiceBrowserDelegate'

    // Found a nearby advertising peer
    func browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!){

    }

    // A nearby peer has stopped advertising
    func browser(browser: MCNearbyServiceBrowser!, lostPeer peerID: MCPeerID!){

    }

    //Type 'MPCManager' does not comform to protocol 'MCNearbyServiceAdvertiserDelegate'
    // Incoming invitation request.  Call the invitationHandler block with YES and a valid session to connect the inviting peer to the session.
    func advertiser(advertiser: MCNearbyServiceAdvertiser!, didReceiveInvitationFromPeer peerID: MCPeerID!, withContext context: NSData!, invitationHandler: ((Bool, MCSession!) -> Void)!){

    }

}

If you want to check what are the required methods for specific delegate then just command + click on that delegate and you will find all the methods related with that delegate suppose if you command + click on MCSessionDelegate then you will see something like this:

// Delegate methods for MCSession
protocol MCSessionDelegate : NSObjectProtocol {

    // Remote peer changed state
    func session(session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState)

    // Received data from remote peer
    func session(session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!)

    // Received a byte stream from remote peer
    func session(session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!)

    // Start receiving a resource from remote peer
    func session(session: MCSession!, didStartReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, withProgress progress: NSProgress!)

    // Finished receiving a resource from remote peer and saved the content in a temporary location - the app is responsible for moving the file to a permanent location within its sandbox
    func session(session: MCSession!, didFinishReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, atURL localURL: NSURL!, withError error: NSError!)

    // Made first contact with peer and have identity information about the remote peer (certificate may be nil)
    optional func session(session: MCSession!, didReceiveCertificate certificate: [AnyObject]!, fromPeer peerID: MCPeerID!, certificateHandler: ((Bool) -> Void)!)
}

Where only last method is optional so if you do not add it your delegate works fine but you have to add all above 5 methods into your class to conform the protocol.

Hope It will help.

Upvotes: 3

Reming Hsu
Reming Hsu

Reputation: 2225

You need to implement all the required delegate methods of MCSessionDelegate and MCNearbyServiceBrowserDelegate and MCNearbyServiceAdvertiserDelegate, if you want to conform to these protocols.

Upvotes: 4

Related Questions