MCNearbyServiceBrowser Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/MultipeerConnectivity.framework
Availability
Available in iOS 7.0 and later.
Declared in
MCNearbyServiceBrowser.h

Overview

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).

Tasks

Initializing the Browser

Browsing for Peers

Inviting Peers

Properties

delegate

The delegate object that handles browser-related events.

@property(assign, nonatomic) id<MCNearbyServiceBrowserDelegate> delegate
Availability
  • Available in iOS 7.0 and later.
Declared In
MCNearbyServiceBrowser.h

myPeerID

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

@property(readonly, nonatomic) MCPeerID *myPeerID
Discussion

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

Availability
  • Available in iOS 7.0 and later.
Declared In
MCNearbyServiceBrowser.h

serviceType

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

@property(readonly, nonatomic) NSString *serviceType
Discussion

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

Availability
  • Available in iOS 7.0 and later.
Declared In
MCNearbyServiceBrowser.h

Instance Methods

initWithPeer:serviceType:

Initializes the nearby service browser object.

- (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.

Availability
  • Available in iOS 7.0 and later.
Declared In
MCNearbyServiceBrowser.h

invitePeer:toSession:withContext:timeout:

Invites a discovered peer to join a Multipeer Connectivity session.

- (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.

Important: The nearby peer should treat any data it receives as potentially untrusted. To learn more about working with untrusted data, read Secure Coding Guide.

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.

Availability
  • Available in iOS 7.0 and later.
Declared In
MCNearbyServiceBrowser.h

startBrowsingForPeers

Starts browsing for peers.

- (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.

Availability
  • Available in iOS 7.0 and later.
Declared In
MCNearbyServiceBrowser.h

stopBrowsingForPeers

Stops browsing for peers.

- (void)stopBrowsingForPeers
Availability
  • Available in iOS 7.0 and later.
Declared In
MCNearbyServiceBrowser.h