Created
February 3, 2016 01:25
-
-
Save hcwang/1a62d7a2def4d271d950 to your computer and use it in GitHub Desktop.
Multipeer Connectivity framework on OS X and iOS
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
//On iOS | |
import UIKit | |
import MultipeerConnectivity | |
class ViewController: UIViewController { | |
var _session: MCSession! | |
var _peerID: MCPeerID! | |
var _browser: MCNearbyServiceBrowser! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
_peerID = MCPeerID(displayName: "TryOniOS") | |
_session = MCSession(peer: _peerID) | |
_session.delegate = self | |
_browser = MCNearbyServiceBrowser(peer: _peerID, serviceType: "TryMC") | |
_browser.delegate = self | |
_browser.startBrowsingForPeers() | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
extension ViewController: MCNearbyServiceBrowserDelegate{ | |
func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?){ | |
browser.invitePeer(peerID, toSession: _session, withContext: nil, timeout: 10) | |
} | |
func browser(browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID){ | |
NSLog("lost peerID: \(peerID.displayName)") | |
} | |
func browser(browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: NSError){ | |
NSLog("Didn't start to browser, and the error is: \(error.debugDescription)") | |
} | |
} | |
extension ViewController: MCSessionDelegate { | |
func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState){ | |
var logString = "" | |
switch state{ | |
case .Connected: | |
logString = "Connected" | |
case .NotConnected: | |
logString = "NotConnected" | |
case .Connecting: | |
logString = "Connecting" | |
// default: break | |
} | |
NSLog("\(peerID.displayName)的状态改变为:\(logString)") | |
} | |
func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID){ | |
} | |
func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID){ | |
} | |
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, didReceiveCertificate certificate: [AnyObject]?, fromPeer peerID: MCPeerID, certificateHandler: (Bool) -> Void){ | |
} | |
} | |
//on OS X | |
import Cocoa | |
import MultipeerConnectivity | |
class ViewController: NSViewController { | |
var _advertiser: MCNearbyServiceAdvertiser! | |
var _session: MCSession! | |
var _peerID: MCPeerID! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
_peerID = MCPeerID(displayName: "TryOnMac") | |
_session = MCSession(peer: _peerID) | |
_session.delegate = self | |
_advertiser = MCNearbyServiceAdvertiser(peer: _peerID, discoveryInfo: nil, serviceType: "TryMC") | |
_advertiser.delegate = self | |
_advertiser.startAdvertisingPeer() | |
} | |
override var representedObject: AnyObject? { | |
didSet { | |
// Update the view, if already loaded. | |
} | |
} | |
} | |
extension ViewController: MCNearbyServiceAdvertiserDelegate{ | |
func advertiser(advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: NSData?, invitationHandler: (Bool, MCSession) -> Void){ | |
//let accept = _session.myPeerID.hashValue != peerID.hashValue | |
invitationHandler(true, _session) | |
} | |
func advertiser(advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: NSError){ | |
NSLog("didn't start to advertise, and the error is \(error.debugDescription)") | |
} | |
} | |
extension ViewController: MCSessionDelegate { | |
func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState){ | |
var logString = "" | |
switch state{ | |
case .Connected: | |
logString = "Connected" | |
break | |
case .NotConnected: | |
logString = "NotConnected" | |
break | |
case .Connecting: | |
logString = "Connecting" | |
break | |
// default: break | |
} | |
NSLog("\(peerID.displayName) current state is :\(logString)") | |
} | |
func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID){ | |
} | |
func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID){ | |
} | |
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, didReceiveCertificate certificate: [AnyObject]?, fromPeer peerID: MCPeerID, certificateHandler: (Bool) -> Void){ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment