ios8- Facebook sdk-如何使用UIWebView打开应用程序请求URL

时间:2015-02-11 09:43:36

标签: ios facebook facebook-graph-api webview facebook-invite

我正在向Facebook好友发送应用请求,那么我如何才能在UIWebView中打开应用请求?

NSDictionary * parametersDict = @ {@" to":@""};

        [FBWebDialogs presentRequestsDialogModallyWithSession:FBSession.activeSession
                                                      message:@"YouPin"
                                                        title:@"my title"
                                                   parameters:parametersDict
                                                      handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
         {
             if(error)
             {
                 NSLog(@"Some errorr: %@", [error description]);
                 UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"Invitiation Sending Failed" message:@"Unable to send inviation at this Moment, please make sure your are connected with internet" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
                 [alrt show];
             }
             else
             {
                 if (![resultURL query])
                 {
                     [self.navigationController popViewControllerAnimated:YES];
                     return;
                 }

                 NSDictionary *paramsStr = [self parseURLParamsSecond:[resultURL query]];
                 NSMutableArray *recipientIDs = [[NSMutableArray alloc] init];
                 for (NSString *paramKey in paramsStr)
                 {
                     if ([paramKey hasPrefix:@"to["])
                     {
                         [recipientIDs addObject:[paramsStr objectForKey:paramKey]];
                     }
                 }
                 if ([paramsStr objectForKey:@"request"])
                 {
                     NSLog(@"Request ID: %@", [paramsStr objectForKey:@"request"]);
                 }
                 if ([recipientIDs count] > 0)
                 {
                     NSLog(@"Recipient ID(s): %@", recipientIDs);
                     UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Invitation(s) sent successfuly!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                     alrt.tag = 10;
                     [alrt show];
                 }
             }
         }friendCache:nil];

    - (NSDictionary *)parseURLParamsSecond:(NSString *)query
    {
        NSArray *pairs = [query componentsSeparatedByString:@"&"];
        NSMutableDictionary *paramsSECOND = [[NSMutableDictionary alloc] init];
        for (NSString *pair in pairs)
        {
            NSArray *kv = [pair componentsSeparatedByString:@"="];

            [paramsSECOND setObject:[[kv objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                             forKey:[[kv objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        }

        return paramsSECOND;
    }

我正在向Facebook好友发送应用请求,那么我如何才能在UIWebView中打开应用请求?

1 个答案:

答案 0 :(得分:1)

对于邀请用户,您可以在添加facebook-sdk后使用以下代码

[FBWebDialogs
 presentRequestsDialogModallyWithSession:nil
message:@"Your invite message"
title:nil
parameters:nil
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                                      if (error) {
                                          // An error occurred, we need to handle the error
                                          // See: https://developers.facebook.com/docs/ios/errors
                                          NSLog(@"Error publishing story: %@", error.description);
                                      } else {
                                          if (result == FBWebDialogResultDialogNotCompleted) {
                                              // User canceled.
                                              NSLog(@"User cancelled.");
                                          } else {
                                              // Handle the publish feed callback




                                              } 

                                    }


}];

/////////////////////////

在网页浏览中显示应用请求:发送朋友的facebookId和您的消息。

- (void)sendAppRequestToFacebookFriend:(NSString*)message andFacebookId:(NSString*)friendFBId {


NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                message, @"message",
                               @"", @"redirect_url",
                               friendFBId,@"to",
                               nil];


[FBWebDialogs
 presentRequestsDialogModallyWithSession:nil
 message:message
 title:@"App Title App"
 parameters:params
 handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {

     if (!error) {


         if (result == FBWebDialogResultDialogNotCompleted) {





         }
         else if([[resultURL description] hasPrefix:@"fbconnect://success?request="]) {
             //code after success
            } 

     } 

 }
  ];

}