我已经使用这种方法在应用程序中注销 facebook
- (void)fbDidLogout {
// Remove saved authorization information if it exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]) {
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
NSLog(@"logout success!");
}
此方法已被调用,但是当我重新启动应用程序时,facebook 仍然知道我的最新授权。
我的完整实施
我为 facebook 实例创建单例对象。
这是我的经理.h
#import <Foundation/Foundation.h>
#import "FBConnect.h"
@interface FacebookManager : NSObject <FBSessionDelegate> {
Facebook *facebook;
}
@property (nonatomic, strong) Facebook *facebook;
+ (FacebookManager *)sharedInstance;
- (void)initWithAppID:(NSString *)appID;
@end
这是单身 facebookmanager .m
#import "FacebookManager.h"
@implementation FacebookManager
@synthesize facebook;
static FacebookManager *_sharedInstance = nil;
+ (FacebookManager *)sharedInstance {
@synchronized(self) {
if (!_sharedInstance) {
_sharedInstance = [[FacebookManager alloc] init];
}
}
return _sharedInstance;
}
- (void)initWithAppID:(NSString *)appID {
facebook = [[Facebook alloc] initWithAppId:appID andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
}
- (void)fbDidLogout {
// Remove saved authorization information if it exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]) {
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
NSLog(@"logout success!");
}
@end
在 appDelegate 我接下来做:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
fbManager = [FacebookManager sharedInstance];
[fbManager initWithAppID:@"myappid"];
... (some other code)
}
还将此代码添加到应用委托:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [fbManager.facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[fbManager.facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[fbManager.facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
在其他视图控制器中,我将此方法称为注销:
- (IBAction)logoutFacebook:(id)sender {
FacebookManager *fbManager = [FacebookManager sharedInstance];
[fbManager.facebook logout];
}
同样在 plist 我添加了所需的 url 方案。