在iOS中使用SLRequest的Facebook会话

时间:2014-02-11 06:57:26

标签: ios facebook-graph-api slrequest facebook-sdk-3.1 acaccountstore

我必须使用facebook sdk发送应用请求,因为无法通过图表调用。我已经获得了我的facebook令牌以及已授予访问权限的SLRequest对象(是)。我只想使用以上两个参数创建fb会话,这样用户就不必在弹出窗口中输入他的凭据,凭据将自动从SLrequest或Accounts对象或访问令牌中获取。

1 个答案:

答案 0 :(得分:1)

终于得到了解决方案,在您从帐户商店获得请求后立即调用requestGrantedForFb:

-(void)requestGrantedForFb
{
 if (gblAppDelegate.session.isOpen)
    {
        [self SendReqClk];
 } else
{
    if (gblAppDelegate.session.state != FBSessionStateCreated)
    {
        gblAppDelegate.session = [[FBSession alloc] init];
    }


    NSArray *permissionArray = [NSArray arrayWithObjects:@"read_friendlists",@"basic_info",nil];

    dispatch_async(dispatch_get_main_queue(), ^{
        [FBSession openActiveSessionWithReadPermissions:permissionArray
                                           allowLoginUI:YES
                                      completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {

                                          if (session.isOpen)
                                          {
                                              gblAppDelegate.session = session;
                                              [self SendReqClk];

                                          } else
                                          {
                                              gblAppDelegate.session = session;

                                          }


                                      }];
    });
}
}

之后

-(void)SendReqClk
{



NSError *error;
NSData *jsonData = [NSJSONSerialization
                    dataWithJSONObject:@{
                                         @"social_karma": @"5",
                                         @"badge_of_awesomeness": @"1"}
                    options:0
                    error:&error];
if (!jsonData) {
    NSLog(@"JSON error: %@", error);
    return;
}

NSString *giftStr = [[NSString alloc]
                     initWithData:jsonData
                     encoding:NSUTF8StringEncoding];

NSMutableDictionary* params = [@{@"data" : giftStr} mutableCopy];

// Display the requests dialog
dispatch_async(dispatch_get_main_queue(), ^{
    [FBWebDialogs
     presentRequestsDialogModallyWithSession:gblAppDelegate.session
     message:@"Welcome to !!! "
     title:@"Join now !!!"
     parameters:params
     handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
         if (error) {
             // Error launching the dialog or sending the request.
             NSLog(@"Error sending request.");
         } else {
             if (result == FBWebDialogResultDialogNotCompleted) {
                 // User clicked the "x" icon
                 NSLog(@"User canceled request.");
                 [self.navigationController popViewControllerAnimated:YES];

             } else {
                 // Handle the send request callback
                 NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
                 if (![urlParams valueForKey:@"request"]) {
                     // User clicked the Cancel button
                     NSLog(@"User canceled request.");
                     [self.navigationController popViewControllerAnimated:YES];

                 } else if(result== FBWebDialogResultDialogCompleted){
                     // User clicked the Send button
                     NSString *requestID = [urlParams valueForKey:@"request"];
                     UIAlertView *aAlertSuccess=[[UIAlertView alloc]initWithTitle:@"" message:@"Friend request sent successfully!!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                     [aAlertSuccess show];
                     NSLog(@"Request ID: %@", requestID);
                     //                     [NSThread detachNewThreadSelector:@selector(stopActivity) toTarget:self withObject:nil];
                     //                     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"my alert" message:@"message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];//
                     //                     [alert setTag:125];
                     //                     [alert show];

                 }
             }
         }
     }];

});

}