iOS Login facebook SDK
First go to Developers facebook in SDKs title iOS turn left. 'download the sdk' and let's start!
- Xcode Create Project
- Add SDK facebook to project 'FacebookSDK.framework'
- new file objective-c file name 'FacebookLogin' subclass of UIViewController and allow also create XIB file
in project we have file FacebookLogin.h , FacebookLogin.m , FacebookLogin.xib , AppDelegate.h , AppDelegate.m
- start FacebookLogin.h file
following code this >
#import <FacebookSDK/FacebookSDK.h>
- in FacebookLogin.m file
#import "FacebookLogin.h"
#import <FacebookSDK/FacebookSDK.h>
@interface FacebookLogin () <FBLoginViewDelegate>
@property (strong, nonatomic) IBOutlet FBProfilePictureView *profilePic;
@property (nonatomic, strong) IBOutlet UILabel *idLabel;
@property (nonatomic, strong) IBOutlet UILabel *firstnameLabel;
@property (nonatomic, strong) IBOutlet UILabel *birthDayLabel;
@property (nonatomic, strong) IBOutlet UILabel *emailLabel;
@property (nonatomic, strong) IBOutlet UILabel *genderLabel;
@property (nonatomic, strong) IBOutlet UILabel *nameLabel;
@property (nonatomic, strong) IBOutlet UILabel *usernameLabel;
@property (nonatomic, strong) IBOutlet UILabel *statusLabel;
@property (nonatomic, strong) id<FBGraphUser> loggedInUser;
@end
@implementation FacebookLogin
#pragma template generate / UIViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self = [super initWithNibName:@"FacebookLogin_iPad" bundle:nil];
}
else {
self = [super initWithNibName:@"FacebookLogin_iPhone" bundle:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
FBLoginView *loginView = [[FBLoginView alloc] initWithReadPermissions:@[@"user_birthday", @"email"]];
loginView.frame = CGRectOffset(loginView.frame, (self.view.center.x - (loginView.frame.size.width / 2)), (self.view.center.y / 2));
loginView.delegate = self;
[self.view addSubview:loginView];
[loginView sizeToFit];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - FBLoginViewDelegate
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {
NSLog(@"Login");
self.statusLabel.text = @"You're logged in as";
}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user {
NSLog(@"FetchUser");
self.idLabel.text = user.id;
self.birthDayLabel.text = user.birthday;
self.emailLabel.text = [user objectForKey:@"email"];
self.firstnameLabel.text = user.first_name;
self.genderLabel.text = [user objectForKey:@"gender"];
self.nameLabel.text = user.name;
self.usernameLabel.text = user.username;
self.profilePic.profileID = user.id;
self.loggedInUser = user;
}
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {
NSLog(@"LogOut");
self.statusLabel.text = @"You're not logged in!";
self.profilePic.profileID = nil;
self.idLabel.text = nil;
self.birthDayLabel.text = nil;
self.emailLabel.text = nil;
self.firstnameLabel.text = nil;
self.genderLabel.text = nil;
self.nameLabel.text = nil;
self.usernameLabel.text = nil;
self.loggedInUser = nil;
}
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
NSLog(@"FBLoginView error = %@", error);
}
@end
Tips: create View interface for IBOutlet FBProfilePictureView
======================================================================
Next Step go to AppDelegate.h and AppDelegate.m
- in AppDelegate.h
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController * navController;
@end
- in AppDelegate.m
#import "AppDelegate.h"
#import <FacebookSDK/FacebookSDK.h>
#import "FacebookLogin.h"
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[Login alloc] initWithNibName:@"FacebookLogin_iPhone" bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
} else {
self.viewController = [[Login alloc] initWithNibName:@"FacebookLogin_iPad" bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
}
#pragma - initial startup interface
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
#pragma mark - SDK Facebook
[FBProfilePictureView class]; // Setting FBProfilePictureView
return YES;
}
#pragma mark - FacebookSDK delegate
- (BOOL)application:(UIApplication *)application //Facebook SDK
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
fallbackHandler:^(FBAppCall *call) {
NSLog(@"In fallback handler");
}];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[FBSession.activeSession close];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBAppEvents activateApp];
[FBAppCall handleDidBecomeActive];
}
@end
Thank: https://developers.facebook.com/
Thank: https://developers.facebook.com/
Comments
Post a Comment