Sunday, February 16, 2014

Multipeer Connectivity in iOS 7 Part 1

In this blog we will discuss about how to achieve multi peer connectivity using iOS 7 new Muti peer connectivity Framework. We will do it in multiple parts. In this part we will discuss how to discover the near by devices and connect them using the BrowserViewController.


In the first part we will discuss how can we use the view controller provided by the MultipeerConnectivity.framework. Classes in the Framework are prefixed with MC so from now we will use MC for mentioning the Multipeer Connectivity.

So What are the advantages of using the MC framework? Here are few mentioned in the WWDC sessions.


  • Interactive Tutorials.
  • Collaborative Documentation/ Photo Editing.
  • File Sharing.
  • Coordination across multiple devices.
  • Sensor Data Aggregation.
I am sure we can explore more use cases as we get to know the framework and the innovative mind will come with something interesting.

The main terms which are introduced with this framework are Advertiser and Browser.  As the names suggests Advertiser is someone who makes himself available to be searchable to other devices and on the contrary Browser is a device who is looking for the advertisers. Here is the simple view of both advertiser and the browser.


Lets start doing some coding...

Here is the view which I used just to test the connectivity of the devices. So I have implemented both the Browser and the Advertisers in the same App.


Simple app with two buttons.

Here is the code of the HandshakeViewController.h. As you can see it implements the MCBrowserViewControllerDelegate and the MCSessionDelegate which takes are of the most of the functionality.

#import <UIKit/UIKit.h>
#import "MultipeerConnectivity/MCPeerID.h"
#import "MultipeerConnectivity/MCSession.h"
#import "MultipeerConnectivity/MCAdvertiserAssistant.h"
#import "MultipeerConnectivity/MCBrowserViewController.h"

@interface HandshakeViewController : UIViewController<MCBrowserViewControllerDelegate, MCSessionDelegate>

@property (strong, nonatomic) MCPeerID *mcBrowserPeerID;
@property (strong, nonatomic) MCSession *mcBrowserSession;
@property (strong, nonatomic) MCPeerID *mcAdvertiserPeerID;
@property (strong, nonatomic) MCSession *mcAdvertiserSession;
@property (strong, nonatomic) MCAdvertiserAssistant *advertisingAssistant;
@property (strong, nonatomic) MCBrowserViewController *browserController;


-(IBAction)btnStartBrowsing:(id)sender;
-(IBAction)btnStartAdvertising:(id)sender;

@end

And here is the code of the HandshakeViewController.m.   file.

import "HandshakeViewController.h"

@interface HandshakeViewController ()

@property (weak, nonatomic) IBOutlet UILabel *connectionStatus;

@end

@implementation HandshakeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)btnStartAdvertising:(id)sender
{
   // Advertiser Code - Customer.
   if (_mcAdvertiserPeerID == nil) {
      self.mcAdvertiserPeerID = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];
   }
   
   //Setup Session
   if (_mcAdvertiserSession == nil) {
      self.mcAdvertiserSession = [[MCSession alloc] initWithPeer:self.mcAdvertiserPeerID];
   }
   
   self.mcAdvertiserSession.delegate = self;
   
   //Setup Advertising Assistant.
   if(_advertisingAssistant == nil){
      self.advertisingAssistant = [[MCAdvertiserAssistant alloc] initWithServiceType:@"Chat" discoveryInfo:nil session:self.mcAdvertiserSession];
   }
   // Start the advertiser.
   [self.advertisingAssistant start];
   
}

-(IBAction)btnStartBrowsing:(id)sender
{
   // Browser's Code. Clerk
   if (_mcBrowserPeerID == nil) {
      self.mcBrowserPeerID = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];
   }
   // Setup Browser Serssion
   if (_mcBrowserSession == nil) {
      self.mcBrowserSession = [[MCSession alloc] initWithPeer:self.mcBrowserPeerID];
   }
   
   self.mcBrowserSession.delegate = self;
   
   // Setup the Browser Controller.
   self.browserController = [[MCBrowserViewController alloc] initWithServiceType:@"Chat" session
                                                                                :self.mcBrowserSession];
   
   self.browserController.delegate = self;
   
   [self presentViewController:self.browserController animated:YES completion:nil];
   
}

- (void) browserViewControllerDidFinish:(MCBrowserViewController *)browserVC
{
   [browserVC dismissViewControllerAnimated:YES completion:nil];
}

- (void) browserViewControllerWasCancelled:(MCBrowserViewController *)browserVC
{
   [browserVC dismissViewControllerAnimated:YES completion:nil];
}

-(void) session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress{
   
}

- (void) session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{
   
}

-(void) session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID{
   
}

-(void) session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error{
   
}

-(void) session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
   switch (state) {
      case MCSessionStateConnected:
         self.connectionStatus.text=@"Connected";
         break;
      case MCSessionStateConnecting:
         
         break;
      case MCSessionStateNotConnected:
         
         break;
      default:
         break;
   }
}

@end

Below are two Actions which you would like to take a look at.

btnStartAdvertising

Clicking the Start Advertising button will start a new advertiser with a new MCSession. 

btnStartBrowsing

Clicking on the start browsing will open the Browser viewcontroller. Which will list all the advertisers and you can pick the ones which you want to connect to and once the device you want to connect to accept your invitation you can see the status changed to Connected.

This is just the work of the beginner, there may be better way to do it. I just wanted to share what I learned. 

Please share your comments. 

Keep reading!!

Happy Coding.







No comments:

Post a Comment