, поэтому у меня есть проект Xcode, в котором я использую платформу Multipeer Connectivity. Я попробовал это с iphone 6 на IOS 12.4.2 и iphone 6 на IOS 12.4.4. Они обнаруживают друг друга, но когда дело доходит до подключения, создается впечатление, что узел, пытающийся подключиться, отправляет неверные данные, поэтому он отключается по истечении времени ожидания. Это консольное сообщение:
2020-01-19 18:41:58.038549+0100 MultipeerConnectivityTest[25092:716509] libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
2020-01-19 18:42:02.577076+0100 MultipeerConnectivityTest[25092:716509] [MCNearbyServiceAdvertiser] PeerConnection connectedHandler (advertiser side) - error [Unable to connect].
2020-01-19 18:42:02.577256+0100 MultipeerConnectivityTest[25092:716509] [MCNearbyServiceAdvertiser] PeerConnection connectedHandler remoteServiceName is nil.
Nous avons recu une invitation de <MCPeerID: 0x600003f7c290 DisplayName = (.:/|\:.) 5G>
2020-01-19 18:42:13.150877+0100 MultipeerConnectivityTest[25092:716834] [MCNearbyServiceAdvertiser] Data from peer [(.:/|\:.) 5G,22BCEE4D] received with error Connection closed.
Ошибка говорит что-то о "remoteServiceName", но ни одна из моих переменных не имеет такого свойства.
Это мой код:
import MultipeerConnectivity
import UIKit
class ViewController: UIViewController{
//MARK: - Property
var peerID : MCPeerID!
var session : MCSession!
var advertiser : MCNearbyServiceAdvertiser!
var browsingService : MCNearbyServiceBrowser!
//MARK: - Load
override func viewDidLoad() {
super.viewDidLoad()
peerID = MCPeerID(displayName: UIDevice.current.name)
session = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .none)
session.delegate = self
advertiser = MCNearbyServiceAdvertiser(peer: peerID, discoveryInfo: nil, serviceType: "test")
advertiser.delegate = self
browsingService = MCNearbyServiceBrowser(peer: peerID, serviceType: "test")
browsingService.delegate = self
advertiser.startAdvertisingPeer()
}
@IBAction func startAdvertising(_ sender: Any) {
browsingService.startBrowsingForPeers()
//print(browsingService)
}
@IBAction func stopAdvertising(_ sender: Any) {
browsingService.stopBrowsingForPeers()
//print(browsingService)
}
}
//MARK: - Delegate Extensions
extension ViewController : MCSessionDelegate{
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
switch state {
case .connected:
print("La peer \(peerID) vient de se connecter")
case .connecting:
print("La peer \(peerID) se connecte")
case .notConnected:
print("La peer \(peerID) vient de se deconnecter")
@unknown default:
fatalError()
}
}
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
var message : String
do {
message = try JSONDecoder().decode(Message.self, from: data).message
print("Message recu de \(peerID) : \(message)")
}catch {
print("Impossibilité de décoder la data envoyée par \(peerID)")
}
}
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) { //Pas de stream dans notre programme
}
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
print("Donné en corus de réception de \(peerID) : \(progress)")
}
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) {
}
}
extension ViewController : MCNearbyServiceBrowserDelegate {
func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
print("Nous avons identifié la peer \(peerID)")
//print("Invitation")
browsingService.invitePeer(peerID, to: session, withContext: nil, timeout: 10)
}
func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
print("La peer \(peerID) vient de disparaitre de nos radars")
}
}
extension ViewController: MCNearbyServiceAdvertiserDelegate {
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {
if advertiser.serviceType == "titouan-test"{
print("Nous avons recu une invitation de \(peerID)")
}
}
}
Я француз, вот почему print are engli sh, но они не очень важны, если вы хорошо работаете с Multipeer Connectivity.