iOS Developer Library — Pre-Release

Developer

MultipeerConnectivity Framework Reference MCNearbyServiceBrowser Class Reference

Options
Deployment Target:

On This Page
Language:

MCNearbyServiceBrowser

Searches (by service type) for services offered by nearby devices using infrastructure Wi-Fi, peer-to-peer Wi-Fi, and Bluetooth, and provides the ability to easily invite those devices to a Multipeer Connectivity session (MCSession).

Inheritance


Conforms To


Import Statement


Swift

import MultipeerConnectivity

Objective-C

@import MultipeerConnectivity;

Availability


Available in iOS 7.0 and later.
  • Initializes the nearby service browser object.

    Declaration

    Swift

    init!(peer myPeerID: MCPeerID!, serviceType serviceType: String!)

    Objective-C

    - (instancetype)initWithPeer:(MCPeerID *)myPeerID serviceType:(NSString *)serviceType

    Parameters

    myPeerID

    The local peer ID for this instance.

    serviceType
    • Must be 1–15 characters long

    • Can contain only ASCII lowercase letters, numbers, and hyphens.

    This name should be easily distinguished from unrelated services. For example, a text chat app made by ABC company could use the service type abc-txtchat.

    For more details, read Domain Naming Conventions.

    Return Value

    Returns an initialized nearby service browser object, or nil if an error occurs.

    Discussion

    This method throws an exception if the session or serviceType parameters do not contain valid objects or the specified Bonjour service type is not valid.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • delegate delegate Property

    The delegate object that handles browser-related events.

    Declaration

    Swift

    weak var delegate: MCNearbyServiceBrowserDelegate!

    Objective-C

    @property(weak, nonatomic) id< MCNearbyServiceBrowserDelegate > delegate

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • myPeerID myPeerID Property

    The local peer ID for this instance. (read-only)

    Declaration

    Swift

    var myPeerID: MCPeerID! { get }

    Objective-C

    @property(readonly, nonatomic) MCPeerID *myPeerID

    Discussion

    This value is set when you initialize the object, and cannot be changed later.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • The service type to browse for. (read-only)

    Declaration

    Swift

    var serviceType: String! { get }

    Objective-C

    @property(readonly, nonatomic) NSString *serviceType

    Discussion

    This value is set when you initialize the object, and cannot be changed later.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • Starts browsing for peers.

    Declaration

    Swift

    func startBrowsingForPeers()

    Objective-C

    - (void)startBrowsingForPeers

    Discussion

    After this method is called (until you call stopBrowsingForPeers), the framework calls your delegate's browser:foundPeer:withDiscoveryInfo: and browser:lostPeer: methods as new peers are found and lost.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • Stops browsing for peers.

    Declaration

    Swift

    func stopBrowsingForPeers()

    Objective-C

    - (void)stopBrowsingForPeers

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • Invites a discovered peer to join a Multipeer Connectivity session.

    Declaration

    Swift

    func invitePeer(_ peer: MCPeerID!, toSession session: MCSession!, withContext context: NSData!, timeout timeout: NSTimeInterval)

    Objective-C

    - (void)invitePeer:(MCPeerID *)peer toSession:(MCSession *)session withContext:(NSData *)context timeout:(NSTimeInterval)timeout

    Parameters

    peer

    The ID of the peer to invite.

    session

    The session you wish the invited peer to join.

    context

    An arbitrary piece of data that is passed to the nearby peer. This can be used to provide further information to the user about the nature of the invitation.

    timeout

    The amount of time to wait for the peer to respond to the invitation.

    This timeout is measured in seconds, and must be a positive value. If a negative value or zero is specified, the default timeout (30 seconds) is used.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.