为什么,我试图从Facebook IOS SDK(2011年10月)我自己的一个类,在那里我可以使所有的方法和功能,我需要,只是导入这个类在任何我的应用程序视图控制器和调用它。这样我就不需要做所有的初始化和委派到我使用facebook的控制器。所以我想这样做,[myfacebookclass initwithappID:myappid][myfacebookclass postmessage:@"some message"];
事情是当我在facebook中设置authorizeWithFBAppAuth:YES safariAuth:YES标志时。m(原始类),我做一个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];
}
我还实现了:
我没有修改过facebook。
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)urlsourceApplication:(NSString *)sourceApplication annotation:(id)annotation{return [facebook handleOpenURL:url];}
创建单例类的一个好习惯是使用共享实例。正确的单例还必须包含一些覆盖内存管理的方法。在您的情况下,可以这样做:
@implementation FacebookManager
static FacebookManager *mySharedFacebookManager;
+ (FacebookManager*)sharedFacebookManager {
@synchronized(self) {
if(mySharedFacebookManager == nil) {
mySharedFacebookManager = [[self alloc] init];
}
}
return mySharedFacebookManager;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if(mySharedFacebookManager == nil) {
mySharedFacebookManager = [super allocWithZone:zone];
return mySharedFacebookManager;
}
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX;
}
- (oneway void)release {
}
- (id)autorelease {
return self;
}
在你的情况下,你可能有Facebook类型对象作为ivar,和一些方法来包装FB API,例如:
- (id)init {
self = [super init];
if(self) {
facebook = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:self];
// restore previous session
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
...
}
return self;
}
- (void)login {
NSArray *permissions = [NSArray arrayWithObjects: @"user_about_me", @"publish_stream", nil];
[facebook authorize:permissions];
}
- (void)postToFeed:(NSString*)messageToPost {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
messageToPost, @"message",
nil];
[facebook requestWithGraphPath:@"me/feed"
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
}
从必须处理Facebook的其他类的构造是简单的:
[[FacebookManager sharedFacebookManager] login];
...
[[FacebookManager sharedFacebookManager] postToFeed:@"Hello World!"];
我希望这些片段对你有帮助。
要管理您的facebook会话,您必须在FBSessionDelegate方法中手动记住accessToken:
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
- (void)fbDidLogout {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];
}