簡體   English   中英

如何在iPhone上將數據附加到Facebook應用程序請求?

[英]How to attach data to Facebook app request on the iPhone?

我正在努力將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文檔 - 是否有支持鍵及其效果的列表?

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM