presentRequestsDialogModallyWithSession 不起作用,但给出了很好的结果 [英] presentRequestsDialogModallyWithSession does not work, but gives good result

查看:26
本文介绍了presentRequestsDialogModallyWithSession 不起作用,但给出了很好的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用网络对话框进行好友请求时,一切都很好,除了没有请求或任何东西.代码:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:facebookFriend.id, @"to",零];[FBWebDialogs presentRequestsDialogModallyWithSession:FBSession.activeSessionmessage:NSLocalizedString(@"FB_FRIEND_INVITE_MESSAGE", @"Facebook好友邀请消息")title:NSLocalizedString(@"FB_FRIEND_INVITE_TITLE", @"Facebook好友邀请标题")参数:参数处理程序:^(FBWebDialogResult 结果,NSURL *resultURL,NSError *error){}];

这是我得到的结果:

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

如何调试出错的地方?

提前致谢.

路德

解决方案

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

让我们一步一步来:

1) 下载和设置 SDK 3.2 或更高版本.

2) 首先按照 这个 网址.

3) 然后使用附加的代码.

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

-(void)inviteFriends{if ([[FBSession activeSession] isOpen]){NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:nil];[FBWebDialogs presentRequestsDialogModallyWithSession:nil消息:[self getInviteFriendMessage]标题:无参数:参数处理程序:^(FBWebDialogResult 结果,NSURL *resultURL,NSError *error){如果(错误){[self requestFailedWithError:error];}别的{如果(结果 == FBWebDialogResultDialogNotCompleted){[self requestFailedWithError:nil];}else if([[resultURL description] hasPrefix:@"fbconnect://success?request="]){//Facebook 返回 FBWebDialogResultDialogCompleted 甚至用户//按下取消"按钮,所以我们根据//url 值,因为当我们实际时它返回请求"//完成对话框[自我请求成功];}别的{//用户取消了对话框[self requestFailedWithError:nil];}}}];}别的{/** 使用发布权限打开一个新会话*/[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_stream"]defaultAudience:FBSessionDefaultAudienceFriends允许登录用户界面:是completionHandler:^(FBSession *session, FBSessionState 状态, NSError *error){if (!error && status == FBSessionStateOpen){NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:nil];[FBWebDialogs presentRequestsDialogModallyWithSession:nil消息:[self getInviteFriendMessage]标题:无参数:参数处理程序:^(FBWebDialogResult 结果,NSURL *resultURL,NSError *error){如果(错误){[self requestFailedWithError:error];}别的{如果(结果 == FBWebDialogResultDialogNotCompleted){[self requestFailedWithError:nil];}else if([[resultURL description] hasPrefix:@"fbconnect://success?request="]){//Facebook 返回 FBWebDialogResultDialogCompleted 甚至用户//按下取消"按钮,所以我们根据//url 值,因为当我们实际时它返回请求"//完成对话框[自我请求成功];}别的{//用户取消了对话框[self requestFailedWithError:nil];}}}];}别的{[self requestFailedWithError:error];}}];}}

这里是调用委托函数OnFBSuccessOnFBFailed的辅助函数.

- (void)requestSucceeded{NSLog(@"requestSucceeded");id 所有者 = [fbDelegate 类];SEL 选择器 = NSSelectorFromString(@"OnFBSuccess");NSMethodSignature *sig = [owner instanceMethodSignatureForSelector:selector];_callback = [NSInvocation invocationWithMethodSignature:sig];[_callback setTarget:owner];[_callback setSelector:selector];[_callback 保留];[_callback invokeWithTarget:fbDelegate];}- (void)requestFailedWithError:(NSError *)error{NSLog(@"requestFailed");id 所有者 = [fbDelegate 类];SEL 选择器 = NSSelectorFromString(@"OnFBFailed:");NSMethodSignature *sig = [owner instanceMethodSignatureForSelector:selector];_callback = [NSInvocation invocationWithMethodSignature:sig];[_callback setTarget:owner];[_callback setSelector:selector];[_callback setArgument:&error atIndex:2];[_callback 保留];[_callback invokeWithTarget:fbDelegate];}

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

-(void)OnFBSuccess{CCLOG(@"成功");//在这里做事【登录发布】;}-(void)OnFBFailed:(NSError *)error{如果(错误 == 零)CCLOG(@"用户已取消");别的CCLOG(@"失败");//在这里做事【登录发布】;}

推荐阅读:

通过 Facebook 发送邀请

API 权限

示例

注意:

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

2) 不要忘记将 AppDelegate 调整为 处理网址.

摘自上述第 2 点链接的部分片段:

/** 如果我们在 openURL 调用时有一个有效的会话,我们会处理* Facebook 通过将 url 参数传递给 handleOpenURL 进行转换*/- (BOOL)application:(UIApplication *)applicationopenURL:(NSURL *)url源应用程序:(NSString *)源应用程序注释:(id)注释{//尝试从 url 中提取令牌返回 [FBSession.activeSession handleOpenURL:url];}

希望有帮助!

编辑

这里:

fbDelegate 的声明是:

@property (nonatomic, assign) id fbDelegate;@protocol FBLoginDelegate @必需的-(无效)OnFBSuccess;-(void) OnFBFailed : (NSError *)error;@结尾

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

FBLoginHandler *login = [[FBLoginHandler alloc] initWithDelegate:self];//这里的 'self' 是你问过的 fbDelegate【登录邀请好友】;

When I use the webdialog for a friendrequest, everything is going fine, except no request or anything is made. The code:

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) {
                                                        }];

This is the result I get:

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

How can I debug what is going wrong?

Thanks in advance.

Ruud

解决方案

For SDK 3.2 or above we have a facility to use FBWebDialogs class that will help us to show a popup along with the friend list and pick one or more from list to send invitation.

Lets do it step by step:

1) Download and setup SDK 3.2 or above.

2) First setup your application on facebook by following this url.

3) Then use the attached code.

Sample Code: (It generates invite friend request)

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

}

and here are the helper functions that calls delegates function OnFBSuccess and 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];
}

So the class taht calls method InviteFriend MUST have these functions:

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

Recommended Reads:

Send Invitation via Facebook

API Permissions

An Example

NOTE:

1) Don't forget to setup Facebook application ID in plist.

2) Don't forget to adjust AppDelegate to handle urls.

Partial snippet taken from above link in point 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];
}

Hope it helps!

EDIT

Here:

declaration for fbDelegate is:

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

@protocol FBLoginDelegate <NSObject>

@required

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

@end

and this is how you can consume this code:

FBLoginHandler *login = [[FBLoginHandler alloc] initWithDelegate:self];   // here 'self' is the fbDelegate you have asked about
[login inviteFriends];

这篇关于presentRequestsDialogModallyWithSession 不起作用,但给出了很好的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆