Search code examples
ioslogoutfacebook-ios-sdk

Facebook tutorial add logout button but blank screen


I'm very new to objective C. I followed the tutorial for integrating Facebook to iOS app and did nothing else. After I added the code for logout button stuff, I ran it and it gave me an error at line : [self.viewController.view addSubview:logoutButton]; The error is "unrecognized selector sent to instance 0x6b6c550". I know this might be a stupid error but if anyone can point out where I'm wrong I would be very appreciated!

static NSString* kAppId = @"340105106048288";


// Method that gets called when the sign out button is clicked
- (void)logoutButtonClicked {
    [facebook logout];
}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
   (NSDictionary *)launchOptions
{

UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
logoutButton.frame = CGRectMake(40, 40, 200, 40);
[logoutButton setTitle:@"Log Out" forState:UIControlStateNormal];
[logoutButton addTarget:self action:@selector(logoutButtonClicked)
       forControlEvents:UIControlEventTouchUpInside];
[self.viewController.view addSubview:logoutButton];


facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults
    objectForKey:@"FBExpirationDateKey"]) {
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
// This part, the authorize method will bring you to the authorization page
if (![facebook isSessionValid])
    [facebook authorize:nil];


    return YES;
}



// Pre iOS 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [facebook handleOpenURL:url];
}

// For iOS 4.2+ support
- (BOOL)application:(UIApplication *)application 
        openURL:(NSURL *)url 
  sourceApplication:(NSString *)sourceApplication 
     annotation:(id)annotation {
    return [facebook handleOpenURL:url];
}

// Save the user credential, specifically the access token and the expiration date to the user defaults
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}


- (void) fbDidLogout {
// Remove saved authorization information if it exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]) {
    [defaults removeObjectForKey:@"FBAccessTokenKey"];
    [defaults removeObjectForKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}
}

Solution

  • If you're using IB on your logoutButton, check if you have linked it to your object (xib) in the interface builder. Let me know if the error still occurs.

    [EDIT]

     [self.viewController.view addSubview:logoutButton];
    

    won't work. You're using storyboard, right? The initial values generated usually doesn't include "viewController" in the properties. You can only see the "window". There's no instance of the viewController which might have been the cause of the error.

    Piece of advice, why don't you implement the whole thing in ViewController and use delegate only when it's necessary, instead.

    And by the way, please show the link of that Facebook tutorial, so I can explain to you how it should be implemented.