ios - Swift multipeer connectivity chat -
i tried create chat app similar tutorial.
the problem have right tutorial coded in swift 1
, writing code in swift 2
.
consequently, there errors appearing on screen not mentioned in tutorials documentation. there 2 delegate errors say,
"mpcmanager not conform x-delegate , y-delegate".
what can fix issue?
code of class mpcmanager:
// // mpcmanager.swift // mpcrevisited // // created maximilian biegel on 22/04/16. // copyright © 2016 appcoda. 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) { (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 session: \(session)") delegate?.connectedwithpeer(peerid) case mcsessionstate.connecting: print("connecting session: \(session)") default: print("did not connect 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) { } }
Comments
Post a Comment