iOS Developer Library

Developer

MultipeerConnectivity Framework Reference MCNearbyServiceAdvertiser Class Reference

Options
Deployment Target:

On This Page
Language:

MCNearbyServiceAdvertiser

The MCNearbyServiceAdvertiser class publishes an advertisement for a specific service that your app provides through the Multipeer Connectivity framework and notifies its delegate about invitations from nearby peers.

Before you can advertise a service, you must create an MCPeerID object that identifies your app and the user to nearby devices.

The serviceType parameter is a short text string used to describe the app's networking protocol. It should be in the same format as a Bonjour service type: 1–15 characters long and valid characters include ASCII lowercase letters, numbers, and the hyphen. A short name that distinguishes itself from unrelated services is recommended; for example, a text chat app made by ABC company could use the service type "abc-txtchat". For more information about service types, read Domain Naming Conventions.

The discoveryInfo parameter is a dictionary of string key/value pairs that will be advertised for browsers to see. The content of discoveryInfo will be advertised within Bonjour TXT records, so you should keep the dictionary small for better discovery performance.

For more information about TXT records, read Bonjour Operations.

Inheritance


Conforms To


Import Statement


Swift

import MultipeerConnectivity

Objective-C

@import MultipeerConnectivity;

Availability


Available in iOS 7.0 and later.
  • Initializes an advertiser object.

    Declaration

    Swift

    init!(peer myPeerID: MCPeerID!, discoveryInfo info: [NSObject : AnyObject]!, serviceType serviceType: String!)

    Objective-C

    - (instancetype)initWithPeer:(MCPeerID *)myPeerID discoveryInfo:(NSDictionary *)info serviceType:(NSString *)serviceType

    Parameters

    myPeerID

    Your app’s local peer ID.

    info

    A dictionary of key-value pairs that are made available to browsers. Each key and value must be an NSString object.

    This data is advertised using a Bonjour TXT record, encoded according to RFC 6763 (section 6). As a result:

    • The key-value pair must be no longer than 255 bytes (total) when encoded in UTF-8 format with an equals sign (=) between the key and the value.

    • Keys cannot contain an equals sign.

    For optimal performance, the total size of the keys and values in this dictionary should be no more than about 400 bytes so that the entire advertisement can fit within a single Bluetooth data packet. For details on the maximum allowable length, read Monitoring a Bonjour Service.

    If the data you need to provide is too large to fit within these constraints, you should create a custom discovery class using Bonjour for discovery and your choice of networking protocols for exchanging the information.

    serviceType

    The type of service to advertise. This should be a short text string that describes the app's networking protocol, in the same format as a Bonjour service type:

    • 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 instance, or nil if an error occurred.

    Discussion

    This method throws an exception if a valid peerID object is not provided or if the value of serviceType is not a legal Bonjour service type.

    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 advertising-related events.

    Declaration

    Swift

    weak var delegate: MCNearbyServiceAdvertiserDelegate!

    Objective-C

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

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • The info dictionary passed when this object was initialized. (read-only)

    Declaration

    Swift

    var discoveryInfo: [NSObject : AnyObject]! { get }

    Objective-C

    @property(readonly, nonatomic) NSDictionary *discoveryInfo

    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.

  • 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 that your app is advertising (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.

  • Begins advertising the service provided by a local peer.

    Declaration

    Swift

    func startAdvertisingPeer()

    Objective-C

    - (void)startAdvertisingPeer

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • Stops advertising the service provided by a local peer.

    Declaration

    Swift

    func stopAdvertisingPeer()

    Objective-C

    - (void)stopAdvertisingPeer

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.