如何在iPhone上将数据附加到Facebook应用程序请求?

时间:2012-06-06 11:11:28

标签: iphone ios facebook apprequests facebook-app-requests

我正在努力将Facebook请求“深度链接”到我的iOS应用中。一个Facebook应用程序设置好像可以工作,因为我可以向朋友发送请求,请求徽章出现在朋友的Facebook上,点击请求启动我的应用程序(所有在iPhone上)。 但是,到目前为止,我无法通过请求传递任何数据,我想在我的应用程序从Facebook应用程序启动请求时使用该数据。

我使用以下电话:

-(void) fbRequestActionWithMessage: (NSString *) message andLink: (NSString *) link
{
    NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"data1", @"key1",
                      @"data2", @"key2",
                      nil];

    NSString *requestDataString = [requestData JSONRepresentation];

    NSMutableDictionary* params = [NSMutableDictionary
                                  dictionaryWithObjectsAndKeys:
                                  message,  @"message",
                                  @"Check this out", @"notification_text",
                                  link, @"link",
                                  requestDataString, @"data",                                   
                                  nil];

    [facebook dialog:@"apprequests" andParams:params andDelegate:self];
}

params字典中的“data”和“link”值似乎都没有任何效果。理想情况下,当我的应用程序从此请求启动时,我会返回“数据”或“链接”值。可以这样做吗? 我找不到任何有关params字典结构的Facebook文档 - 是否有支持键及其效果的列表?

1 个答案:

答案 0 :(得分:2)

iOS中不支持“link”参数和“notification_text”,但您应该能够传入数据并将其取回。

示例,传入数据:

FBSBJSON *jsonWriter = [FBSBJSON new];
NSDictionary *requestData = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"data1", @"key1",
                      @"data2", @"key2",
                      nil];

NSString *requestDataString = [jsonWriter stringWithObject:requestData];
NSMutableDictionary* params = 
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
    message, @"message",
    requestDataString, @"data",
    nil];

[facebook dialog:@"apprequests"
       andParams:params
     andDelegate:self];

示例,请回读:

....
@property (nonatomic, retain) NSURL *openedURL;

....
@synthesize openedURL = _openedURL;

....
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    self.openedURL = url;
    return [FBSession.activeSession handleOpenURL:url]; 
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBSession.activeSession handleDidBecomeActive];
    if (FBSession.activeSession.isOpen) {
        [self checkIncomingNotification];
    }
}

- (void) notificationGet:(NSString *)requestid {
    [FBRequestConnection startWithGraphPath:requestid
          completionHandler:^(FBRequestConnection *connection,
                              id result,
                              NSError *error) {
              if (!error) {
                  NSString *title;
                  NSString *message;
                  if ([result objectForKey:@"data"]) {
                      // Process data in request
                      FBSBJSON *jsonParser = [FBSBJSON new];
                      NSDictionary *requestData = 
                      [jsonParser 
                        objectWithString:[result objectForKey:@"data"]];
                      [NSString stringWithFormat:@"Badge: %@, Karma: %@",
                      NSString *data1 = [requestData objectForKey:@"key1"];
                      NSString *data2 = [requestData objectForKey:@"key2"];
                  }
              }
          }];
}

- (void) checkIncomingNotification {
  if (self.openedURL) {
    NSString *query = [self.openedURL fragment];
    if (!query) {
        query = [self.openedURL query];
    }        
    NSDictionary *params = [self parseURLParams:query];
    // Check target URL exists
    NSString *targetURLString = [params valueForKey:@"target_url"];
    if (targetURLString) {
        NSURL *targetURL = [NSURL URLWithString:targetURLString];
        NSDictionary *targetParams = [self parseURLParams:[targetURL query]];
        NSString *ref = [targetParams valueForKey:@"ref"];
        // Check for the ref parameter to check if this is one of
        // our incoming news feed link, otherwise it can be an
        // an attribution link
        if ([ref isEqualToString:@"notif"]) {
            // Get the request id
            NSString *requestIDParam = [targetParams 
                                        objectForKey:@"request_ids"];
            NSArray *requestIDs = [requestIDParam 
                                   componentsSeparatedByString:@","];

            // Get the request data from a Graph API call to the
            // request id endpoint
            [self notificationGet:[requestIDs objectAtIndex:0]];
        }
    }
    // Clean out to avoid duplicate calls
    self.openedURL = nil;
  }
}

您可以使用最新的SDK v3.1找到更多相关信息: https://developers.facebook.com/docs/howtos/send-requests-using-ios-sdk/