FBSessionDelegate 方法未触发
我正在尝试实施最新的 Facebook Connect SDK,但遇到了一些麻烦。由于某种原因,FBSessionDelegate
协议的委托回调未被触发。我已按照 git Facebook 页面上的说明进行操作,并尝试模仿 Facebook 示例应用程序,但没有成功。我在这里要疯了,所以我要发布我的代码,也许有人会看到我错过的一些愚蠢的东西。
#import <Foundation/Foundation.h>
#import "FBConnect.h"
@interface FacebookWrapper : UIViewController <FBSessionDelegate, FBRequestDelegate, FBDialogDelegate>{
Facebook* _facebook;
NSArray* _permissions;
}
@property(readonly) Facebook *facebook;
- (void)login;
@end
#import "FacebookWrapper.h"
static NSString* kAppId = @"1234455667778";
@implementation FacebookWrapper
@synthesize facebook = _facebook;
- (id)init {
if (self = [super init]) {
_permissions = [[NSArray arrayWithObjects: @"read_stream", @"offline_access",nil] retain];
_facebook = [[Facebook alloc] initWithAppId:kAppId];
}
return self;
}
- (void)dealloc {
[_facebook release];
[_permissions release];
[super dealloc];
}
- (void)login {
[_facebook authorize:_permissions delegate:self];
}
- (void)fbDidLogin {
NSLog(@"Did Log In");
}
- (void)fbDidNotLogin:(BOOL)cancelled {
NSLog(@"Failed to log in");
}
- (void)fbDidLogout {
NSLog(@"Logged Out");
}
从另一个班级调用这个,
FacebookWrapper *fw = [[FacebookWrapper alloc] init];
[fw login];
我在电话上看到的行为符合预期。 Facebook 应用程序在 init 时启动并请求权限。然后,手机将我的应用程序带回前台,但 FBSessionDelegate 的委托永远不会被触发。我已经使用我的应用程序 ID 在 Facebook 示例应用程序中尝试过此操作,效果很好。我不知道有什么区别。
I'm attempting to implement the latest Facebook Connect SDK and I'm having some troubles. For some reason the delegate callbacks for FBSessionDelegate
protocol are not being fired. I've followed the instructions on the git Facebook page and tried to mimic the Facebook sample app but no luck. I'm going crazy here so I'm gonna post my code and maybe somebody will see something silly that I've missed.
#import <Foundation/Foundation.h>
#import "FBConnect.h"
@interface FacebookWrapper : UIViewController <FBSessionDelegate, FBRequestDelegate, FBDialogDelegate>{
Facebook* _facebook;
NSArray* _permissions;
}
@property(readonly) Facebook *facebook;
- (void)login;
@end
#import "FacebookWrapper.h"
static NSString* kAppId = @"1234455667778";
@implementation FacebookWrapper
@synthesize facebook = _facebook;
- (id)init {
if (self = [super init]) {
_permissions = [[NSArray arrayWithObjects: @"read_stream", @"offline_access",nil] retain];
_facebook = [[Facebook alloc] initWithAppId:kAppId];
}
return self;
}
- (void)dealloc {
[_facebook release];
[_permissions release];
[super dealloc];
}
- (void)login {
[_facebook authorize:_permissions delegate:self];
}
- (void)fbDidLogin {
NSLog(@"Did Log In");
}
- (void)fbDidNotLogin:(BOOL)cancelled {
NSLog(@"Failed to log in");
}
- (void)fbDidLogout {
NSLog(@"Logged Out");
}
And to call this from another class,
FacebookWrapper *fw = [[FacebookWrapper alloc] init];
[fw login];
The behavior that I'm seeing on the phone is as expected. The Facebook app launches on init and permissions are requested. The phone then brings my app back to the foreground but the delegates for FBSessionDelegate
are never fired. I've tried this in the Facebook sample app using my app ID and it worked fine. I have no idea what the difference is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了问题所在。在App Delegate中,您需要覆盖handleOpenURL。
如果您像我一样创建一个包装类,您将需要在应用程序委托中拥有它的一个实例,以便您可以调用 Facebook 类中的 hanleOpenURL 方法。另请注意,我的 FacebookWrapper 类中的 Facebook 实例有一个公共只读属性,因此我可以调用 handlOpenURL。
通过执行此操作,您的应用程序将知道当它从 Facebook 应用程序内部获取权限返回时要继续到哪里。
I figured out the problem. In the App Delegate you need to override handleOpenURL.
If you're creating a wrapper class as I am you'll need an instance of it in the app delegate so you can call the hanleOpenURL method in the Facebook class. Also notice that there is a public readonly property for my Facebook instance in my FacebookWrapper class so I can call handlOpenURL.
By doing this your app will know where to continue when it returns from getting permissions inside of the Facebook app.
我开发了包装类,这对您非常有帮助。
首先下载Facebook最新的SDK。
创建一个名为“FacebookHelper”的类
在 .h 文件中添加以下代码:
在 .m 文件中添加以下代码:
就是这样。现在我们的 Facebook 包装类已准备就绪。
创建 3 个以下类以更好地组织。
FacebookGlobal.h
添加以下数据:
FacebookKey.h
添加以下数据:
FacebookMessage.h
添加以下数据:
将以下文件导入到您的 .pch 文件中:
现在,您就可以使用了。
在您的视图控制器中,添加以下委托:
在您的视图控制器 .m 文件中,添加以下方法来调用以及委托:
快乐编码:)
享受吧。
I have developed wrapper class which will be very helpful to you.
First download latest SDK of Facebook.
Create one class named as "FacebookHelper"
Add following code in .h file:
Add following code in .m file:
That's it. Now our Facebook wrapper class is ready.
Create 3 below class for better organization.
FacebookGlobal.h
Add below data:
FacebookKey.h
Add below data:
FacebookMessage.h
Add below data:
Import below files in your .pch files:
Now, you are ready for use.
In your view controller, add below delegate:
In your view controller .m file, add below methods to call as well delegate:
Happy Coding :)
Enjoy.