Connecting app to Facebook...

Discussion in 'iOS Programming' started by Abrexas, Jun 18, 2012.

  1. Abrexas, Jun 18, 2012
    Last edited: Jun 18, 2012

    Abrexas macrumors member

    Joined:
    Jul 29, 2011
    #1
    Hey guys,
    I've been following the steps on https://developers.facebook.com/docs/mobile/ios/build/#logout
    to try and learn how to connect my app to Facebook. I have tried several times now, both with and without using storyboards, and I always run into the same problem.
    Step 4: Adding Log Out to your App
    Whenever I try this stem, nothing happens. I'm implementing everything into my AppDelegate like it tells me to, but when I connect to Facebook and click 'Okay' there is no 'Log Out' button visible anywhere..
    Anybody have any solutions?
    I don't know what code to include in this post, since I have no original code. I just created a new project and tried following the steps.

    Thanks,
    Steven French

    If there is a tutorial on connecting an iOS5 app to Facebook then please let me know. I have looked everywhere! Everything I've read says that this is really easy to do... but I'm struggling haha.
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    It happens that i've been researching this today, I might release a blog post on it, but i'll give you a quick hint (i've been using the FBConnect from Facebook itself).
    You just send

    Code:
    [self.facebook logout];
    
    to your facebook instance in your appDelegate.

    Basically, that will return you this

    Code:
    - (void)fbDidLogout {
        [SVProgressHUD showErrorWithStatus:@"To bad you logged off"];
    }
    from the "FBSessionDelegate"

    That SVProgressHUD is a free open source project I have of github, this is just to show the user that it logged off succesfully :)
    I think that has to take care of most things, you can always set the values you have in your NSUserDefaults to nil, since I haven't checked if the logout does this for you or not ^_- (probably not).
     
  3. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #3
    Thank you for the response, but i believe my problem lies a step before that. I don't have a log out button at all haha. I am making the button with

    Code:
    // Add the logout button
    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];
    in the AppDelegate.m in the didFinishLaunchingEithOptions method. My assumption is that the problem might be here

    Code:
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
    I think this because I opened a really old project of mine from a previous version of Xcode and added the button code there, and it showed up first try. In replace of the last code i posted, the old project that I had used this instead

    Code:
    // Set the view controller as the window's root view controller and display.
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
    I know this is probably a very novice problem, but I haven't coded in mad long and am probably just having the girthiest brain fart right now.

    Thanks for the help,
    SF
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    Basically, that's all you need, all you need to do is call the code
    Code:
     [self.facebook logout]; 
    In that "logoutButtonClicked" method of you :) , but you're adding the logout button to the viewController, and want your appdelegate's facebook to logout. Then you need to make a pointer in that logoutButtonClicked to your appdelegate.
    Code:
    YourAppDelegate *appDelegate = (YourAppDelegate*)[[UIApplication sharedApplication] delegate];
    [appDelegate.facebook logout];
    
    I didn't check syntax 100% typed this out of my head, so please double check that :) (that should go into your logOutButtonClicked)
     
  5. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #5
    I don't know if you looked at the steps that Facebook provides, but they have the button code and the logoutButtonClicked method all in the delegate. There isn't any code in the viewController. What it feels like to me is that the

    Code:
    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];
    is writing the button to a different viewController than the one that i see. Could this be the case?
    I understand that if I code the logout button into the view controller i would need to add a pointer to my delegates, but the Facebook site doesn't seem to have it set up that way. I will give it a try and see if it works though.

    Thanks,
    SF
     
  6. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    I haven't watched at Facebooks code, I just made it myself.
    Basically, the code you posted is putting the button on "self.viewController.view", which means your appdelegate has a controller called self.viewController (standard generated in AppDelegate), and it will add the button on the view of the Viewcontroller pointer.
    It's a bit like you want.. You can add th ebutton on what view you want, since your AppDelegate is a shared Singleton, so you can acces the Facebook pointer whenever and wherever you want. Hope that makes any sense.
     
  7. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #7
    Yeah that kinda makes sense. Is there a way i can change the self.vieController.view to the view controller that I see when I start up the app?I just feel like there is an inaccessible button being programmed right now. I know it's not a significant memory loss, but if I don't need it there then I would rather remove it haha.

    Again, thanks for the help!
    SF
     
  8. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #8
    Then don't add the code there, and add the code in the viewController you want it to have, like settings or whatever.
    Then make a reference to the AppDelegate like I showed you above, and then add it to self.view addSubview, instead of the self.viewController blah.
    Hope that made sense.
     
  9. Abrexas, Jun 25, 2012
    Last edited: Jun 25, 2012

    Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #9
    Sorry for the late response, I haven't had too much time to work on this as of lately.

    I tried what you said and coded the button into my vieController. As well as the -(void) logoutButtonClicked and linked it to a reference to my AppDelegate. However when I click the logout button, i get this error

    2012-06-25 15:42:01.217 myBookFaceBook[419:f803] -[ViewController logoutButtonClicked]: unrecognized selector sent to instance 0x687a680
    2012-06-25 15:42:01.297 myBookFaceBook[419:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController logoutButtonClicked]: unrecognized selector sent to instance 0x687a680'
    *** First throw call stack:
    (0x13d6052 0x1567d0a 0x13d7ced 0x133cf00 0x133cce2 0x13d7ec9 0x305c2 0x3055a 0xd5b76 0xd603f 0xd52fe 0x55a30 0x55c56 0x3c384 0x2faa9 0x12c0fa9 0x13aa1c5 0x130f022 0x130d90a 0x130cdb4 0x130cccb 0x12bf879 0x12bf93e 0x2da9b 0x26a8 0x2605 0x1)
    terminate called throwing an exception


    Could this be because I am calling logout in my Delegates, yet there isn't a logout method there? The Facebook instructions never told me to add anything to AppDelegate that I haven't added already.

    Thanks for all the help,
    SF


    Edit: I think I have this figured out, but maybe you can still tell me where the problem was coming from. I decided to change "logoutButtonClicked" to a different name.. and it works...
    The Facebook tutorial had me copy it's logoutButtonClicked code verbatim, could there be some inheritance or polymorphism problem going on here??

    I just made a new method and implemented the logout.... off to the next step now, but it would be swell to know why i can't use logoutButtonClicked haha.

    Thank you for all the help!!
     

Share This Page