简体   繁体   English

在iOS中使用SLRequest的Facebook会话

[英]Facebook session using SLRequest in iOS

I have to send app request using facebook sdk as it was not possible through graph call. 我必须使用facebook sdk发送应用程序请求,因为通过图形调用无法实现。 I have already got my facebook token as well as SLRequest object with access permission granted(yes). 我已经获得了我的facebook令牌以及具有授予访问权限的SLRequest对象(是)。 I just want to create fb session using above two parameters so that user does not have to enter his credentials on the pop up and the credentials will be automatically taken from SLrequest or Accounts object or from access token. 我只想使用上述两个参数来创建fb会话,这样用户就不必在弹出窗口中输入其凭据,并且凭据将自动从SLrequest或Accounts对象或访问令牌中获取。

Finally got the solution, call the requestGrantedForFb just after you get your request granted from 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;

                                          }


                                      }];
    });
}
}

After that 之后

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

                 }
             }
         }
     }];

});

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM