-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMPCManager.swift
More file actions
99 lines (76 loc) · 2.71 KB
/
MPCManager.swift
File metadata and controls
99 lines (76 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// Scanner.swift
// MultipeerExample
//
// Created by Ben Gottlieb on 8/18/18.
// Copyright © 2018 Stand Alone, Inc. All rights reserved.
//
import Foundation
import MultipeerConnectivity
class MPCManager: NSObject {
var advertiser: MCNearbyServiceAdvertiser!
var browser: MCNearbyServiceBrowser!
struct Notifications {
static let deviceDidChangeState = Notification.Name("deviceDidChangeState")
}
static let instance = MPCManager()
let localPeerID: MCPeerID
let serviceType = "MPC-Testing"
var devices: [Device] = []
override init() {
if let data = UserDefaults.standard.data(forKey: "peerID"), let id = NSKeyedUnarchiver.unarchiveObject(with: data) as? MCPeerID {
self.localPeerID = id
} else {
let peerID = MCPeerID(displayName: UIDevice.current.name)
let data = NSKeyedArchiver.archivedData(withRootObject: peerID)
UserDefaults.standard.set(data, forKey: "peerID")
self.localPeerID = peerID
}
super.init()
self.advertiser = MCNearbyServiceAdvertiser(peer: localPeerID, discoveryInfo: nil, serviceType: self.serviceType)
self.advertiser.delegate = self
self.browser = MCNearbyServiceBrowser(peer: localPeerID, serviceType: self.serviceType)
self.browser.delegate = self
}
func device(for id: MCPeerID) -> Device {
for device in self.devices {
if device.peerID == id { return device }
}
let device = Device(peerID: id)
self.devices.append(device)
return device
}
func start() {
self.advertiser.startAdvertisingPeer()
self.browser.startBrowsingForPeers()
NotificationCenter.default.addObserver(self, selector: #selector(enteredBackground), name: Notification.Name.UIApplicationDidEnterBackground, object: nil)
}
@objc func enteredBackground() {
for device in self.devices {
device.disconnect()
}
}
}
extension MPCManager {
var connectedDevices: [Device] {
return self.devices.filter { $0.state == .connected }
}
}
extension MPCManager: MCNearbyServiceAdvertiserDelegate {
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {
let device = MPCManager.instance.device(for: peerID)
device.connect()
invitationHandler(true, device.session)
// Handle our incoming peer
}
}
extension MPCManager: MCNearbyServiceBrowserDelegate {
func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
let device = MPCManager.instance.device(for: peerID)
device.invite(with: self.browser)
}
func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
let device = MPCManager.instance.device(for: peerID)
device.disconnect()
}
}