presentRequestsDialogModallyWithSession不起作用,但给出了很好的结果

当我使用webdialog进行friendrequest时,一切都很顺利,除非没有任何请求或任何东西。 代码:

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 

我怎样才能debugging出了什么问题?

提前致谢。

范尼

对于SDK 3.2或更高版本,我们有一个使用FBWebDialogs类的工具,它将帮助我们显示一个popup窗口以及朋友列表,并从列表中select一个或多个来发送邀请。

让我们一步一步来做:

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

这里是调用委托函数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]; } 

所以类的调用方法InviteFriend必须具有这些function:

 -(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设置Facebook应用程序ID。

2)不要忘记调整AppDelegate来处理url 。

点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]; 

我认为你的应用程序不适用于Android和Web。 而且您正试图通过networking或Android设备获取通知。

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

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

让享受通知。 🙂