views:

89

answers:

2

OK, so I need my app to be able to post something on the users's wall but I'm not very happy with the solution of showing the login pop-up and then the extended permission request popup. Can it be done with only one popup? Or even with 2 pop-ups but triggered by a single API call?

Thanks!

A: 

It might help -

- (void)viewDidLoad {
    FacebookAPIAppDelegate *appDelegate =(FacebookAPIAppDelegate *)[[UIApplication sharedApplication]delegate];

    if (appDelegate._session == nil){
        appDelegate._session = [FBSession sessionForApplication:_APP_KEY 
                                                         secret:_SECRET_KEY delegate:self];
    }
    if(self.loginButton == NULL)
        self.loginButton = [[[FBLoginButton alloc] init] autorelease];
    loginButton.frame = CGRectMake(0, 0, 200, 50);

    [self.view addSubview:loginButton];

    [super viewDidLoad];
}



- (void)session:(FBSession*)session didLogin:(FBUID)uid {
    self.usersession =session;
    NSLog(@"User with id %lld logged in.", uid);
    [self getFacebookName];
}

- (void)getFacebookName {
    NSString* fql = [NSString stringWithFormat:
                     @"select uid,name from user where uid == %lld", self.usersession.uid];
    NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@"query"];
    [[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params];
    self.post=YES;
}

- (void)request:(FBRequest*)request didLoad:(id)result {
    if ([request.method isEqualToString:@"facebook.fql.query"]) {
        NSArray* users = result;
        NSDictionary* user = [users objectAtIndex:0];
        NSString* name = [user objectForKey:@"name"];
        self.username = name;       

        if (self.post) {
            [self postToWall];
            self.post = NO;
        }
    }
}

- (void)postToWall {

    FBStreamDialog *dialog = [[[FBStreamDialog alloc] init] autorelease];
    dialog.userMessagePrompt = @"Enter your message:";
    //dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"Check this Mix\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone\",\"caption\":\"Caption\",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}"];

    dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"Check this Mix\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone\",\"caption\":\"Caption\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}"];   

    [dialog show];

}
Saurabh
Your example is using FBStreamDialog to post something, I'm posting in the background using a FBRequest. I think that for posting with FBStreamDialog you don't need to have posting permissions granted as the user will be approving the posting from the dialog. Thanks anyway!
Nick Dima
A: 

as long as the user has logged in and granted permissions once, he should not need to do so again. here's the code I use:

//these to need to be declared in .h
NSString * currentRequest;
BOOl hasAskedForPermissions;
FBSession * mySession


//then in .m
@synthesize currentRequest
@synthesize hasAskedForPermissions

-(void)viewDidLoad {
static NSString* kApiKey = @"XXXXXXXXXXXXXXXXXXXXXXX";
static NSString* kApiSecret = @"XXXXXXXXXXXXXXXXXXXXXXXXXX";

mySession = [[FBSession alloc] initWithKey:kApiKey secret:kApiSecret getSessionProxy:nil];


}

-(void)resumeConnection {
    NSLog(@"resuming connection");
    static NSString* kApiKey = @"XXXXXXXXXXXXXXXXXXXXXXX";
    static NSString* kApiSecret = @"XXXXXXXXXXXXXXXXXXXXXXXXXX";

    if ([mySession resume]) {

    }
    else {
        NSLog(@"session did not resume successfully");
        mySession = [[FBSession sessionForApplication:kApiKey 
                                              secret:kApiSecret delegate:self] retain];
        _loginDialog = [[FBLoginDialog alloc] init];    
        [_loginDialog show];
    }
}

-(void)postStatus {
    currentRequest = @"post status";
    //NSLog(@"current request: %@", currentRequest);
    if (!(mySession.isConnected)) {

        [self resumeConnection];


    }
    else if (mySession.isConnected) {
        NSLog(@"session is connected");
                //Add your posting code here.

    } else {
        NSLog(@"session is not connected, did not connect");


    }

}

-(void)loadPermissionDialog {
    FBPermissionDialog* dialog = [[FBPermissionDialog alloc] initWithSession:_session];
    dialog.delegate = self;
    dialog.permission = @"read_stream, publish_stream, read_friendlists";
    [dialog show];

}

- (void)session:(FBSession *)session didLogin:(FBUID)uid {
    NSLog(@"Session Logged in sucessfully");
    //NSLog(@"current request: %@", currentRequest);
    mySession = session;
    if (hasAskedForPermissions) {
        if ([currentRequest isEqualToString:@"post status"]) {
            [self postStatus];
        }
    }   
    else {
        NSLog(@"asking for permissions");
        hasAskedForPermissions = YES; 
      //you should save this in UserDefaults and recall it in viewDidLoad

        [self loadPermissionDialog];
        }

}

I don't know how to actually do the post request, but if you run this code the user should only have to log in and assign permissions once.

Jason Jensen
@Jason, what will happen if the user will remove the permission from his Facebook account?
Michael Kessler
The solution above is outdated. The new SDK asks for authorization and permissions in a single call. It can be found here: http://github.com/facebook/facebook-ios-sdk/
Jason Jensen