Before you begin

When you first use Resonance SDK, you have to create account at console.getresonance.net and register an application to get access to API key and download Resonance SDK for iOS.

Note: You should use the same API key on all platforms in order to allow cross platform user matching.

Initializing Resonance

Before using Resonance API you must initialize the SDK using the API key of your application. To do this use setupWithApiKey: static method of RSNResonance class.

Example:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [RSNResonance setupWithApiKey: @"DEMO_API_KEY"];
    return YES;
}

Access to Microphone

The application needs to gain access to a devices’ microphone from the user in order to start search devices.

Request permission example:

AVAudioSessionRecordPermission permissionStatus = [[AVAudioSession sharedInstance] recordPermission];
if  (permissionStatus == AVAudioSessionRecordPermissionGranted){
        // permission has been granted earlier
        // you can start search
}
else {
        [AVAudioSession sharedInstance] [requestRecordPermission:^(BOOL granted) {
            if (granted) {
                // permission granted
                // you can start search
            }
            else {
                // user denied microphone access
            }
        }];
}

More info about record permission available here

Internet connection check

Before initiating a search it is desirable to make sure that the device has an active internet connection. Check out this sample provided by Apple.

Starting search

For devices to discover eachother, all of them must start a search. To do this you need to call startSearchingWithPayload method:

+ (void) startSearchingWithPayload: (NSString *_Nonnull)payload
              onSearchStartedBlock: (void(^_Nullable)()) onSearchStartedBlock
                onClientFoundBlock: (void(^_Nonnull)(RSNClient *_Nonnull client)) clientFoundBlock
                      onClientLost: (void(^_Nullable)(RSNClient *_Nonnull client)) clientLostBlock
              onSearchStoppedBlock: (void(^_Nonnull)(NSError *_Nullable error)) onSearchStopedBlock;

payload is data which will be delivered to nearby devices. Maximum length is 2048.

To receive a notification when a device is located nearby or if there was an error, you need to implement onClientFoundBlock and onSearchStoppedBlock blocks.

Example:

[RSNResonance startSearchingWithPayload: @"Some useful data to send"
                   onSearchStartedBlock: ^(){
                       NSLog(@"Search started");
                   }
                     onClientFoundBlock: ^(RSNClient * __Nonnul client){
                         NSLog(@"Nearby found with payload: %@", client.payload);
                     }
                           onClientLost: ^(RSNClient * __Nonnul client){
                               NSLog(@"Nearby lost with payload: %@", client.payload);
                           }
                   onSearchStoppedBlock: ^(NSError * _Nullable error){
                       if (error != nil) NSLog(@"Search stopped with error: %@", error);
                   }
 ];

Stopping search

To stop the search, you should call stopSearch. Do not forget to stop the search when the application is in background.

onSearchStoppedBlock is invoked when the search is stopped by application or when search is stopped due to an error – such as when there is no connection with the server. If the argument error is equal to null, then the search has been stopped by the application. If not then there was an error.