Last active
January 1, 2017 16:11
-
-
Save brandon-james105/7de62dccb4b8c4ef14b1300c211fb34a to your computer and use it in GitHub Desktop.
MPCManager from AppCoda tutorial (https://www.appcoda.com/chat-app-swift-tutorial/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MPCManager.swift | |
// MPCRevisited | |
// | |
// Created by Gabriel Theodoropoulos on 11/1/15. | |
// Copyright (c) 2015 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 delegate: MPCManagerDelegate? | |
var session: MCSession! | |
var peer: MCPeerID! | |
var browser: MCNearbyServiceBrowser! | |
var advertiser: MCNearbyServiceAdvertiser! | |
var foundPeers = [MCPeerID]() | |
var invitationHandler: ((Bool, MCSession?)->Void)! | |
override init() | |
{ | |
super.init() | |
peer = MCPeerID(displayName: UIDevice.current.name) | |
session = MCSession(peer: peer) | |
session.delegate = self | |
browser = MCNearbyServiceBrowser(peer: peer, serviceType: "appcoda-mpc") | |
browser.delegate = self | |
advertiser = MCNearbyServiceAdvertiser(peer: peer, discoveryInfo: nil, serviceType: "appcoda-mpc") | |
advertiser.delegate = self | |
} | |
public func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL, withError error: Error?) | |
{ | |
print("Finished receiving resource from " + peerID.displayName) | |
} | |
public func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) | |
{ | |
print("Received invitation from " + peerID.displayName) | |
} | |
public func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) | |
{ | |
print("Found peer: " + peerID.displayName) | |
} | |
// MARK: MCNearbyServiceBrowserDelegate method implementation | |
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.enumerated() | |
{ | |
if aPeer == peerID | |
{ | |
foundPeers.remove(at: index) | |
break | |
} | |
} | |
delegate?.lostPeer() | |
} | |
func browser(_ browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: Error) | |
{ | |
print(error.localizedDescription) | |
} | |
// MARK: MCNearbyServiceAdvertiserDelegate method implementation | |
func advertiser(advertiser: MCNearbyServiceAdvertiser!, didReceiveInvitationFromPeer peerID: MCPeerID!, withContext context: Data!, invitationHandler: ((Bool, MCSession?) -> Void)!) | |
{ | |
self.invitationHandler = invitationHandler | |
delegate?.invitationWasReceived(fromPeer: peerID.displayName) | |
} | |
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: Error) | |
{ | |
print(error.localizedDescription) | |
} | |
// MARK: MCSessionDelegate method implementation | |
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) | |
{ | |
switch state{ | |
case MCSessionState.connected: | |
print("Connected to session: \(session)") | |
delegate?.connectedWithPeer(peerID: peerID) | |
case MCSessionState.connecting: | |
print("Connecting to session: \(session)") | |
default: | |
print("Did not connect to session: \(session)") | |
} | |
} | |
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) { | |
let dictionary: [String: AnyObject] = ["data": data as AnyObject, "fromPeer": peerID] | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "receivedMPCDataNotification"), object: dictionary) | |
} | |
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) { } | |
func session(session: MCSession!, didFinishReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, atURL localURL: NSURL!, withError error: NSError!) { } | |
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) { } | |
// MARK: Custom method implementation | |
func sendData(dictionaryWithData dictionary: Dictionary<String, String>, toPeer targetPeer: MCPeerID) -> Bool { | |
let dataToSend = NSKeyedArchiver.archivedData(withRootObject: dictionary) | |
let peersArray = NSArray(object: targetPeer) | |
do { | |
try session.send(dataToSend, toPeers: peersArray as! [MCPeerID], with: MCSessionSendDataMode.reliable) | |
} | |
catch _ { | |
return false | |
} | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment