Question

I am trying the facebook integration for the first time. I am confused of many codes i am able to get from different tutorial sites. I have used the following code to fetch user information from facebook data but i am not able to get the data using storyboard. Can someone suggest what is wrong with this code??

 - (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {

BOOL wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];


return wasHandled;
}

The following code paragraph under here is not getting excecuted. So is there some other way to get the control into this code??

 - (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                         user:(id<FBGraphUser>)user {
 regisrationdetails.fbid = user.id;
 regisrationdetails.firstname = user.first_name;
 }

Rest everything i tried different types to call it but its not happening.

- (void)loginView:(FBLoginView *)loginView
  handleError:(NSError *)error {
NSString *alertMessage, *alertTitle;
if (error.fberrorShouldNotifyUser) {

    alertTitle = @"Facebook Error";
    alertMessage = error.fberrorUserMessage;
} else if (error.fberrorCategory == FBErrorCategoryAuthenticationReopenSession) {



    alertTitle = @"Session Error";
    alertMessage = @"Your current session is no longer valid. Please log in again.";
} else if (error.fberrorCategory == FBErrorCategoryUserCancelled) {


    NSLog(@"user cancelled login");
} else {

    alertTitle  = @"Unknown Error";
    alertMessage = @"Error. Please try again later.";
    NSLog(@"Unexpected error:%@", error);
}

if (alertMessage) {
    [[[UIAlertView alloc] initWithTitle:alertTitle
                                message:alertMessage
                               delegate:nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil] show];
}
}


- (void)viewDidLoad
{
[FBLoginView class];

FBLoginView *loginView = [[FBLoginView alloc] init];
loginView.readPermissions= @[@"email", @"user_likes"];
loginView.publishPermissions = @[@"publish_actions"];
loginView.defaultAudience = FBSessionDefaultAudienceFriends;





FBSession *session = [[FBSession alloc] init];

[FBSession setActiveSession:session];

[session openWithBehavior:FBSessionLoginBehaviorWithNoFallbackToWebView
        completionHandler:^(FBSession *session,
                            FBSessionState status,
                            NSError *error) {


        }];
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    if (!error) {

        regisrationdetails.firstname = [result objectForKey:@"name"];
    }
}];

NSLog(@"%@", regisrationdetails.firstname);

[FBRequestConnection
 startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                   id<FBGraphUser> user,
                                   NSError *error) {
     if (!error)    {
         regisrationdetails.firstname = user.name;

         regisrationdetails.address1 = user.location[@"name"];


         regisrationdetails.city = user[@"city"];



         }
 }];


 - (IBAction)CreateAccount:(id)sender
 {


regisrationdetails.firstname=_FirstName.text;
regisrationdetails.lastname=_LastName.text;
regisrationdetails.email=_EmailAddress.text;
regisrationdetails.address1=_Address1.text;
regisrationdetails.address2=_Address2.text;
regisrationdetails.city=_City.text;
regisrationdetails.state=_State.text;
regisrationdetails.zip=_Zip.text;

NSURL *url = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@first_name=%@&last_name=%@&email=%@&address_1=%@&address_2=%@&city=%@&state=%@&zip=%@&&fb_id=%@&action=fbsignin",MainURL, regisrationdetails.firstname,regisrationdetails.lastname,regisrationdetails.email,regisrationdetails.address1,regisrationdetails.address2,regisrationdetails.city,regisrationdetails.state,regisrationdetails.zip,regisrationdetails.fbid ]];
    NSError *errors;
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errors];
    status = json[@"status"];
    error = json[@"error"];
    user = json[@"user"];
  if ([status isEqualToString:@"success"])
{
    UIAlertView *success=[[UIAlertView alloc]initWithTitle:@"Success" message:nil delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
    [success show];
}
else if ([status isEqualToString:@"failure"])
{
    messages=[[NSString alloc]init];
    for (int i=0; i<[error count]; i++)
    {
        messages=[messages stringByAppendingString:[error objectAtIndex:i]];
        messages=[messages stringByAppendingString:@"\n"];
    }

    UIAlertView *failure=[[UIAlertView alloc]initWithTitle:@"Failure" message:messages delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
    [failure show];
}
 }

Is there a simple sample code available to fetch the facebook user information using storyboard??

Was it helpful?

Solution

Make sure you have implement facebook delegate methods,

fbLoginview.delegate = self;

And implement this method to get user information

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {
    // here we use helper properties of FBGraphUser to dot-through to first_name and
    // id properties of the json response from the server; alternatively we could use


    NSLog(@"user :%@",user);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top