@@ -9,46 +9,46 @@ import Foundation
9
9
import MultipeerConnectivity
10
10
11
11
// MARK: - Main Class
12
- class MultiPeer : NSObject {
12
+ public class MultiPeer : NSObject {
13
13
14
- static let instance = MultiPeer ( )
14
+ public static let instance = MultiPeer ( )
15
15
16
16
// MARK: Properties
17
17
18
18
/** Conforms to MultiPeerDelegate: Handles receiving data and changes in connections */
19
19
weak var delegate : MultiPeerDelegate ?
20
20
21
21
/** Name of MultiPeer session: Up to one hyphen (-) and 15 characters */
22
- var serviceType : String !
22
+ public var serviceType : String !
23
23
24
24
/** Device's name */
25
- var devicePeerID : MCPeerID !
25
+ public var devicePeerID : MCPeerID !
26
26
27
27
/** Advertises session */
28
- var serviceAdvertiser : MCNearbyServiceAdvertiser !
28
+ public var serviceAdvertiser : MCNearbyServiceAdvertiser !
29
29
30
30
/** Browses for sessions */
31
- var serviceBrowser : MCNearbyServiceBrowser !
31
+ public var serviceBrowser : MCNearbyServiceBrowser !
32
32
33
33
/** Amount of time to spend connecting before timeout */
34
- var connectionTimeout = 10.0
34
+ public var connectionTimeout = 10.0
35
35
36
36
/** Peers available to connect to */
37
- var availablePeers : [ Peer ] = [ ]
37
+ public var availablePeers : [ Peer ] = [ ]
38
38
39
39
/** Peers connected to */
40
- var connectedPeers : [ Peer ] = [ ]
40
+ public var connectedPeers : [ Peer ] = [ ]
41
41
42
42
/** Names of all connected devices */
43
- var connectedDeviceNames : [ String ] {
43
+ public var connectedDeviceNames : [ String ] {
44
44
return session. connectedPeers. map ( { $0. displayName} )
45
45
}
46
46
47
47
/** Prints out all errors and status updates */
48
- var debugMode = false
48
+ public var debugMode = false
49
49
50
50
/** Main session object that manages the current connections */
51
- lazy var session : MCSession = {
51
+ public lazy var session : MCSession = {
52
52
let session = MCSession ( peer: self . devicePeerID, securityIdentity: nil , encryptionPreference: . none)
53
53
session. delegate = self
54
54
return session
@@ -60,7 +60,7 @@ class MultiPeer: NSObject {
60
60
/// - Parameters:
61
61
/// - serviceType: String with name of MultiPeer service. Up to one hyphen (-) and 15 characters.
62
62
/// Uses default device name
63
- func initialize( serviceType: String ) {
63
+ public func initialize( serviceType: String ) {
64
64
#if os(iOS)
65
65
initialize ( serviceType: serviceType, deviceName: UIDevice . current. name)
66
66
#elseif os(macOS)
@@ -72,7 +72,7 @@ class MultiPeer: NSObject {
72
72
/// - Parameters:
73
73
/// - serviceType: String with name of MultiPeer service. Up to one hyphen (-) and 15 characters.
74
74
/// - deviceName: String containing custom name for device
75
- func initialize( serviceType: String , deviceName: String ) {
75
+ public func initialize( serviceType: String , deviceName: String ) {
76
76
// Setup device/session properties
77
77
self . serviceType = serviceType
78
78
self . devicePeerID = MCPeerID ( displayName: deviceName)
@@ -97,51 +97,51 @@ class MultiPeer: NSObject {
97
97
// MARK: - Methods
98
98
99
99
/** HOST: Automatically browses and invites all found devices */
100
- func startInviting( ) {
100
+ public func startInviting( ) {
101
101
self . serviceBrowser. startBrowsingForPeers ( )
102
102
}
103
103
104
104
/** JOIN: Automatically advertises and accepts all invites */
105
- func startAccepting( ) {
105
+ public func startAccepting( ) {
106
106
self . serviceAdvertiser. startAdvertisingPeer ( )
107
107
}
108
108
109
109
/** HOST and JOIN: Uses both advertising and browsing to connect. */
110
- func autoConnect( ) {
110
+ public func autoConnect( ) {
111
111
startInviting ( )
112
112
startAccepting ( )
113
113
}
114
114
115
115
/** Stops the invitation process */
116
- func stopInviting( ) {
116
+ public func stopInviting( ) {
117
117
self . serviceBrowser. stopBrowsingForPeers ( )
118
118
}
119
119
120
120
/** Stops accepting invites and becomes invisible on the network */
121
- func stopAccepting( ) {
121
+ public func stopAccepting( ) {
122
122
self . serviceAdvertiser. stopAdvertisingPeer ( )
123
123
}
124
124
125
125
/** Stops all invite/accept services */
126
- func stopSearching( ) {
126
+ public func stopSearching( ) {
127
127
stopAccepting ( )
128
128
stopInviting ( )
129
129
}
130
130
131
131
/** Disconnects from the current session and stops all searching activity */
132
- func disconnect( ) {
132
+ public func disconnect( ) {
133
133
session. disconnect ( )
134
134
connectedPeers. removeAll ( )
135
135
availablePeers. removeAll ( )
136
136
}
137
137
138
138
/** Stops all invite/accept services, disconnects from the current session, and stops all searching activity */
139
- func end( ) {
139
+ public func end( ) {
140
140
stopSearching ( )
141
141
disconnect ( )
142
142
}
143
143
144
- var isConnected : Bool {
144
+ public var isConnected : Bool {
145
145
return connectedPeers. count > 0
146
146
}
147
147
@@ -150,7 +150,7 @@ class MultiPeer: NSObject {
150
150
/// - object: Object (Any) to send to all connected peers.
151
151
/// - type: Type of data (UInt32) sent
152
152
/// After sending the object, you can use the extension for Data, `convertData()` to convert it back into an object.
153
- func send( object: Any , type: UInt32 ) {
153
+ public func send( object: Any , type: UInt32 ) {
154
154
if isConnected {
155
155
let data = NSKeyedArchiver . archivedData ( withRootObject: object)
156
156
@@ -163,7 +163,7 @@ class MultiPeer: NSObject {
163
163
/// - data: Data (Data) to send to all connected peers.
164
164
/// - type: Type of data (UInt32) sent
165
165
/// After sending the data, you can use the extension for Data, `convertData()` to convert it back into data.
166
- func send( data: Data , type: UInt32 ) {
166
+ public func send( data: Data , type: UInt32 ) {
167
167
if isConnected {
168
168
do {
169
169
let container : [ Any ] = [ data, type]
@@ -188,15 +188,15 @@ class MultiPeer: NSObject {
188
188
extension MultiPeer : MCNearbyServiceAdvertiserDelegate {
189
189
190
190
// Received invitation
191
- func advertiser( _ advertiser: MCNearbyServiceAdvertiser , didReceiveInvitationFromPeer peerID: MCPeerID , withContext context: Data ? , invitationHandler: @escaping ( Bool , MCSession ? ) -> Void ) {
191
+ public func advertiser( _ advertiser: MCNearbyServiceAdvertiser , didReceiveInvitationFromPeer peerID: MCPeerID , withContext context: Data ? , invitationHandler: @escaping ( Bool , MCSession ? ) -> Void ) {
192
192
193
193
OperationQueue . main. addOperation {
194
194
invitationHandler ( true , self . session)
195
195
}
196
196
}
197
197
198
198
// Error, could not start advertising
199
- func advertiser( _ advertiser: MCNearbyServiceAdvertiser , didNotStartAdvertisingPeer error: Error ) {
199
+ public func advertiser( _ advertiser: MCNearbyServiceAdvertiser , didNotStartAdvertisingPeer error: Error ) {
200
200
printDebug ( " Could not start advertising due to error: \( error) " )
201
201
}
202
202
@@ -206,7 +206,7 @@ extension MultiPeer: MCNearbyServiceAdvertiserDelegate {
206
206
extension MultiPeer : MCNearbyServiceBrowserDelegate {
207
207
208
208
// Found a peer
209
- func browser( _ browser: MCNearbyServiceBrowser , foundPeer peerID: MCPeerID , withDiscoveryInfo info: [ String : String ] ? ) {
209
+ public func browser( _ browser: MCNearbyServiceBrowser , foundPeer peerID: MCPeerID , withDiscoveryInfo info: [ String : String ] ? ) {
210
210
printDebug ( " Found peer: \( peerID) " )
211
211
212
212
// Update the list and the controller
@@ -216,15 +216,15 @@ extension MultiPeer: MCNearbyServiceBrowserDelegate {
216
216
}
217
217
218
218
// Lost a peer
219
- func browser( _ browser: MCNearbyServiceBrowser , lostPeer peerID: MCPeerID ) {
219
+ public func browser( _ browser: MCNearbyServiceBrowser , lostPeer peerID: MCPeerID ) {
220
220
printDebug ( " Lost peer: \( peerID) " )
221
221
222
222
// Update the lost peer
223
223
availablePeers = availablePeers. filter { $0. peerID != peerID }
224
224
}
225
225
226
226
// Error, could not start browsing
227
- func browser( _ browser: MCNearbyServiceBrowser , didNotStartBrowsingForPeers error: Error ) {
227
+ public func browser( _ browser: MCNearbyServiceBrowser , didNotStartBrowsingForPeers error: Error ) {
228
228
printDebug ( " Could not start browsing due to error: \( error) " )
229
229
}
230
230
@@ -234,7 +234,7 @@ extension MultiPeer: MCNearbyServiceBrowserDelegate {
234
234
extension MultiPeer : MCSessionDelegate {
235
235
236
236
// Peer changed state
237
- func session( _ session: MCSession , peer peerID: MCPeerID , didChange state: MCSessionState ) {
237
+ public func session( _ session: MCSession , peer peerID: MCPeerID , didChange state: MCSessionState ) {
238
238
// If the new state is connected, then remove it from the available peers
239
239
// Otherwise, update the state
240
240
if state == . connected {
@@ -255,7 +255,7 @@ extension MultiPeer: MCSessionDelegate {
255
255
}
256
256
257
257
// Received data
258
- func session( _ session: MCSession , didReceive data: Data , fromPeer peerID: MCPeerID ) {
258
+ public func session( _ session: MCSession , didReceive data: Data , fromPeer peerID: MCPeerID ) {
259
259
printDebug ( " Received data: \( data. count) bytes " )
260
260
261
261
guard let container = data. convert ( ) as? [ Any ] else { return }
@@ -269,17 +269,17 @@ extension MultiPeer: MCSessionDelegate {
269
269
}
270
270
271
271
// Received stream
272
- func session( _ session: MCSession , didReceive stream: InputStream , withName streamName: String , fromPeer peerID: MCPeerID ) {
272
+ public func session( _ session: MCSession , didReceive stream: InputStream , withName streamName: String , fromPeer peerID: MCPeerID ) {
273
273
printDebug ( " Received stream " )
274
274
}
275
275
276
276
// Started receiving resource
277
- func session( _ session: MCSession , didStartReceivingResourceWithName resourceName: String , fromPeer peerID: MCPeerID , with progress: Progress ) {
277
+ public func session( _ session: MCSession , didStartReceivingResourceWithName resourceName: String , fromPeer peerID: MCPeerID , with progress: Progress ) {
278
278
printDebug ( " Started receiving resource with name: \( resourceName) " )
279
279
}
280
280
281
281
// Finished receiving resource
282
- func session( _ session: MCSession , didFinishReceivingResourceWithName resourceName: String , fromPeer peerID: MCPeerID , at localURL: URL ? , withError error: Error ? ) {
282
+ public func session( _ session: MCSession , didFinishReceivingResourceWithName resourceName: String , fromPeer peerID: MCPeerID , at localURL: URL ? , withError error: Error ? ) {
283
283
printDebug ( " Finished receiving resource with name: \( resourceName) " )
284
284
}
285
285
@@ -289,12 +289,12 @@ extension MultiPeer: MCSessionDelegate {
289
289
extension Data {
290
290
291
291
/** Unarchive data into an object. It will be returned as type `Any`. */
292
- func convert( ) -> Any {
292
+ public func convert( ) -> Any {
293
293
return NSKeyedUnarchiver . unarchiveObject ( with: self ) !
294
294
}
295
295
296
296
/** Converts an object into Data using NSKeyedArchiver */
297
- static func toData( object: Any ) -> Data {
297
+ public static func toData( object: Any ) -> Data {
298
298
return NSKeyedArchiver . archivedData ( withRootObject: object)
299
299
}
300
300
0 commit comments