-
The problem
Continue with the above [iOS to achieve WIFI book transfer], what if the LAN is removed? Whether files can be transferred between devices
Common data transfer modes between iOS devices:
Bluetooth 2. Airdrop 3.MultipeerConnectivityCopy the code
This article is about MultipeerConnectivity
-
serving
The device serves as the server
- Have a PeerID that identifies you
lazy var me: MCPeerID = { let peer = MCPeerID(displayName: UIDevice.current.name) return peer }() Copy the code
- A session
lazy var session: MCSession = { let ss = MCSession(peer: me, securityIdentity: nil, encryptionPreference: .none) ss.delegate = self return ss }() Copy the code
- Broadcast it so other devices can find it
lazy var advertiser: MCNearbyServiceAdvertiser = { let advertiser = MCNearbyServiceAdvertiser(peer: me, discoveryInfo: ["demo": "data"], serviceType: "shanzhai") advertiser.delegate = self return advertiser }() Copy the code
- Began to broadcast
advertiser.startAdvertisingPeer() Copy the code
- Broadcast proxy method execution
extension MultipeerConnectVC: MCNearbyServiceAdvertiserDelegate { func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data? , invitationHandler: @escaping (Bool, MCSession?) -> Void) {// Confirm connection, assign session invitationHandler(true, session)} func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: Error) { advertiser.stopAdvertisingPeer() print("Woops! Advertising failed with error \(String(describing: error))") } }Copy the code
The device serves as the client
- Have a PeerID that identifies you
lazy var me: MCPeerID = { let peer = MCPeerID(displayName: UIDevice.current.name) return peer }() Copy the code
- A session
lazy var session: MCSession = { let ss = MCSession(peer: me, securityIdentity: nil, encryptionPreference: .none) ss.delegate = self return ss }() Copy the code
- A search is required for PeerID
lazy var browser: MCNearbyServiceBrowser = { let bs = MCNearbyServiceBrowser(peer: me, serviceType: "shanzhai") bs.delegate = self return bs }() Copy the code
- Begin your search
browser.startBrowsingForPeers() Copy the code
- The nearby PeerID was retrieved from the proxy, which can be saved by itself or directly linked according to business requirements
extension MultipeerConnectVC: MCNearbyServiceBrowserDelegate { func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {print("search peerId = \(peerId)") {print("search peerId = \(peerId)") {print("search peerId = \(peerId)"); session, withContext: nil, timeout: 10) } func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) { print("lost peerID = \(peerID)") } func browser(_ browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: Error) { print("error = \(error)") } }Copy the code
The connection process looks at the Session, and the Session agent looks at each process
extension MultipeerConnectVC: MCSessionDelegate { func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) { } func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) { } func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) { } func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) { } func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL? , withError error: Error?) {}}Copy the code
-
Agent method didChange. MCSessionState ==. Connected in didChange
-
DidFinishReceivingResourceWithName agent method, you can view the received data
-
You can also use MCBrowserViewController, the PeerID that you find, you don’t have to write your own interface
-
conclusion
This mode does not connect iOS and Android, and is applicable to non-network data transfer between Apple devices. The way FileChat is implemented across Apple devices should be based on this