I'm starting to use iOS' Multipeer Connectivity and have the following:
class Connector : NSObject, ObservableObject
{
@Published var peers = [MCPeerID]()
@Published var event: String?
private let serviceType = "app"
private let peerId = MCPeerID(displayName: UIDevice.current.name)
private let serviceAdvertiser: MCNearbyServiceAdvertiser
private let serviceBrowser: MCNearbyServiceBrowser
private let session: MCSession
private let log = Logger()
override init()
{
session = MCSession(peer: peerId, securityIdentity: nil, encryptionPreference: .none)
serviceAdvertiser = MCNearbyServiceAdvertiser(peer: peerId,
discoveryInfo: ["event" : "hello"],
serviceType: serviceType)
serviceBrowser = MCNearbyServiceBrowser(peer: peerId, serviceType: serviceType)
super.init()
session.delegate = self
serviceAdvertiser.delegate = self
serviceBrowser.delegate = self
serviceAdvertiser.startAdvertisingPeer()
serviceBrowser.startBrowsingForPeers()
}
deinit
{
serviceAdvertiser.stopAdvertisingPeer()
serviceBrowser.stopBrowsingForPeers()
}
}
extension Connector : MCNearbyServiceAdvertiserDelegate
{
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: Error)
{
log.error("ServiceAdvertiser didNotStartAdvertisingPeer: \(String(describing: error))")
}
func advertiser(_ advertiser: MCNearbyServiceAdvertiser,
didReceiveInvitationFromPeer peerID: MCPeerID,
withContext context: Data?,
invitationHandler: @escaping (Bool, MCSession?) -> Void)
{
log.info("didReceiveInvitationFromPeer \(peerID)")
}
}
... <basic other delegate stuff> ...
When I first create a Connector
object, iOS pops up a typical alert asking if the user allows finding and connecting to other devices (together with the mandatory .plist privacy text I've set).
When I tap "Don't Allow", the advertiser(_:didNotStartAdvertisingPeer:)
delegate method is not called. I also don't see anything in the iOS APIs by which I can see (later, e.g. when app restarts) if the user has granted this or not.
Is there a way to figure out what the user had chosen, like this is possible with camera access, location access, ...?