Facebook IOS SDK 试图制作单例类?
嘿,我正在尝试从 Facebook IOS SDK(2011 年 10 月)创建一个我自己的类,在其中我可以创建我需要的所有方法和函数,只需在我的任何应用程序视图控制器中导入此类并调用它即可。 因此,这样我就不需要在我使用 facebook 的地方进行所有初始化和委派给控制器。 所以我想这样做,[myfacebookclass initwithappID:myappid] [myfacebookclass postmessage:@"一些消息"];
问题是,当我在 facebook.m (原始类)中设置authorizeWithFBAppAuth:YES safariAuth:YES 标志并且我执行一条 postmessage 时,它会在 Facebook 应用程序或 Safari 上重定向我并检查权限,我点击“确定”,然后在我的应用程序中重定向我。应用程序并且不发布消息。
当我尝试发布消息时,我会这样做:
这是在我自己的 Facebook 类中,
myfacebookclass *facebook2;
+(void)initWithAppID:(NSString*) facebookAppId {
if (facebook2 == nil) {
facebook2 = [myfacebookclass new];
[facebook2 setWithAppId:facebookAppId];
}
}
- (void) postMessage1:(NSString *)messageToPost {
if ([self.facebook isSessionValid]) {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
self.applicationID, @"api_key",
messageToPost, @"message",
nil];
[self.facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
} else {
NSArray * neededPermissions = [[[NSArray alloc] initWithObjects:@"user_about_me", @"publish_stream", nil] autorelease];
[self.facebook authorize:neededPermissions];
}
}
+ (void)postMessage:(NSString*) message {
[facebook2 postMessage1:message];
}
我还实现了:
- (BOOL)应用程序:(UIApplication *)应用程序 openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication 注释:(id)注释 { 返回 [facebook handleOpenURL:url]; }
我没有修改 facebook.m 或 Facebook IOS SDK 中的其他类
Hy, i am trying to make from Facebook IOS SDK (Octomber 2011) a class of my own in where i can make all the methods and functions that i need and just import this class in any of my application view controllers and call it.
So this way i don't need to do all the initialization and delegation to the controllers in where i use the facebook.
So i want to do like this, [myfacebookclass initwithappID:myappid]
[myfacebookclass postmessage:@"some message"];
The thing is when i am seting authorizeWithFBAppAuth:YES safariAuth:YES flags in facebook.m ( the original class) and i do a postmessage it redirects me on Facebook Application or Safari and check's for permision, i hit ok and then redirects me in my application and doesn't post message.
WHen i am trying to post message i do like this :
This is in my own facebook class
myfacebookclass *facebook2;
+(void)initWithAppID:(NSString*) facebookAppId {
if (facebook2 == nil) {
facebook2 = [myfacebookclass new];
[facebook2 setWithAppId:facebookAppId];
}
}
- (void) postMessage1:(NSString *)messageToPost {
if ([self.facebook isSessionValid]) {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
self.applicationID, @"api_key",
messageToPost, @"message",
nil];
[self.facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
} else {
NSArray * neededPermissions = [[[NSArray alloc] initWithObjects:@"user_about_me", @"publish_stream", nil] autorelease];
[self.facebook authorize:neededPermissions];
}
}
+ (void)postMessage:(NSString*) message {
[facebook2 postMessage1:message];
}
I also implemented:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [facebook handleOpenURL:url];
}
i've didn't modified facebook.m or other classes in Facebook IOS SDK.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建单例类时的一个好习惯是使用共享实例。正确的单例还必须包含一些内存管理重写方法。在您的情况下,可以这样做:
在您的情况下,您可能有 Facebook 类型对象作为 ivar,以及一些包装 FB API 的方法,例如像这样:
从必须处理 Facebook 的其他类中,构造很简单:
我希望这些片段能对您有所帮助。
要管理您的 Facebook 会话,您必须在 FBSessionDelegate 方法中手动记住 accessToken,如下所示:
a good practice when making a singleton class is to use shared instance. Correct singleton also has to include some memory management overridden methods. In your case it can be done like this:
In your case you may have Facebook type object as ivar, and some methods to wrap over FB API, for example like this:
From other classes which have to deal with Facebook the construction is simple:
I hope this snippets will help you.
To manage your facebook session you have to remember accessToken manually in FBSessionDelegate methods like that: