iOS facebook集成 - 发送和接收请求

时间:2013-01-10 14:24:41

标签: ios facebook facebook-requests

我使用facebook api连接到facebook并通过api提供的原生对话发送请求。

我关注了developers.facebook.com上的文档中发布的示例 但我有以下问题发送请求:  1.请求不会显示在通知中 - 仅在应用程序中心 - 在这种情况下,我认为这是一个问题,即应用程序位于沙箱中而未发布到APPSTORE

  1. 我使用正确的fbUser id成功向facebook服务器发送请求。但是,当我想在应用程序中收到通知时,问题就出现了:
  2. 作为授权用户遵循文档,我应该这样做 这在open url方法中:

    fb[APP_ID]://authorize#expires_in=[ACCESS_TOKEN_EXPIRATION]
        &access_token=[USER_ACCESS_TOKEN]
        &target_url=https://apps.facebook.com/[APP_NAME_SPACE]/?request_ids=
        [COMMA_SEPARATED_REQUESTIDs]&ref=notif&app_request_type=user_to_user
    

    但我只能看到没有targer url的普通登录....我可以看到会话到期日期,fb app id,访问令牌等等。但没有目标网址?

    那么target_url基本上是什么? 怎么设置? 发送请求时我必须包含哪些内容?

    另外: 应用程序句柄打开url方法被正确调用。 应用程序变为活动状态后,也会正确调用checkRequests方法。

    请不要将我链接到文档。我已经阅读了50次,没有找到任何合理的解决方案......

    - (BOOL)application:(UIApplication *)application
                    openURL:(NSURL *)url
          sourceApplication:(NSString *)sourceApplication
                 annotation:(id)annotation {
            // attempt to extract a token from the url
            self.openedURL = url;
            NSLog(@"%@",url);
            return [FBSession.activeSession handleOpenURL:url];
        }
    
    
        - (void)sendRequest {
            FBSBJSON *jsonWriter = [FBSBJSON new];
            NSDictionary *gift = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"5", @"points",
                                  @"1", @"badge",
                                  nil];
    
            NSString *giftStr = [jsonWriter stringWithObject:gift];
            NSMutableDictionary* params =
            [NSMutableDictionary dictionaryWithObjectsAndKeys:
             @"Hi from test app", @"message",
             giftStr, @"data",
             nil];
    
            [self.facebook dialog:@"apprequests"
                        andParams:params
                      andDelegate:self];
        }
    
        // Handle the request call back
        - (void)dialogCompleteWithUrl:(NSURL *)url {
            NSDictionary *params = [self parseURLParams:[url query]];
            NSString *requestID = [params valueForKey:@"request"];
            NSLog(@"Request ID: %@", requestID);
        }
        -(FBSession*)returnSession{
            return self.session;
        }
        /*
         * Helper function to get the request data
         */
        - (void) notificationGet:(NSString *)requestid {
            [FBRequestConnection startWithGraphPath:requestid
                                  completionHandler:^(FBRequestConnection *connection,
                                                      id result,
                                                      NSError *error) {
                                      if (!error) {
                                          NSString *title;
                                          NSString *message;
                                          if ([result objectForKey:@"data"]) {
                                              title = [NSString
                                                       stringWithFormat:@"%@ sent you a gift",
                                                       [[result objectForKey:@"from"]
                                                        objectForKey:@"name"]];
                                              FBSBJSON *jsonParser = [FBSBJSON new];
                                              NSDictionary *requestData =
                                              [jsonParser
                                               objectWithString:[result objectForKey:@"data"]];
                                              message =
                                              [NSString stringWithFormat:@"Badge: %@, Karma: %@",
                                               [requestData objectForKey:@"badge"],
                                               [requestData objectForKey:@"points"]];
                                          } else {
                                              title = [NSString
                                                       stringWithFormat:@"%@ sent you a request",
                                                       [[result objectForKey:@"from"] objectForKey:@"name"]];
                                              message = [NSString stringWithString:
                                                         [result objectForKey:@"message"]];
                                          }
                                          UIAlertView *alert = [[UIAlertView alloc]
                                                                initWithTitle:title
                                                                message:message
                                                                delegate:nil
                                                                cancelButtonTitle:@"OK"
                                                                otherButtonTitles:nil,
                                                                nil];
                                          [alert show];
                                          // Delete the request notification
                                          [self notificationClear:[result objectForKey:@"id"]];
                                      }
                                  }];
        }
    
        /*
         * Helper function to check incoming URL
         */
        - (void) checkIncomingNotification {
    
            if (self.openedURL) {
                NSString *query = [self.openedURL fragment];
                if (!query) {
                    query = [self.openedURL query];
                }
                NSDictionary *params = [self parseURLParams:query];
                for (NSString * str in [params allKeys]) {
    
                    NSLog(@"key %@", str);
                }
                // 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;
            }
        }
    

    这些问题是否有任何原因导致应用程序未在Appstore上发布(Appstore ID既未设置为iPhone也未设为iPad)?

    以下是显示使用fb api的代码段:

    非常感谢您的时间。

2 个答案:

答案 0 :(得分:1)

在Facebook应用设置中启用深层链接

答案 1 :(得分:0)

Facebook sdk 3.5 requests not working

我认为此链接可以帮助您,也可以在Facebook上配置应用