presentRequestsDialogModallyWithSession不起作用,但效果很好

时间:2013-03-19 09:17:37

标签: api facebook-ios-sdk facebook-app-requests

当我使用webdialog进行朋友请求时,一切都很顺利,除了没有请求或任何事情。 代码:

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

如何调试出错的地方?

提前致谢。

路德

2 个答案:

答案 0 :(得分:16)

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

让我们一步一步地做:

1)下载并设置SDK 3.2或以上。

2)首先按照this 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];
             }
         }];
    }

}

以下是调用委托函数OnFBSuccessOnFBFailed的辅助函数。

- (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];
}

所以类taht调用方法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];
}

推荐阅读:

Send Invitation via Facebook

API Permissions

An Example

注意:

1)不要忘记在plist中设置Facebook应用程序ID。

2)不要忘记将AppDelegate调整为handle 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];

答案 1 :(得分:0)

我认为您的应用程序无法启用Android和Web。并且您正尝试在网络或Android设备上收到通知。

积分:要在Android或网络上获取通知,您必须启用适用于Android和网络的应用。

在您的应用上启用Android和Web:转到您的应用&gt;设置&gt;点击 +添加平台添加输入必要信息并保存。

让我们享受通知。 : - )