Question

I have watched tutorials and what I have learnt, that I have implemented on a project with two viewControllers just a simple firstName, lastName practice but I don't know where to place the method setFirstName: in a facebook project that I'm doing I wish to login (that works) then it automatically goes to another viewController (that works) I just cant get the user.name to be displayed, in which facebook delegate do i call the method, I fetch the info here and place it in the property in the loginViewController

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{  
    firstName = user.name;
    [[self delegate] setFirstName:firstName];     
}

then in the profileViewController I place this:

- (void)setFirstName:(NSString *)firstName
{
    firstNameString = firstName;
}

then i assign the properties here

-(void)viewWillAppear:(BOOL)animated
{
    self.firstNameLabel.text = firstNameString;
}

i think my issue is when I'm pushing to the profileViewController I'm not passing the info in the transition cause i don't use a segue I'm just pushing the viewController please help been on this issue for a week or so thanks

EDIT::

LoginViewController.m

// this method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
    self.profilePictureView.profileID = user.id;
    FBNamePass = user.name;
}

- (void)pushViewController
{
    NeXtViewController *controller = [[NeXtViewController alloc] init];
    controller.FBNameString = FBNamePass;
    [self.navigationController pushViewController:controller animated:YES];
}

EDIT 2::

LoginViewController.h

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>


@protocol passNames <NSObject>

- (void)setFBName: (NSString *)FBName;

@end

@interface ViewController : UIViewController <FBLoginViewDelegate>


@property (retain) id <passNames> delegate;
@property (strong, nonatomic) NSString *FBNamePass;

@end

LoginViewController.m

#import "ViewController.h"
#import "NeXtViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UILabel *statusLabel;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet FBProfilePictureView *profilePictureView;


@end

@implementation ViewController

@synthesize delegate, FBNamePass;

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



// Create a FBLoginView to log the user in with basic, email and likes permissions
// you should always ask for basic permissions when loggin the user in
FBLoginView *loginView = [[FBLoginView alloc]        initWithReadPermissions:@[@"basic_info",@"email",@"user_likes"]];

// set this loginUIViewCOntroller to be the loginView button's delegate
loginView.delegate = self;


// align the button in the center horizontally
loginView.frame = CGRectMake(25, 299, 271, 50);

// align the button in the center vertically
//loginView.center = self.view.center;

// add the button to the view
[self.view addSubview:loginView];

[self pushViewController];


}

-(void)viewDidAppear:(BOOL)animated
{

}

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

// this method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
    self.profilePictureView.profileID = user.id;
    FBNamePass = user.name;
}

// implement the loginViewShowingLoggedInUser: delegate method to modify your app's UI for a logged-in user experoience
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
    self.statusLabel.text = @"Logged in";

    if ([self.statusLabel.text isEqualToString:@"Logged in"]) {
        NeXtViewController *n = [self.storyboard   instantiateViewControllerWithIdentifier:@"NeXt"];
        [self.navigationController pushViewController:n animated:NO];

    }
}

// implement the loginViewShowingLoggedOutUser: delegate method to modify your app's UI for a logged-out user experoience
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
    self.profilePictureView.profileID = nil;
    self.nameLabel.text = @"";
    self.statusLabel.text = @"You're not logged in";
}

// You need to override loginView:handleError in order to handle possible errors that can occur during login
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error
{
    NSString *alertMessage, *alertTitle;

    // If the user should perform an action outside of you app to recover,
    // the SDK will provide a message for the user, you just need to surface it.
    // This conveniently handles cases like Facebook password change or unverified Facebook accounts.
    if ([FBErrorUtility shouldNotifyUserForError:error]) {
        alertMessage = [FBErrorUtility userMessageForError:error];
        alertTitle = @"Facebook Error";

        // This code will handle session closures since that happen outside of the app.
        // You can take a look at our error handling guide to know more about it
        // https://developers.facebook.com/docs/ios/errors
    } else if ([FBErrorUtility errorCategoryForError:error] ==          FBErrorCategoryAuthenticationReopenSession) {
        alertTitle = @"Session Error";
        alertMessage = @"Your current session is no longer valid. Please log in again.";

        // If the user has cancelled a login, we will do nothing.
        // You can also choose to show the user a message if cancelling login will result in
        // the user not being able to complete a task they had initiated in your app
        // (like accessing FB-stored information or posting to Facebook)
    } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
        NSLog(@"user cancelled login");

        // For simplicity, this sample handles other errors with a generic message
        // You can checkout our error handling guide for more detailed information
        // https://developers.facebook.com/docs/ios/errors
    } else {
        alertTitle = @"Something went wrong";
        alertMessage = @"Please try again later";
        NSLog(@"Unexpected error:%@",error);
    }

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




}

- (void)pushViewController
{
NeXtViewController *controller = [[NeXtViewController alloc] init];
controller.FBNameString = FBNamePass;
[self.navigationController pushViewController:controller animated:YES];
}

ProfileViewController.h

#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import "ViewController.h"

@interface NeXtViewController : UIViewController <passNames>
{
    ViewController *view;
}


@property (strong,nonatomic) NSString *FBNameString;

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


@end

ProfileViewController.m

#import "NeXtViewController.h"

@interface NeXtViewController ()

@end

@implementation NeXtViewController

@synthesize FBNameString;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    } 
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.

    view = [[ViewController alloc] init];
    [view setDelegate:self];
    self.nameLabel.text = FBNameString;


    self.navigationItem.hidesBackButton = YES;
}

-(void)viewDidAppear:(BOOL)animated
{

}

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


- (void)setFBName:(NSString *)FBName
{
    FBNameString = FBName;
}

@end
Was it helpful?

Solution

You need create property in ProfileViewController -> create instance of ProfileViewController -> set user name to instance property -> push instance of ProfileViewController:

LoginViewController.m

- (void)pushProfileViewController {
   ProfileViewController *controller = [[ProfileViewController alloc] init];
   controller.firstName = firstNameString;
   [self.navigationController pushViewController:controller animated:YES];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top