multipeer1
Uncategorized

Hey Folks,

I have been recently exploring the Multipeer connectivity framework iOS and i thought sharing the same would be really helpful for those who have unexplored this framework.

Well First things first. Few questions & answers :

  • Why use Multipeer connectivity ?
    • If you are looking forward for connecting two or more iPhones via bluetooth or wifi and that need file sharing or needs something like chat application, you are on right track
  • What is Multipeer ?
    • Multipeer framework is build for iPhone to connect peripheral devices, We can use it to connect to iPhones, iWatch, iBeacons and other bluetooth accessories too. Its basic framework that defines protocol for peer to peer connections

Enough talking, lets dig into coding part.

Pre-requisite for implementing peer to peer connectivity.

  • iPhone (Physical Device)
  • Simulator OR another iPhone or Bluetooth peripheral devices
  • Basic overview of Peer commands and full understanding of sending bytes in packets through encryptions. (Lolz just kidding)

So lets Start.

Step 1 : Add Multipeer Connectivity Framework

Screen Shot 2016-06-17 at 9.10.09 PM

Step 2 : Create two files Master & Slave

Also download Multipeer this 2 files

Master would be searching for another device & slave would advertise itself that i am available for connection. (Same like Groom/Bride Searching :P).

So here is code for Master :

#import <MultipeerConnectivity/MultipeerConnectivity.h>
@import MultipeerConnectivity;
@interface MasterVC : UIViewController <MCBrowserViewControllerDelegate>
- (IBAction)browseForDevices:(id)sender;
@end

For Master.m

// in your appdelegate

@property (nonatomic, strong) MPManager *mpManager;

_mpManager = [[MPManager alloc] init];

// In your master.m

- (IBAction)browseForDevices:(id)sender {
    [[_appDelegate mpManager] setupMCBrowser];
    [[[_appDelegate mpManager] browser] setDelegate:self];
    [self presentViewController:[[_appDelegate mpManager] browser] animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didStartReceivingResourceWithNotification:)
                                                 name:@"MCDidStartReceivingResourceNotification"
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateReceivingProgressWithNotification:)
                                                 name:@"MCReceivingProgressNotification"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didFinishReceivingResourceWithNotification:)
                                                 name:@"didFinishReceivingResourceNotification"
                                               object:nil];
}

#pragma mark - Public method implementation

- (IBAction)browseForDevices:(id)sender {
    [[_appDelegate mpManager] setupMCBrowser];
    [[[_appDelegate mpManager] browser] setDelegate:self];
    [self presentViewController:[[_appDelegate mpManager] browser] animated:YES completion:nil];
}


#pragma mark - MCBrowserViewControllerDelegate method implementation

-(void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController{
    [_appDelegate.mpManager.browser dismissViewControllerAnimated:YES completion:nil];
    [_tblConnectedDevices reloadData];
}


-(void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController{
    [_appDelegate.mpManager.browser dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Private method implementation

-(void)peerDidChangeStateWithNotification:(NSNotification *)notification{
    MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
    NSString *peerDisplayName = peerID.displayName;
    MCSessionState state = [[[notification userInfo] objectForKey:@"state"] intValue];
    
    if (state != MCSessionStateConnecting) {
        if (state == MCSessionStateConnected) {
            [_arrConnectedDevices addObject:peerDisplayName];
        }
        else if (state == MCSessionStateNotConnected){
            if ([_arrConnectedDevices count] > 0) {
                @try {
                    int indexOfPeer = [_arrConnectedDevices indexOfObject:peerDisplayName];
                    [_arrConnectedDevices removeObjectAtIndex:indexOfPeer];
                } @catch (NSException *exception) {
                    
                } @finally {
                    
                }
                
            }
        }
        [_tblConnectedDevices reloadData];
        
        BOOL peersExist = ([[_appDelegate.mpManager.session connectedPeers] count] == 0);
    }
}

And For Slave

- (void)viewDidLoad {
    [super viewDidLoad];
    _appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [[_appDelegate mpManager] setupPeerAndSessionWithDisplayName:[UIDevice currentDevice].name];
    [[_appDelegate mpManager] advertiseSelf:YES];
}

-(void)didStartReceivingResourceWithNotification:(NSNotification *)notification{


}
-(void)didFinishReceivingResourceWithNotification:(NSNotification *)notification{

}

Step 3 : Send Data to Slave Device

NSData *dataToSend = [@"play" dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *allPeers = _appDelegate.mpManager.session.connectedPeers;
    NSError *error;
    
    [_appDelegate.mpManager.session sendData:dataToSend
                                     toPeers:allPeers
                                    withMode:MCSessionSendDataReliable
                                       error:&error];

Step 4 : Test & Debug :)

Happy Multipeering. Feel free to contact me.

screen-shots-on-mobile_01
Uncategorized

Hello readers,

Today i am going to show, what could happen if you take a screenshot of any app content for some user. Snapchat users get notified that this user has taken a screenshot of your post.

This is what we are going to implement.

It is not that hard as it sounds, iOS 7.0 and later gives you an observer in NSNotificationCenter named UIApplicationUserDidTakeScreenshotNotification.

Just add this observer in the view did load and you can add a selector in this to perform any other task after a screenshot is taken.

Add the following code in the ViewDidLoad of you view controller.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenshotDetected) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
-(void)anotherAlert {
    UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:@"Hey!"
                                          message:@"Did you smile...???"
                                          preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction
                               actionWithTitle:NSLocalizedString(@"Lets see", @"OK action")
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action)
                               {
                                   NSLog(@"OK action");
                               }];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

You can perform any selector in this, i have added an alert controller, you can get the details of the user logged in, and can perform a notification.

More in this screenshot stuff, i am currently trying to take a picture from the front camera, when a screenshot is taken.

screen-shot
Uncategorized

Hello again readers, we were just thinking a new category to our blog posts, so here we bring you a post on watchOS. Yes this post will guide you through the basics of how to develop an app for the Apple watch.

This post will help you create a simple table view which will display a label and a detail Interface controller which will contain the details of the cell selected in the table view.

Simulator Screen Shot Sep 27, 2016, 10.55.44 AM

Tables basically shows the list of data whose contents changes dynamically. The class from which it inherits is WKInterfaceTable class. Note that tables on watch OS supports only single columns.

The layout should be set in the interface of the watch extension and the data to be displayed should be displayed runtime.

Screen Shot 2016-09-27 at 11.07.36 AM

MainWC is the Scene which will be displayed first as the watch app launches, I have placed a row controller and a label in it called “Country Name”, give the outlet to this label in the row controller class. This will be the custom class for you row controller. You can add one or more row controllers in the Scene as per your requirement. The row controller is a template used for displaying the content in the app at run time.

For this first we need to determine the number of rows that will be displayed in the app. The method in which we pass the number of rows is setNumberOfRows:withRowType: and we can use this to iterate over the total number and add content on each row with rowControllerAtIndex:.

This is the array I have used to insert the data.

arrWeekDay = [[NSMutableArray alloc] initWithObjects:@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday", nil];

[self.weekName setNumberOfRows:arrWeekDay.count withRowType:@"city"];
NSInteger rowCount = self.weekName.numberOfRows;
// Iterate over the rows and set the label for each one.
for (NSInteger i = 0; i < rowCount ; i++) {
    // Get the to-do item data.
    NSString* itemText = arrWeekDay[i];
    // Assign the text to the row's label.
    WeekDay* row = [self.weekName rowControllerAtIndex:i];
    [row.lblName setText:itemText];
}

Now the part is running the watch app. Go to the target selection in Xcode and select the watch extension. Set the device as specified, iPhone 7 + Apple Watch Series 2 (this will be shown if you have xcode 8, in Xcode 7.2 iPhone 7 will not be there.) Hit the play button.

You will be shown a new simulator specially made for watch, the watch app run there.

          Simulator Screen Shot Sep 27, 2016, 10.56.50 AM          Simulator Screen Shot Sep 27, 2016, 10.56.34 AM

Now we will see what can we do if we select the row of this table,

There is a similar method as for the iOS, didSelectRowAtIndexPath for the WatchOS called didselectrowatindex. This is how you use it,

-(void)table:(WKInterfaceTable *)countryName didSelectRowAtIndex:(NSInteger)rowIndex
{
    NSString *data = [arrCityName objectAtIndex:rowIndex];
    [self presentControllerWithName:@"detailWC" context:data];
}

Here the present controller with name will present the scene and the context will send the data to the next presenting controller. We can use this data in the next controller.

Here is have passed the selected day to the next controller and displayed it through the context in the next controller in a label.

zRun it again and select any day, a new interface will be presented, with the day you selected as the label in it.
Simulator Screen Shot Sep 27, 2016, 11.56.47 AM          Simulator Screen Shot Sep 27, 2016, 11.56.42 AM

Thats all about how to display data in tables in WatchOs, for further practice try to generate different results on the select, calculations with the help of the data from frameworks. Thanks.