Created
July 6, 2012 04:52
-
-
Save ppworks/3058189 to your computer and use it in GitHub Desktop.
facebook-ios-sdk sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface AccessTokenManager : NSObject | |
@property (nonatomic, copy) NSString *facebookAccessTokenKey; | |
+ (AccessTokenManager *)sharedManager; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "AccessTokenManager.h" | |
#define FB_SERVICE_NAME @"Facebook" | |
#define FB_ACCOUNT_MAIN @"main" | |
#define FB_IDENTIFIER @"fbAccessToken" | |
@implementation AccessTokenManager | |
@synthesize facebookAccessTokenKey = _facebookAccessTokenKey; | |
static AccessTokenManager * _sharedInstance = nil; | |
+ (AccessTokenManager *)sharedManager { | |
if (!_sharedInstance) { | |
_sharedInstance = [[AccessTokenManager alloc]init]; | |
} | |
return _sharedInstance; | |
} | |
- (id)init { | |
self = [super init]; | |
return self; | |
} | |
- (void)setFacebookAccessTokenKey:(NSString *) facebookAccessTokenKey { | |
if (facebookAccessTokenKey == nil) { | |
NSMutableDictionary* query = [NSMutableDictionary dictionary]; | |
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass]; | |
[query setObject:FB_SERVICE_NAME forKey:(id)kSecAttrService]; | |
[query setObject:FB_ACCOUNT_MAIN forKey:(id)kSecAttrAccount]; | |
[query setObject:[FB_IDENTIFIER dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecAttrGeneric]; | |
OSStatus err = SecItemDelete((CFDictionaryRef)query); | |
if (err == noErr) { | |
} else { | |
NSLog(@"SecItemDelete: error(%ld)", err); | |
} | |
return; | |
} | |
// keychain check & save | |
NSMutableDictionary* attributes = nil; | |
NSMutableDictionary* query = [NSMutableDictionary dictionary]; | |
NSData* data = [facebookAccessTokenKey dataUsingEncoding:NSUTF8StringEncoding]; | |
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass]; | |
[query setObject:FB_SERVICE_NAME forKey:(id)kSecAttrService]; | |
[query setObject:FB_ACCOUNT_MAIN forKey:(id)kSecAttrAccount]; | |
[query setObject:[FB_IDENTIFIER dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecAttrGeneric]; | |
OSStatus err = SecItemCopyMatching((CFDictionaryRef)query, NULL); | |
if (err == noErr) { | |
// update item | |
attributes = [NSMutableDictionary dictionary]; | |
[attributes setObject:data forKey:(id)kSecValueData]; | |
[attributes setObject:[NSDate date] forKey:(id)kSecAttrModificationDate]; | |
err = SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)attributes); | |
if (err == noErr) { | |
} else { | |
NSLog(@"SecItemUpdate: error(%ld)", err); | |
return; | |
} | |
} else if (err == errSecItemNotFound) { | |
// add new item | |
attributes = [NSMutableDictionary dictionary]; | |
[attributes setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass]; | |
[attributes setObject:data forKey:(id)kSecValueData]; | |
[attributes setObject:FB_SERVICE_NAME forKey:(id)kSecAttrService]; | |
[attributes setObject:FB_ACCOUNT_MAIN forKey:(id)kSecAttrAccount]; | |
[attributes setObject:[FB_IDENTIFIER dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecAttrGeneric]; | |
[attributes setObject:[NSDate date] forKey:(id)kSecAttrCreationDate]; | |
[attributes setObject:[NSDate date] forKey:(id)kSecAttrModificationDate]; | |
err = SecItemAdd((CFDictionaryRef)attributes, NULL); | |
if (err == noErr) { | |
} else { | |
NSLog(@"SecItemAdd: error(%ld)", err); | |
return; | |
} | |
} else { | |
NSLog(@"SecItemCopyMatching: error(%ld)", err); | |
return; | |
} | |
} | |
- (NSString *)facebookAccessTokenKey { | |
NSMutableDictionary* query = [NSMutableDictionary dictionary]; | |
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass]; | |
[query setObject:FB_SERVICE_NAME forKey:(id)kSecAttrService]; | |
[query setObject:[FB_IDENTIFIER dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecAttrGeneric]; | |
[query setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData]; | |
NSData* data = nil; | |
OSStatus err = SecItemCopyMatching((CFDictionaryRef)query, | |
(CFTypeRef*)&data); | |
if (err == noErr) { | |
return [[[NSString alloc] initWithData:data | |
encoding:NSUTF8StringEncoding] autorelease]; | |
} else if(err == errSecItemNotFound) { | |
NSLog(@"SecItemCopyMatching: errSecItemNotFound"); | |
} else { | |
NSLog(@"SecItemCopyMatching: error(%ld)", err); | |
} | |
return @""; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "FBUser.h" | |
@interface CurrentUser : NSObject | |
+(void) setFBUser:(FBUser *)fbUser; | |
+ (FBUser *)fbUser; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "CurrentUser.h" | |
@implementation CurrentUser | |
static FBUser *_fbUserInstance = nil; | |
+(void) setFBUser:(FBUser *)fbUser { | |
[fbUser retain]; | |
[_fbUserInstance release]; | |
_fbUserInstance = fbUser; | |
} | |
+(FBUser *) fbUser { | |
return _fbUserInstance; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "FBConnect.h" | |
@interface FBController : NSObject<FBSessionDelegate, FBRequestDelegate> | |
@property (nonatomic, retain) Facebook *facebook; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Security/Security.h> | |
#import "FBController.h" | |
#import "CurrentUser.h" | |
#import "AccessTokenManager.h" | |
@implementation FBController | |
@synthesize facebook = _facebook; | |
- (id)init { | |
self = [super init]; | |
if (self) { | |
self.facebook = [[[Facebook alloc] initWithAppId:@"123456789" | |
andDelegate:self]autorelease]; | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
NSString *fbAccessToken = AccessTokenManager.sharedManager.facebookAccessTokenKey; | |
if (fbAccessToken && [defaults objectForKey:@"FBExpirationDateKey"]) { | |
self.facebook.accessToken = fbAccessToken; | |
self.facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; | |
} | |
if ([self.facebook isSessionValid]) { | |
[self fetchUserInfo]; | |
} | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[_facebook release], _facebook = nil; | |
[super dealloc]; | |
} | |
- (void)fetchUserInfo { | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
if ([defaults objectForKey:@"FBUserName"] | |
&& [defaults objectForKey:@"FBUserImage"]) { | |
FBUser *fbUser = [[[FBUser alloc]initWithUserName:[defaults objectForKey:@"FBUserName"] | |
image:[defaults objectForKey:@"FBUserImage"]]autorelease]; | |
[CurrentUser setFBUser:fbUser]; | |
} else { | |
[UIApplication.sharedApplication setNetworkActivityIndicatorVisible:YES]; | |
NSMutableDictionary* params = | |
[NSMutableDictionary dictionaryWithObjectsAndKeys: | |
@"ja", @"locale", | |
nil]; | |
[self.facebook requestWithGraphPath:@"me" andParams:params andDelegate:self]; | |
} | |
} | |
- (void) notifyLoginStatus:(NSString *)status { | |
NSNotification *note = [NSNotification notificationWithName:@"LoginStatus" | |
object:self | |
userInfo:[NSDictionary dictionaryWithObject:status forKey:@"status"]]; | |
[NSNotificationCenter.defaultCenter postNotification:note]; | |
} | |
#pragma mark - FBSessionDelegate - | |
- (void)fbDidLogin { | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
[defaults setObject:self.facebook.expirationDate forKey:@"FBExpirationDateKey"]; | |
[defaults synchronize]; | |
AccessTokenManager.sharedManager.facebookAccessTokenKey = self.facebook.accessToken; | |
NSMutableDictionary* params = | |
[NSMutableDictionary dictionaryWithObjectsAndKeys: | |
@"ja", @"locale", | |
nil]; | |
[self.facebook requestWithGraphPath:@"me" andParams:params andDelegate:self]; | |
} | |
- (void)fbDidNotLogin:(BOOL)cancelled { | |
[self notifyLoginStatus:@"logout"]; | |
} | |
- (void)fbDidLogout { | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; | |
[defaults removePersistentDomainForName:appDomain]; | |
AccessTokenManager.sharedManager.facebookAccessTokenKey = nil; | |
[CurrentUser setFBUser:nil]; | |
[self notifyLoginStatus:@"logout"]; | |
} | |
- (void)fbDidExtendToken:(NSString*)accessToken | |
expiresAt:(NSDate*)expiresAt { | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
[defaults setObject:expiresAt forKey:@"FBExpirationDateKey"]; | |
[defaults synchronize]; | |
AccessTokenManager.sharedManager.facebookAccessTokenKey = accessToken; | |
self.facebook.accessToken = AccessTokenManager.sharedManager.facebookAccessTokenKey; | |
self.facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; | |
} | |
- (void)fbSessionInvalidated { | |
} | |
#pragma mark - FBRequestDelegate - | |
- (void)requestLoading:(FBRequest *)request { | |
} | |
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response { | |
} | |
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error { | |
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; | |
} | |
- (void)request:(FBRequest *)request didLoad:(id)result { | |
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; | |
NSString *user_name = [result objectForKey:@"name"]; | |
NSString *user_image = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=square", [result objectForKey:@"id"]]; | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
[defaults setObject:user_name forKey:@"FBUserName"]; | |
[defaults setObject:user_image forKey:@"FBUserImage"]; | |
[defaults synchronize]; | |
FBUser *fbUser = [[FBUser alloc]initWithUserName:user_name image:user_image]; | |
[CurrentUser setFBUser:fbUser]; | |
[fbUser release]; | |
[self notifyLoginStatus:@"login"]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface FBUser : NSObject | |
@property (nonatomic, retain) NSString *name; | |
@property (nonatomic, retain) NSString *image; | |
- (id)initWithUserName: (NSString *)name image: (NSString *)image; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "FBUser.h" | |
@implementation FBUser | |
@synthesize name = _name, image = _image; | |
-(id) initWithUserName:(NSString *)user_name image:(NSString *)user_image { | |
self = [super init]; | |
if (self) { | |
[self setName:user_name]; | |
[self setImage:user_image]; | |
} | |
return self; | |
} | |
-(id) init { | |
return [self initWithUserName:@"" image:nil]; | |
} | |
-(void) dealloc { | |
[_name release], _name = nil; | |
[_image release], _image = nil; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment