iphone - iOS :Sending Facebook Request

标签 iphone ios facebook

我已经发送了 facebook 请求。它工作正常,但我只在 iPhone facebook 应用程序中收到请求(通知),而不是在 Facebook 网络应用程序中。我希望 Facebook native 应用程序和 Web 应用程序都收到通知。我该怎么做?

#pragma Sending Facebook app request

- (NSDictionary*)parseURLParams:(NSString *)query {
    NSArray *pairs = [query componentsSeparatedByString:@"&"];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    for (NSString *pair in pairs) {
        NSArray *kv = [pair componentsSeparatedByString:@"="];
        NSString *val =
        [[kv objectAtIndex:1]
         stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [params setObject:val forKey:[kv objectAtIndex:0]];
    }
    return params;
}
- (void)sendRequest {
    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 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   giftStr, @"data",
                                   nil];

    // Display the requests dialog
    [FBWebDialogs
     presentRequestsDialogModallyWithSession:nil
     message:@"Learn how to make your iOS apps social."
     title:nil
     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.");
             } 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.");
                 } else {
                     // User clicked the Send button
                     NSString *requestID = [urlParams valueForKey:@"request"];
                     NSLog(@"Request ID: %@", requestID);
                 }
             }
         }
     }];
}

- (void)sendRequestClicked {
    // Filter and only show friends using iOS
    [self requestFriendsUsingDevice:@"iOS"];
}


- (void)sendRequest:(NSArray *) targeted {
    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 =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:giftStr, @"data",
     nil];

    // Filter and only show targeted friends
    if (targeted != nil && [targeted count] > 0) {
        NSString *selectIDsStr = [targeted componentsJoinedByString:@","];
        [params setObject:selectIDsStr forKey:@"suggestions"];
    }

    // Display the requests dialog
    [FBWebDialogs
     presentRequestsDialogModallyWithSession:nil
     message:@"Learn how to make your iOS apps social."
     title:nil
     parameters:params
     handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
         if (error) {
             // Error launching the dialog or sending request.
             NSLog(@"Error sending request.");
         } else {
             if (result == FBWebDialogResultDialogNotCompleted) {
                 // User clicked the "x" icon
                 NSLog(@"User canceled request.");
             } 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.");
                 } else {
                     // User clicked the Send button
                     NSString *requestID = [urlParams valueForKey:@"request"];
                     NSLog(@"Request ID: %@", requestID);
                 }
             }
         }
     }];
}


- (void) requestFriendsUsingDevice:(NSString *)device {
    NSMutableArray *deviceFilteredFriends = [[NSMutableArray alloc] init];
    [FBRequestConnection startWithGraphPath:@"me/friends"
                                 parameters:[NSDictionary
                                             dictionaryWithObjectsAndKeys:
                                             @"id,devices", @"fields",
                                             nil]
                                 HTTPMethod:nil
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error) {
                              if (!error) {
                                  // Get the result
                                  NSArray *resultData = [result objectForKey:@"data"];
                                  // Check we have data
                                  if ([resultData count] > 0) {
                                      // Loop through the friends returned
                                      for (NSDictionary *friendObject in resultData) {
                                          // Check if devices info available
                                          if ([friendObject objectForKey:@"devices"]) {
                                              NSArray *deviceData = [friendObject
                                                                     objectForKey:@"devices"];
                                              // Loop through list of devices
                                              for (NSDictionary *deviceObject in deviceData) {
                                                  // Check if there is a device match
                                                  if ([device isEqualToString:
                                                       [deviceObject objectForKey:@"os"]]) {
                                                      // If there is a match, add it to the list
                                                      [deviceFilteredFriends addObject:
                                                       [friendObject objectForKey:@"id"]];
                                                      break;
                                                  }
                                              }
                                          }
                                      }
                                  }
                              }
                              // Send request
                              [self sendRequest:deviceFilteredFriends];
                          }];
}

最佳答案

如果您的应用程序实现了 Facebook Canvas 应用程序,您只能在 Facebook 网络应用程序上收到通知。

The invitable_friends API is only available for games that have a Facebook Canvas app implementation using version 2.0 of the Graph API.

Check here the full documentation

Canvas is a frame in which to put your app or game directly on Facebook.com on desktops and laptops.

Details about Canvas

注意:在文档中您会找到“您的游戏”,它们的意思是“您的游戏或您的应用”。

关于iphone - iOS :Sending Facebook Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17335575/

相关文章:

android - 无效的 key 散列。 key 哈希与任何存储的 key 哈希都不匹配

java - 使用 facebook 登录 Android 应用程序时出现错误

iphone - 在 iOS 上,如何使 cell.imageView 刷新其内容?

iPhone:如何检测是否可以修改 EKEvent 实例?

iOS:带有四个圆形按钮的动画

ios - 从 Swift 函数中的异步调用返回数据

iphone - UITableView 分组显示每个部分的新信息

ios - Swift 3 发布后,Swift 2 应用程序可以正常运行吗?

ios - 应用程序在 swift 3 中的 for 循环内崩溃

ios - 如何使用 SOCIAL KIT 框架从我的 iOS 应用程序将视频上​​传到 Facebook?