0

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

4

1 回答 1

1

终于得到了解决方案,在您从 account store 获得您的请求后立即调用 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];

                 }
             }
         }
     }];

});

}
于 2014-04-24T11:07:23.140 回答