Friday, 18 December 2015

UIDatePicker


Sometimes we need  of a date and time picker in our app to pick a date and time . Today I am going to  demonstrate how to use Apple's inbuilt UIDatePicker to pick the date and time. It is very simple.

To use UIDatePicker in an app , first of all create a UIDatePicker instance and set mode as what we want  to have in our picker, only date/time or both. As in my case I want to have both in my app . So i set the mode to  UIDatePickerModeDateAndTime .

//create an  instance of the NSDatePicker
UIDatePicker  *datePicker = [[UIDatePicker alloc] initWithFrame: CGRectZero];

//set mode
[datePicker setDatePickerMode:UIDatePickerModeDateAndTime];


Then add a selector to the date picker that will be called when user want to select the date and time from  datePicker .

//add target

[datePicker addTarget:self action:@selector(selectDateAndTime:) forControlEvents:UIControlEventValueChanged];

Thats all, we have configured the datePicker. Now add the datePicker to the control when we want to datePicker to be appeared.
As I want datePicker to be appeared when user tap on a textField, So i add the datePicker to texField's accessory view as:

//add datePicker as accessory view of the textField:
self.txt_Time.inputView = datePicker ;


Now implement the method that we have assigned as a selector to datePicker:

-(void)selectDateAndTime:(UIDatePicker*)picker{
    NSDateFormatter *dateFormatter  = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"hh:mm a dd-MM-yyyy "];
     NSString *formattedString = [dateFormatter stringFromDate:[datePicker date]];
     self.txt_time.text = formattedString;
}


That is it. How easy it is. 

Friday, 4 December 2015

Fetch user profile data after login into Facebook

It is easy to integrate Facebook  login in an iOS app . To integrate Facebook login , first of all we have to configure out app at developer site of the Facebook and  get and app ID which will be used in Xcode configuration . 

We can follow these easy steps form developer site of the Facebook. 

Here I am emphasizing how to get the user profile data once user is successfully login in an app from Facebook.

Example:  First create an IBAction  for a button to login Facebook. and define the method to login and fetch data.

  
    if ([FBSDKAccessToken currentAccessToken]) {
     // 1 user is already login, handle it.
    }else{
    
     //2 log in to Facebook first time
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login
     logInWithReadPermissions: @[@"public_profile",@"email",@"user_about_me"]
     fromViewController:self
     handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
         if (error) {
          
         //3 handle error   
             NSLog(@"Process error %@", error.localizedDescription);
             
         } else if (result.isCancelled) {
             
             NSLog(@"Cancelled");
             
         } else {
        //4login success, proceed next steps
             
             if ([FBSDKAccessToken currentAccessToken])
             {
                 
                 [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"picture,name,location, email"}]
                  startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                      
                      if (!error) {
                          NSLog(@"fetched user:%@", result);
                          
                          //fetch email and profile picture
                          NSString *email = [result objectForKey:@"email"];
                          NSString *user_name = [result objectForKey:@"name"];
                         
                          NSString *pictureURL = [[[result objectForKey:@"picture"]objectForKey:@"data"]objectForKey:@"url"];

                          NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:pictureURL]];
                          
                          
                     }else{
                          NSLog(@"error in fetching user data==%@", error.localizedDescription);
                      }
                  }];
             
             }
            
         }
     }];
    
    }

Now lets describe the above code:

1)We are checking, if user is already login , we don't follow login process.
2)If user is not login through Facebook, then first login him.Here we have to assign some permission that we want to access his profile during login .

      [login logInWithReadPermissions:       @[@"public_profile",@"email",@"user_about_me"]

3) In completion handler, we check for error, cancel of the login process and success of the login and handle accordingly.

4) If login is success,  we get an access token from Facebook . Here we check for access token , if it is , then we fetch the  required data.