9

当我将网络对话框用于好友请求时,一切都很好,除了没有请求或发出任何请求。编码:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   facebookFriend.id, @"to",
                                   nil];
    [FBWebDialogs presentRequestsDialogModallyWithSession:FBSession.activeSession 
                                                  message:NSLocalizedString(@"FB_FRIEND_INVITE_MESSAGE", @"Facebook friend invite message")       
                                                    title:NSLocalizedString(@"FB_FRIEND_INVITE_TITLE", @"Facebook friend invite title") 
                                               parameters:params 
                                                  handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                                                        }];

这是我得到的结果:

fbconnect://success?request=xxxxxxxxxxxx&to%5B0%5D=xxxxxxxx

如何调试出了什么问题?

提前致谢。

路德

4

2 回答 2

16

对于SDK 3.2 或更高版本,我们可以使用FBWebDialogs类来帮助我们显示一个弹出窗口以及好友列表,并从列表中选择一个或多个来发送邀请。

让我们一步一步来:

1) 下载并安装 SDK 3.2或更高版本。

2) 首先按照这个url 在 facebook 上设置你的应用程序。

3)然后使用随附的代码。

示例代码:(生成邀请好友请求)

-(void)inviteFriends
{
    if ([[FBSession activeSession] isOpen])
    {
        NSMutableDictionary* params =  [NSMutableDictionary dictionaryWithObjectsAndKeys:nil];
       [FBWebDialogs presentRequestsDialogModallyWithSession:nil
                                                      message:[self getInviteFriendMessage]
                                                        title:nil
                                                   parameters:params
                                                      handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
         {
             if (error)
             {
                 [self requestFailedWithError:error];
             }
             else
             {
                 if (result == FBWebDialogResultDialogNotCompleted)
                 {
                     [self requestFailedWithError:nil];
                 }
                 else if([[resultURL description] hasPrefix:@"fbconnect://success?request="]) 
                 {
                    // Facebook returns FBWebDialogResultDialogCompleted even user 
                    // presses "Cancel" button, so we differentiate it on the basis of
                    // url value, since it returns "Request" when we ACTUALLY
                    // completes Dialog
                     [self requestSucceeded];
                 }
                 else
                 {
                     // User Cancelled the dialog
                     [self requestFailedWithError:nil];
                 }
             }
         }
       ];

    }
    else
    {
        /*
         * open a new session with publish permission
         */
        [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_stream"]
                                           defaultAudience:FBSessionDefaultAudienceFriends
                                              allowLoginUI:YES
                                         completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
         {
             if (!error && status == FBSessionStateOpen)
             {
                 NSMutableDictionary* params =   [NSMutableDictionary dictionaryWithObjectsAndKeys:nil];
                 [FBWebDialogs presentRequestsDialogModallyWithSession:nil
                                                               message:[self getInviteFriendMessage]
                                                                 title:nil
                                                            parameters:params
                                                               handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
                  {
                      if (error)
                      {
                           [self requestFailedWithError:error];
                      }
                      else
                      {
                          if (result == FBWebDialogResultDialogNotCompleted)
                          {
                              [self requestFailedWithError:nil];
                          }
                          else if([[resultURL description] hasPrefix:@"fbconnect://success?request="])
                          {
                              // Facebook returns FBWebDialogResultDialogCompleted even user 
                              // presses "Cancel" button, so we differentiate it on the basis of
                              // url value, since it returns "Request" when we ACTUALLY
                              // completes Dialog
                              [self requestSucceeded];
                          }
                          else
                          {
                              // User Cancelled the dialog
                              [self requestFailedWithError:nil];
                          }

                      }
                  }];
             }
             else
             {
                 [self requestFailedWithError:error];
             }
         }];
    }

}

这里是调用委托函数OnFBSuccess和的辅助函数OnFBFailed

- (void)requestSucceeded 
{
    NSLog(@"requestSucceeded");
    id owner = [fbDelegate class];
    SEL selector = NSSelectorFromString(@"OnFBSuccess");
    NSMethodSignature *sig = [owner instanceMethodSignatureForSelector:selector];
    _callback = [NSInvocation invocationWithMethodSignature:sig];
    [_callback setTarget:owner];
    [_callback setSelector:selector];
    [_callback retain];

    [_callback invokeWithTarget:fbDelegate];
}

- (void)requestFailedWithError:(NSError *)error
{
    NSLog(@"requestFailed");
    id owner = [fbDelegate class];
    SEL selector = NSSelectorFromString(@"OnFBFailed:");
    NSMethodSignature *sig = [owner instanceMethodSignatureForSelector:selector];
    _callback = [NSInvocation invocationWithMethodSignature:sig];
    [_callback setTarget:owner];
    [_callback setSelector:selector];
    [_callback setArgument:&error atIndex:2];
    [_callback retain];

    [_callback invokeWithTarget:fbDelegate];
}

所以调用方法的类InviteFriend必须具有以下功能:

-(void)OnFBSuccess
{
    CCLOG(@"successful");

    //  do stuff here  
    [login release];
}

-(void)OnFBFailed:(NSError *)error
{
    if(error == nil)
        CCLOG(@"user cancelled");
    else
        CCLOG(@"failed");

    //  do stuff here  
    [login release];
}

推荐阅读:

通过 Facebook 发送邀请

API 权限

一个例子

笔记:

1) 不要忘记在plist.

2)不要忘记调整AppDelegate处理 urls

部分摘录自第 2 点中的上述链接:

/*
 * If we have a valid session at the time of openURL call, we handle
 * Facebook transitions by passing the url argument to handleOpenURL
 */
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBSession.activeSession handleOpenURL:url];
}

希望能帮助到你!

编辑

这里:

fbDelegate 的声明是:

@property (nonatomic, assign) id <FBLoginDelegate> fbDelegate;

@protocol FBLoginDelegate <NSObject>

@required

-(void) OnFBSuccess;
-(void) OnFBFailed : (NSError *)error;

@end

这就是您可以使用此代码的方式:

FBLoginHandler *login = [[FBLoginHandler alloc] initWithDelegate:self];   // here 'self' is the fbDelegate you have asked about
[login inviteFriends];
于 2013-05-20T17:12:17.360 回答
0

我认为您的应用程序不适用于 Android 和 web 。而您正试图在网络或 Android 设备上获得通知。

要点:要在 Android 或 Web 上获得通知,您还必须为 Android 和 Web 启用您的应用程序。

要在您的应用程序上启用 Android 和 Web:转到您的应用程序 > 设置 > 单击+ 添加平台,添加必要的信息并保存。

让我们享受通知。:-)

于 2015-03-21T14:31:41.560 回答