In this recipe, we will start to implement additional functionality to our ViewController
class, as well as importing some very important header files and extend our class.
Following on from our previous recipe, ensure that our SocialNetworkApp
project file is open.
To begin, follow these simple steps as outlined in the given order:
ViewController.h
interface file from the Project Navigator.// ViewController.h // SocialNetworkApp // Created by Steven F. Daniel on 11/12/12. // Copyright (c) 2012 GenieSoft Studios. All rights reserved. #import <UIKit/UIKit.h> #import "AppDelegate.h" @interface ViewController : UIViewController<UIActionSheetDelegate, FBRequestDelegate> { AppDelegate *mainDelegate; IBOutlet UIBarButtonItem *loginButton; IBOutlet UIBarButtonItem *postMessage; IBOutlet UIImageView *imgPhoto; IBOutlet UITextView *userInfoDetails; Facebook *facebook; } // Create the required class Setters and Getters @property (nonatomic, strong) UIBarButtonItem *loginButton; @property (nonatomic, strong) UIBarButtonItem *postMessage; @property (nonatomic, strong) UIImageView *imgPhoto; @property (nonatomic, strong) UITextView *userInfoDetails; @property(nonatomic, retain) Facebook *facebook; @property(nonatomic, retain) AppDelegate *mainDelegate; // Declare our Instance methods -(void)SendNotificationRequest; -(void)PostMessagetoWall; -(void)getGraphAPIData; -(void)dialogDidComplete:(FBDialog *)dialog; -(void)request:(FBRequest *)request didLoad:(id)result; -(void)request:(FBRequest *)request didFailWithError:(NSError *)error; @end
ViewController.m
implementation file from the Project Navigator and modify the implementation file as shown by the highlighted code sections:// ViewController.m // SocialNetworkApp // Created by Steven F. Daniel on 11/12/12. // Copyright (c) 2012 GenieSoft Studios. All rights // reserved. #import "ViewController.h" #import "FBConnect.h" @interface ViewController () @end @implementation ViewController @synthesize mainDelegate, loginButton, postMessage; @synthesize imgPhoto,userInfoDetails, facebook; - (void)viewDidLoad{ [super viewDidLoad]; // Set up our delegate object self.mainDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; self.facebook = mainDelegate.facebook; // Initialize our form fields userInfoDetails.text = @""; } #pragma mark Handle when the login button is pressed. - (IBAction)loginButton:(id)sender{ if ([loginButton.title isEqualToString:@"Sign In"]) { if (![self.facebook isSessionValid]) { NSLog(@"facebook session"); NSArray *permissions = [[NSArray alloc] initWithObjects:@"email",@"publish_actions", nil]; [self.facebook authorize:permissions]; [permissions release]; } else {NSLog(@"session still valid");} [loginButton setTitle:@"Sign Out"]; } else if([loginButton.title isEqualToString:@"Sign Out"]) { [loginButton setTitle:@"Sign In"]; [self.facebook logout:self.mainDelegate]; } }
In this recipe, we started by extending our class to include the UIActionSheetDelegate
and FBRequestDelegate
class protocols, as well as its methods. We then declared an instance variable called facebook
that will enable us to access the Facebook
class methods. Next, we added a property instance of the Facebook
class to create the class getters and setters and then proceed to synthesize our facebook
variable that we defined within the AppDelegate.h
interface file.
Next, we import the interface file header information for our FBConnect.h
interface file, so that we can access the request dialog class methods. Next, we declare an application delegate object mainDelegate
that points to the properties and class methods within our delegate
class. Next, we initialize our facebook object within our ViewController
to use the same object instance as the one which we instantiated within our delegate
class. Finally, we initialize our user info details using UITextView
control.
Next, we create our loginButton:
method, and check the status of the button. If the button reads Sign In, we use the isSessionValid
method of the facebook
object to determine if we still have a valid connection to the facebook
instance.
If it proves that our session has expired, we initialize and pass to our authorize
method of the facebook class permissions to request for accessing the user's e-mail, and allow the iOS to publish to the Open Graph API actions. Finally, we release the memory that has been allocated by our permissions
object and then update the title of the button to read Sign Out. If our button reads Sign Out as determined by the loginButton:
method, we update the title of the button to read Sign In and then call the logout
method of the Facebook
class, and then pass in our mainDelegate
object. When this method is called, it will call the fbDidLogout
method of the FBSessionDelegate
within our AppDelegate
class to handle any post logout actions and releasing of objects as well as for notifying the user that a successful logout has taken place.
When making a call to the logout
method, your application's permissions will not be revoked; it will simply clear the value of your application's access_token
. If a user who has previously logged out of your application decides to run it again, they will simply see a notification that they are logging back into your application, not a notification requesting for permissions. For more information on the Facebook protocol methods, refer to the following URL: http://developers.facebook.com/docs/reference/iossdk/#protocols