How to check if user has granted Multipeer Connectivity to connect to other devices?
Asked Answered
C

0

0

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, ...?

Cardinale answered 16/1, 2023 at 20:49 Comment(5)
The following might be considered a duplicate, if not at least helpful: iOS 14 How to trigger Local Network dialog and check user answer?. Lots of answers with ideas. Even some of the related questions on that page can be helpful.Penick
@Penick Thanks a lot for this link. But why did you delete your answer below?Cardinale
Just after posting the answer I found serious flaw. If I'm able to get it working I'll update it.Penick
@Penick What's the serious flaw?Cardinale
On a real iOS device it only works when the Local Network permission has been denied. Otherwise the code hangs and never gives a response. It works better on a simulator but that's not a good test.Penick

© 2022 - 2024 — McMap. All rights reserved.