How to update UITextView after login - ios

I am trying to integrate Facebook to my iOS app with ARC. To use FB SDK, I have disabled ARC with "-fno-objc-arc" for those files. However, it still have EXC_BAD_ACCESS error and I need to change
#property(nonatomic, assign) id<FBSessionDelegate> sessionDelegate;
to
#property(nonatomic, retain) id<FBSessionDelegate> sessionDelegate;
The login is working now but the UITextView is not updated after login
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
[delegate facebook].accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
[delegate facebook].expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
if (![[delegate facebook] isSessionValid])
{
[[delegate facebook] authorize:nil];
}
}
- (void)fbDidLogin
{
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[[delegate facebook] accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[[delegate facebook] expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[[delegate facebook] requestWithGraphPath:#"me" andDelegate:self];
}
- (void)request:(FBRequest *)request didLoad:(id)result
{
if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0];
}
if ([result objectForKey:#"name"]) {
self.uitextview.text = [result objectForKey:#"name"];
}
}
I have checked that "[result objectForKey:#"name"]" will return a result but "self.uitextview" is not recognized.
How do I update a UITextView after successful login?

Found out that the ViewController init in Appdelegate can't be used as shown in Facebook Hackbook sample.
Replace with the following declaration:
UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;
ViewController *viewcontroller = [[tabBarController viewControllers] objectAtIndex:4];
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

Related

Register Login Logout App(NSUserDefaults) in iOS using Xcode

There should be a registration page with Full Name, User Name, Password, Email and Register Button.
When we Click on Register Button we then go to Login Page where it validate the username and password that we created in registration page using NSUserDefaults.
Now if we give right credentials then it should redirect to logout page and when we next time open the app it should directly redirect to logout page.
Data Should be stored locally using NSUserDefaults(value should be stored in string format).
Login view controller .h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
#property(strong, nonatomic)NSString *dataString;
#property(strong, nonatomic)NSString *dataString2;
#property (strong, nonatomic) IBOutlet UILabel *lblOutput;
#property (strong, nonatomic) IBOutlet UITextField *inputTxt1;
#property (strong, nonatomic) IBOutlet UITextField *inputTxt2;
- (IBAction)btnAction:(id)sender;
#end
Login view controller .m
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firstName = [defaults objectForKey:#"firstName"];
NSString *lastName = [defaults objectForKey:#"lastname"];
firstName = _inputTxt1.text;
lastName = _inputTxt2.text;
[super viewDidLoad];
NSLog(#"%#",self.dataString);
NSLog(#"%#",self.dataString2);
self.navigationItem.title = #"Login Page";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnAction:(id)sender {
NSString *firstName = [_inputTxt1 text];
NSString *lastName = [_inputTxt2 text];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:firstName forKey:#"firstName"];
[defaults setObject:lastName forKey:#"lastname"];
[defaults synchronize];
// [Defaults setObject:#"Eezy"forKey:#"iOS"];
NSLog(#"%#",[defaults stringForKey:#"firstName"]);
NSLog(#"%#",[defaults stringForKey:#"lastName"]);
if ([self.dataString isEqualToString:[defaults objectForKey:#"firstName"]] && [self.dataString2 isEqualToString:[defaults objectForKey:#"lastName"]])
{
NSLog(#"goog");
[self performSegueWithIdentifier:#"segueToNextPage2" sender:self];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hey Listen" message:#"Wrong User Name or Password" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
#end
The problem is that I have passed the value from registration page but when I am storing it in NSUserDefault then it is storing the value of username but in case of password it is showing null value.
FOR Saving login values you can use
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:txtUsername.text forKey:#"userName"];
[prefs setObject:txtPassword.text forKey:#"password"];
[prefs synchronize];
FOR RETRIEVING Values
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *savedUsername = [prefs stringForKey:#"userName"];
NSString *savedPassword = [prefs stringForKey:#"password"];
For logout:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"Your Key"];
Here is the Solution i got :-
Step 1: In login page define a bool variable and set its value as true on clicking the login button
- (IBAction)btnAction:(id)sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:#"registered"];
[prefs synchronize];
}
Step 2. Create a storyBoard in identity inspector as "UITabBarController". Keep in mind this will be the page where you want to go.
Step 3. In AppDelegate.h file just below #implemention write following code :-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"registered"]) {
UITabBarController *controller = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"UITabBarController"];
self.window.rootViewController = controller;
}
return YES;
}
That's it very simple.
Save the details in NSUserDefaults from registration page. Also make entry in NSUserDefaults to check whether user logged in or not.
[[NSUserDefaults standardUserDefaults]setValue:#"YES" forKey:#"isLoggedIn"];
In your AppDelegate's didFinishLaunching method, change the initialViewController based on isLoggedIn key value e.g.
if ([[[NSUserDefaults standardUserDefaults]valueForKey:#"isLoggedIn"] isEqualToString:#"YES"])
{
// redirect to logout screen
}
else
{
//show login or register screen
}

How to launch a different viewController on the second launch

Here the code I have under the viewController.m
This code will run when a user selects a viewController
-(void)switchViews {
UIStoryboard *mainStory = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStory instantiateViewControllerWithIdentifier:schoolName];
[self presentModalViewController:vc animated:YES];
NSUserDefaults *defaultViewController = [NSUserDefaults standardUserDefaults];
[defaultViewController setObject:nil forKey:#"save"];
[defaultViewController synchronize];
}
This code will run on the second time the app is launched
-(void)loadNewView {
UIStoryboard *mainStory = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NSUserDefaults *newUserDefault = [NSUserDefaults standardUserDefaults];
NSString *newVC = [NSString stringWithFormat:#"save", schoolName];
UIViewController *newViewController = [mainStory instantiateViewControllerWithIdentifier:newVC];
[self presentModalViewController:newViewController animated:YES];
}
Keeping in mind that schoolNameis a string
How can I run [self loadNewView] under the viewDidLoad but running it on the second time the app is launched?
You should do something like
id value = [[NSUserDefaults standardUserDefaults] objectForKey:#"save"];
if (value) {
UIViewController *vc = [mainStory instantiateViewControllerWithIdentifier:value];
[self presentModalViewController:vc animated:YES];
} else {
// first time logic
}
in your AppDelegate or wherever you have your navigation logic. You should not have that logic in viewDidLoad.
-(void)viewDidLoad {
[super viewDidLoad];
NSString *launchCount = #"LaunchCount";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger count;
if([userDefaults objectForKey:launchCount]) {
count = [userDefaults integerForKey:launchCount];
}
else {
count = 0;
}
count++; //increment the launch count
[userDefaults setObject:[NSNumber numberWithInt:count] forKey:launchCount];
[userDefaults synchronize];
if([userDefaults integerForKey:launchCount] >= 2) {
// Do your thang
}
}
on ViewDidLoad function do something like this
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedFirst"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLauncheFirst"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self switchViews];
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:No forKey:#"HasLauncheFirst"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self loadNewView];
}

Pull data from view controller on a function

Passing data just isn't doing the trick...I need to pull string data from a view controller when a function in the AppDelegate is called. Here's the code:
In App Delegate:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
AMViewController *viewController = [[AMViewController alloc] initWithNibName:#"AMViewController" bundle:nil];
self.xpData = viewController.xpLabel.text;
NSLog(#"Value of xpString in AD: %#", self.xpData);
}
In my ViewController, I'm not using an action to pass/retrieve the string. I'm basically pulling it from the ViewController when the user hits the home button. I'm doing this because I'd like to save my data on exit. Thanks!
In your AppDelegate.h
#property (atomic, copy) NSString *yourString;
Where you have changed xpLabel.text, do this
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.yourString = xpLabel.text;
Now in
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Value of xpString in AD: %#", self.yourString);
}
Another Method
Set your value in AMViewController class like this
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:xpLabel.text forKey:#"yourkey"];
[prefs synchronize];
Get your Value in applicationDidEnterBackground like this
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
yourString = [prefs stringForKey:#"yourkey"];
Did you tried working with NSNotificationCenter
Or shared delegate:
http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/
http://coderchrismills.wordpress.com/2011/05/05/basic-delegate-example/

Facebook ios logout method does not work

I have used this method for logout facebook in app
- (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];
}
NSLog(#"logout success!");
}
This method has been invoked, but when I relaunch app the facebook still know about my latest authorization.
My full implementation
I create singleton object for facebook instance.
this is my manager .h
#import <Foundation/Foundation.h>
#import "FBConnect.h"
#interface FacebookManager : NSObject <FBSessionDelegate> {
Facebook *facebook;
}
#property (nonatomic, strong) Facebook *facebook;
+ (FacebookManager *)sharedInstance;
- (void)initWithAppID:(NSString *)appID;
#end
this is singleton facebookmanager .m
#import "FacebookManager.h"
#implementation FacebookManager
#synthesize facebook;
static FacebookManager *_sharedInstance = nil;
+ (FacebookManager *)sharedInstance {
#synchronized(self) {
if (!_sharedInstance) {
_sharedInstance = [[FacebookManager alloc] init];
}
}
return _sharedInstance;
}
- (void)initWithAppID:(NSString *)appID {
facebook = [[Facebook alloc] initWithAppId:appID andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
}
- (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];
}
NSLog(#"logout success!");
}
#end
in appDelegate I make next:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
fbManager = [FacebookManager sharedInstance];
[fbManager initWithAppID:#"myappid"];
... (some other code)
}
also add this code to app delegate:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [fbManager.facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[fbManager.facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[fbManager.facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
In other view controller I call this method for logout:
- (IBAction)logoutFacebook:(id)sender {
FacebookManager *fbManager = [FacebookManager sharedInstance];
[fbManager.facebook logout];
}
also in plist I have add needed url scheme.
Modify the Facebook.m code to this, which works for me.
-(void)logout:(id)delegate {
self.sessionDelegate = delegate;
[_accessToken release];
_accessToken = nil;
[_expirationDate release];
_expirationDate = nil;
NSHTTPCookieStorage *cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *facebookCookies = [cookies cookiesForURL:[NSURL URLWithString:#"http://login.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies){
[cookies deleteCookie:cookie];
}
//Adds this one.
for (NSHTTPCookie *_cookie in cookies.cookies){
NSRange domainRange = [[_cookie domain] rangeOfString:#"facebook"];
if(domainRange.length > 0){
[cookies deleteCookie:_cookie];
}
}
if ([self.sessionDelegate respondsToSelector:#selector(fbDidLogout)]){
[_sessionDelegate fbDidLogout];
}
}
What do you mean by "when I relaunch app the facebook still know about my latest authorization" ? Does this mean your FB accesstoken is still valid? You still see the userdefaults values? What exactly?
If you mean that when you trigger the FB login again, it automatically logs you in again, then yes if you have the Facebook APP installed in iOS, the user will have to logout from the FB app manually to switch an account.
If the FB app is not installed, then yes the SSO should prompt the user to login again after initiating a logout.
That seems to be the way the ios sdk api with sso/oauth 2.0 works. I have not been able to logout completely even after clearing the tokens and I have not been able to switch users from the App. Got to go to the FB app to switch users
As a workaround, change the line in Facebook.m and disable the safariAuth
// [self authorizeWithFBAppAuth:YES safariAuth:YES];
[self authorizeWithFBAppAuth:NO safariAuth:NO]
But then you have to type in the username and password everytime you authorize.
In Facebook.m, add the following code to remove cookies at m.facebook.com domain.
- (void)invalidateSession {
...
NSArray* facebookMCookies = [cookies cookiesForURL:
[NSURL URLWithString:#"https://m.facebook.com"]];
for (NSHTTPCookie* cookie in facebookMCookies) {
[cookies deleteCookie:cookie];
}
...
}
I have tried with success this code:
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSLog(#"cache dir %#", cachesDirectory);
NSError *error = nil;
for (NSString *file in [fm contentsOfDirectoryAtPath:cachesDirectory error:&error]) {
BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:#"%#/%#", cachesDirectory, file] error:&error];
if (!success || error) {
NSLog(#"Error delete file: %#, %#", file, error);
} else {
NSLog(#"Deleted file: %#", file);
}
}

Using Facebook Graph API on iOS

This is my first post on StackOverflow but i use this for a long time...
My problem is basically simple, I want to share the content of a UITextView(mShareText) on the user's wall without prompting any Dialog. Basically, the user has to fill its message on the UITextView and then click on a "post" UIButton. Simple isn't it ?
I've downloaded the Facebook iOS SDK and copied it into my project. I've included "FBConnect.h" and "Facebook.h" on my controller's header file and created a var : Facebook* facebook;, the controller also implements the following delegates : FBSessionDelegate, FBRequestDelegate.
On my controller's implementation file, I've a IBAction triggered when the user clicks on the "post" UIButton.
- (IBAction)onShareByFBPressed:(id)sender {
NSLog(#"onShareByFBPressed");
// Facebook settings
NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithCapacity:3];
[params setObject:mShareText.text forKey:#"message"];
[params setObject:#"http://www.example.com" forKey:#"link"];
[params setObject:#"https://www.example.com/myImg.jpg" forKey:#"picture"];
[facebook requestWithGraphPath:#"me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
[params release];
}
I've implemented this on my viewdidload:
- (void)viewDidLoad
{
NSLog(#"viewDidLoad");
[super viewDidLoad];
(...)
// Test facebook
facebook = [[Facebook alloc] initWithAppId:#"XXXXXXXXXXX" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
NSLog(#"AccessToken: %# ExpirationDate: %#", facebook.accessToken, facebook.expirationDate);
}else{
NSLog(#"No AccessToken or ExpirationDate");
}
if (![facebook isSessionValid]) {
[facebook authorize:[NSArray arrayWithObjects:#"publish_stream", nil]];
}
}
Here are different methods I had to implement due to the delegates :
// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSLog(#"fbDidLogin");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
- (void)fbSessionInvalidated{
NSLog(#"fbSessionInvalidated");
}
- (void)fbDidLogout{
NSLog(#"fbDidLogout");
}
- (void)fbDidExtendToken:(NSString*)accessToken expiresAt:(NSDate*)expiresAt{
NSLog(#"fbDidExtendToken");
}
- (void)fbDidNotLogin:(BOOL)cancelled{
NSLog(#"fbDidNotLogin");
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response{
NSLog(#"didReceiveResponse: %#", response);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error{
NSLog(#"didFailWithError: %#", [error description]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Share on Facebook"
message:#"An error occured"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
Unfortunatelly, everytime I try to trigger my "onShareByFBPressed" method, I got the following error on the log:
2012-02-17 13:02:06.401 Mixtapes[935:707] didFailWithError: Error Domain=facebookErrDomain Code=10000 "The operation couldn’t be completed. (facebookErrDomain error 10000.)" UserInfo=0x1f6f60 {error=<CFBasicHash 0x1f6660 [0x3ec6d630]>{type = mutable dict, count = 3,
entries =>
2 : <CFString 0x1f64a0 [0x3ec6d630]>{contents = "type"} = <CFString 0x1f6b10 [0x3ec6d630]>{contents = "OAuthException"}
3 : <CFString 0x1f6ab0 [0x3ec6d630]>{contents = "message"} = <CFString 0x1f6a10 [0x3ec6d630]>{contents = "An active access token must be used to query information about the current user."}
6 : <CFString 0x1f6e60 [0x3ec6d630]>{contents = "code"} = 2500
}
}
Can anyone tell me what's wrong with my implementation ??
Thanks a lot.
Best regards.
EDIT:
#Adil
Thanks for your proposal. It actually works somethimes.
Sometimes i got the following error :
didFailWithError: Error Domain=facebookErrDomain Code=10000 "The operation couldn’t be completed. (facebookErrDomain error 10000.)" UserInfo=0xa17e0d0 {error=<CFBasicHash 0xa171e10 [0x3ec6d630]>{type = mutable dict, count = 3,
entries =>
2 : <CFString 0xa179d10 [0x3ec6d630]>{contents = "type"} = <CFString 0xa17a280 [0x3ec6d630]>{contents = "OAuthException"}
3 : <CFString 0x6127710 [0x3ec6d630]>{contents = "message"} = <CFString 0xa178db0 [0x3ec6d630]>{contents = "Error validating access token: Session is invalid. This could be because the application was uninstalled after the session was created."}
6 : <CFString 0xa17e020 [0x3ec6d630]>{contents = "code"} = 190
}
}
Thanks for your help
The problem here is:
The facebook which is authororized is AppDelegate's facebook object, not your ViewController's facebook.
Solution:
Instead allocating new Facebook object for every ViewController or where you needed.
you should make a property of your application delegate. like this:
in appDelegate.h
#property(nonatomic, retain) Facebook *facebook;
and synthesize it in appDelegate.m and now put initialization of Facebook in
- (BOOL)application:(UIApplication *)ap didFinishLaunchingWithOptions:(NSDictionary *)op
{
// other code....
facebook = [[Facebook alloc] initWithAppId:#"XXXXXXXXXXX" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
NSLog(#"AccessToken: %# ExpirationDate: %#", facebook.accessToken, facebook.expirationDate);
}else{
NSLog(#"No AccessToken or ExpirationDate");
}
return true;
}
now where do you want to use it, write something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
//...
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
facebook = app.facebook;
if (![facebook isSessionValid]) {
[facebook authorize:[NSArray arrayWithObjects:#"publish_stream", nil]];
}
}
Try to deauthorize your application and then authorize it again

Resources