简体   繁体   English

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

[英]iOS facebook integration - sending and receiving requests

I use a facebook api to connect to facebook and send request via native dialogs provided by the api. 我使用facebook api连接到facebook,并通过api提供的本机对话框发送请求。

I followed the sample posted in the docs on developers.facebook.com But I have following problem sending requests : 1. The requests are not shown in notifications - only in application center - In this case i think that it is a problem of that the app is in sandbox and not posted to APPSTORE 我遵循了developer.facebook.com上文档中发布的示例,但是发送请求时遇到以下问题:1.请求未在通知中显示-仅在应用程序中心中出现-在这种情况下,我认为这是一个问题应用程序在沙箱中,尚未发布到APPSTORE

  1. I succesfully send request to facebook server with right fbUser id. 我成功地将请求发送到具有正确的fbUser ID的Facebook服务器。 But when I want to receive the notification in app here comes the problem : 但是,当我想在应用程序中接收通知时,就会出现问题:

Following the docs as an authorized user I should se this in open url method: 在以授权用户身份访问文档之后,我应该在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

But i can see only plain login without targer url .... I can see session expiration date, fb app id, access token and so on. 但是我只能看到普通的登录,而没有目标url...。我可以看到会话到期日期,fb应用程序ID,访问令牌等。 But no target url? 但是没有目标网址吗?

So basically what the target_url is? 那么基本上target_url是什么? How it should be set? 应该如何设置? What i have to include when sending request? 发送请求时我必须包括什么?

In addition : application handle open url method is called properly. 另外:应用程序句柄打开url方法被正确调用。 checkRequests method is also called properly after app becomes active. 应用激活后,也将正确调用checkRequests方法。

Please do not link me to the docs. 请不要将我链接到文档。 I have read it moreless 50 times and didn't find any reasonable solution... 我读了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;
        }
    }

Is there any way that these problems are caused by the way that the app is not published on Appstore (Appstore id is not set neither for iPhone nor iPad)? 这些问题是否有可能是由于该应用程序未在Appstore上发布(iPhone和iPad均未设置Appstore ID)引起的?

Here are code snippets showing using of the fb api: 以下是显示使用fb api的代码段:

Thank you very much for the time. 非常感谢您抽出宝贵的时间。

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

Facebook sdk 3.5 requests not working Facebook SDK 3.5请求不起作用

I think this link will help you,configure App on Facebook as well 我认为此链接也将帮助您在Facebook上配置App

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM