Swift多路连接聊天

时间:2016-04-25 10:21:47

标签: ios swift delegates multipeer-connectivity

我尝试创建一个与此tutorial类似的聊天应用。

我现在遇到的问题是教程是用Swift 1编写的,但是我在Swift 2编写代码。

因此,我的屏幕上出现错误,这些错误未在教程文档中提及。 有两个委托错误,

  

" MPCManager不符合x-delegate和y-delegate"。

我该怎么做才能解决这个问题?

MPCManager类的代码:

//
//  MPCManager.swift
//  MPCRevisited
//
//  Created by Maximilian BIEGEL on 22/04/16.
//  Copyright © 2016 Appcoda. All rights reserved.
//

import UIKit
import MultipeerConnectivity

protocol MPCManagerDelegate {
    func foundPeer()
    func lostPeer() 
    func invitationWasReceived(fromPeer: String)
    func connectedWithPeer(peerID: MCPeerID)
}

class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {

    var session: MCSession!

    var peer: MCPeerID!
    var browser: MCNearbyServiceBrowser!
    var advertiser: MCNearbyServiceAdvertiser!
    var foundPeers = [MCPeerID]()
    var invitationHandler: ((Bool, MCSession!)->Void)!
    var delegate: MPCManagerDelegate?

    override init() {
        super.init()

        peer = MCPeerID(displayName: UIDevice.currentDevice().name)

        browser = MCNearbyServiceBrowser(peer: peer, serviceType: "chat-mpc")
        browser.delegate = self

        advertiser = MCNearbyServiceAdvertiser(peer: peer, discoveryInfo: nil, serviceType: "chat-mpc")
        advertiser.delegate = self
    }

    func browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!) {
        foundPeers.append(peerID)

        delegate?.foundPeer()
    }

    func browser(browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
        for (index, aPeer) in foundPeers.enumerate() {
            if aPeer == peerID {
                foundPeers.removeAtIndex(index)
                break
            }
        }

        delegate?.lostPeer()
    }

    func browser(browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: NSError) {
        print(error.localizedDescription)
    }

    func advertiser(advertiser: MCNearbyServiceAdvertiser!, didReceiveInvitationFromPeer peerID: MCPeerID!, withContext context: NSData!, invitationHandler: ((Bool, MCSession!) -> Void)!) {
        self.invitationHandler = invitationHandler

        delegate?.invitationWasReceived(peerID.displayName)
    }        

    func advertiser(advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: NSError) {
        print(error.localizedDescription)
    }

    func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState) {
        switch state {
            case MCSessionState.Connected:
                print("Connected to session: \(session)")
                delegate?.connectedWithPeer(peerID)

            case MCSessionState.Connecting:
                print("Connecting to session: \(session)")

            default:
                print("Did not connect to session: \(session)")
        }
    }

    func sendData(dictionaryWithData dictionary: Dictionary<String, String>, toPeer targetPeer: MCPeerID) -> Bool {
        let dataToSend = NSKeyedArchiver.archivedDataWithRootObject(dictionary)
        let peersArray = NSArray(object: targetPeer)
        var error: NSError?
        /*
         if !session.sendData(dataToSend, toPeers: peersArray, withMode: MCSessionSendDataMode.Reliable, error: &error) {
         print(error?.localizedDescription)
         return false
         }
         */
        return true
    }

    func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) {
        let dictionary: [String: AnyObject] = ["data": data, "fromPeer": peerID]
        NSNotificationCenter.defaultCenter().postNotificationName("receivedMPCDataNotification", object: dictionary)
    }

    func session(session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, withProgress progress: NSProgress) { }

    func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?) { }

    func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID) { }  
}

0 个答案:

没有答案