【问题标题】:How to attach data to Facebook app request on the iPhone?如何将数据附加到 iPhone 上的 Facebook 应用请求?
【发布时间】:2012-06-06 11:11:27
【问题描述】:

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

【问题讨论】:

  • 这个运气好吗?他们在这方面的文档......很少,即使对于网络应用程序版本也是如此。

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


【解决方案1】:

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/

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多