2010-07-13 21 views
0

好的,所以我需要我的应用程序能够在用户的墙上张贴某些内容,但我对显示登录弹出窗口和扩展权限请求弹出窗口的解决方案并不满意。如何在一次通话中登录并申请扩展权限?

只有一个弹出窗口可以吗?或甚至有2个弹出窗口,但由单个API调用触发?

回答

0

可能有帮助 -

- (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]; 

} 
+0

你的例子是使用FBStreamDialog发布的东西,我使用FBRequest在后台发布。我认为,使用FBStreamDialog进行发布,您不需要授予发布权限,因为用户将从对话框批准发布内容。不管怎么说,还是要谢谢你! – 2010-07-13 13:36:35

0

只要用户登录一次授予的权限,他应该不需要再次这样做。这里是我使用的代码:

//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]; 
     } 

} 

我不知道如何真正做到POST请求,但如果你运行该代码的用户应该只需要登录一次分配权限。

+0

如果用户将从他的Facebook帐户中删除权限,会发生什么情况? – 2010-09-07 08:27:52

+2

上述解决方案已过时。新SDK在一次调用中要求授权和许可。它可以在这里找到:http://github.com/facebook/facebook-ios-sdk/ – 2010-09-25 03:56:02