I've added the facebook login button as described in the tutorial here. When I try testing this out however, the FB login button simply shows a loading indicator and the app freezes up. Is there any other integration I forgot to implement?
AppDelegate.m:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// Start updating locations when the app returns to the foreground.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
DefaultSettingsViewController *defaultSettingsViewController = [storyboard instantiateViewControllerWithIdentifier:#"DefaultSettingsViewController"];
//[defaultSettingsViewController.locationManager stopUpdatingLocation];
// Clear badge when app is opened
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (currentInstallation.badge != 0) {
currentInstallation.badge = 0;
[currentInstallation saveEventually];
}
[FBSDKAppEvents activateApp];
[FBAppCall handleDidBecomeActiveWithSession:[PFFacebookUtils session]];
}
DefaultViewController.m:
- (void)_loginWithFacebook {
// Set permissions required from the facebook user account
NSArray *permissionsArray = #[ #"user_about_me", #"user_relationships", #"user_birthday", #"user_location"];
// Login PFUser using Facebook
[PFFacebookUtils logInInBackgroundWithReadPermissions:permissions block:^(PFUser *user, NSError *error) {
if (!user) {
NSLog(#"Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew) {
NSLog(#"User signed up and logged in through Facebook!");
} else {
NSLog(#"User logged in through Facebook!");
}
}];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (![PFUser currentUser]) { // No user logged in
// Create the log in view controller
PFLogInViewController *logInViewController = [[PFLogInViewController alloc] init];
[logInViewController setDelegate:self]; // Set ourselves as the delegate
[logInViewController setFacebookPermissions:[NSArray arrayWithObjects:#"friends_about_me", nil]];
[logInViewController setFields: PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton | PFLogInFieldsFacebook | PFLogInFieldsSignUpButton | PFLogInFieldsPasswordForgotten];
// Create the sign up view controller
PFSignUpViewController *signUpViewController = [[PFSignUpViewController alloc] init];
[signUpViewController setDelegate:self]; // Set ourselves as the delegate
// Assign our sign up controller to be displayed from the login controller
[logInViewController setSignUpController:signUpViewController];
// Present the log in view controller
[self presentViewController:logInViewController animated:YES completion:NULL];
} else {
[self performSegueWithIdentifier:#"login" sender:self];
}
}
Can you re-run your app with a breakpoint on the line of code in DefaultViewController.m
[logInViewController setDelegate:self]; // Set ourselves as the delegate
And in the identity inspector tell me what objects it contains?
I haven't had much experience with Parse, but it sounds like one of the frameworks may not be implemented properly. (if that's the case, it's a quick fix. you just re-download and put it back in your app)
Related
im using facebook login in my app and a want to proceed user to main interface after he logged oon facebook.
Here is how im handling results:
- (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error {
if (error) {
NSLog(#"FB login failed");
}
else if (result.isCancelled) {
NSLog(#"FB login cancelled");
}
else {
[self performSegueWithIdentifier:#"fbLogin" sender:self];
NSLog(#"User logged in");
}
}
The problem is that in good case my NSLog(#"User logged in"); calls, but segue doesnt perform
Added:
Here is my viewDidLoad method to provide login button:
- (void)viewDidLoad {
[super viewDidLoad];
fbLoginButton.readPermissions = #[#"public_profile"];
fbLoginButton.publishPermissions = #[#"publish_actions"];
fbLoginButton.delegate = self;
}
And AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
[FBSDKLoginButton class];
[FBSDKShareButton class];
return YES;
}
I also want to say, that login works correctly, its just segue problem
I Want open facebook application at login time that already install in the device for login authentication, but always open in the Safari browser.
- facebook button click
-(void)loginButtonClicked
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
// [login setLoginBehavior:FBSDKLoginBehaviorNative];
[login logInWithReadPermissions:#[#"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
NSLog(#"Login process error");
}
else if (result.isCancelled)
{
NSLog(#"User cancelled login");
}
else
{
NSLog(#"Login Success");
if ([result.grantedPermissions containsObject:#"email"])
{
NSLog(#"result is:%#",result);
[self fetchUserInfo];
}
else
{
// [SVProgressHUD showErrorWithStatus:#"Facebook email permission error"];
}
}
}];
}
}
-here to fetch user info
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(#"Token is available : %#",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"results:%#",result);
NSString *email = [result objectForKey:#"email"];
NSString *userId = [result objectForKey:#"id"];
if (email.length >0 )
{
NSString *accessToken = [[FBSDKAccessToken currentAccessToken] tokenString];
[ref authWithOAuthProvider:#"facebook" token:accessToken
withCompletionBlock:^(NSError *error, FAuthData *authData)
{
if (error)
{
NSLog(#"Login failed. %#", error);
}
else
{
NSLog(#"Logged in! %#", authData);
//Start you app Todo
}
else
{
NSLog(#"Facebook email is not verified");
}
}
else
{
NSLog(#"Error %#",error);
}
}];
}
I Want to open facebook application for login with facebook.
From V4.6.0 it won't redirect to fb app. See below
(v4.6.0 - September 10, 2015) In addition, the SDK dialogs such as
Login, Like, Share Dialogs automatically determine the best UI based
on the device, including SFSafariViewController instead of Safari.
Follow the our Preparing for iOS 9 guide.
Please Use FBLoginView as Facebook SignIn button It automatically detects Facebook app & u can able to login with facebook.
FBLoginView class available in Facebook SDK.
Please set info.plist parameters properly, you may have not provided the URLs like this
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb776035152513539</string>
</array>
</dict>
</array>
and
<key>FacebookAppID</key>
<string>776035152513539</string>
If you are not able to achieve what you want,then you can use these classes.
Header File -
//
// LxFaceBookHandler.h
// KickOffSlotMachine
//
// Created by Prakhar Goyal on 06/08/15.
// Copyright (c) 2015 LOGICNEXT. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
typedef NS_ENUM(NSInteger,LOGINRESPONSE)
{
LOGINRESPONSE_ERROR,
LOGINRESPONSE_CANCEL,
LOGINRESPONSE_SUCCESS
};
#protocol LxFaceBookHandlerDelegate <NSObject>
-(void)DidLogInWithResponse:(LOGINRESPONSE)type;
#end
#interface LxFaceBookHandler : NSObject
#property(weak,nonatomic)id<LxFaceBookHandlerDelegate>delegate;
-(void)InitFaceBookLogin;
//Called from app Delegate
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
#end
and Implementation file is -
//
// LxFaceBookHandler.m
// KickOffSlotMachine
//
// Created by Prakhar Goyal on 06/08/15.
// Copyright (c) 2015 LOGICNEXT. All rights reserved.
//
#import "LxFaceBookHandler.h"
#implementation LxFaceBookHandler
#synthesize delegate = __delegate;
- (instancetype)init
{
self = [super init];
if (self)
{
__delegate = nil;
}
return self;
}
-(void)InitFaceBookLogin;
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
if (__delegate!=nil)
{
if ( [__delegate respondsToSelector:#selector(DidLogInWithResponse:)])
{
[__delegate DidLogInWithResponse:LOGINRESPONSE_ERROR];
}
}
}
else if (result.isCancelled)
{
if (__delegate!=nil)
{
if ( [__delegate respondsToSelector:#selector(DidLogInWithResponse:)])
{
[__delegate DidLogInWithResponse:LOGINRESPONSE_CANCEL];
}
}
}
else
{
if (__delegate!=nil)
{
if ( [__delegate respondsToSelector:#selector(DidLogInWithResponse:)])
{
[__delegate DidLogInWithResponse:LOGINRESPONSE_SUCCESS];
}
}
// if ([result.grantedPermissions containsObject:#"email"]) {
// // Do work
// }
}
}];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
}
#end
Add FBSDKCoreKit.Framework and FBSDKLoginKit.Framework in your project and set your app Delefate like this -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor=[UIColor blackColor];
SplashViewController *rVC = [[SplashViewController alloc] initWithNibName:#"SplashViewController" bundle:nil];
self.mNavController=[[UINavigationController alloc]initWithRootViewController:rVC];
[self.mNavController setNavigationBarHidden:YES];
self.window.rootViewController=self.mNavController;
[self.window makeKeyAndVisible];
self.handler = [[LxFaceBookHandler alloc]init];
[self.handler application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [self.handler application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}
now you can use these classes as on any event suppose on button click-
- (IBAction)FacebookLoginPressed:(UIButton *)sender
{
App.handler.delegate = self;
[App.handler InitFaceBookLogin];
}
I'm using the twitter and Facebook login in the Parse SDK. For every app launch I can log into each service once, but when I logout using [PFUser logOut] I am unable to log in again. The [PFFacebookUtils logInWithPermissions] block never gets called either with a user or an error.
My (Facebook) Login Code is:
- (IBAction)facebookLogin:(id)sender {
NSArray *permissionsArray = #[ #"user_about_me" ];
// Login PFUser using Facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
if (!user) {
NSString *errorMessage = nil;
if (!error) {
NSLog(#"Uh oh. The user cancelled the Facebook login.");
errorMessage = #"Uh oh. The user cancelled the Facebook login.";
} else {
NSLog(#"Uh oh. An error occurred: %#", error);
errorMessage = [error localizedDescription];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Log In Error"
message:errorMessage
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"Dismiss", nil];
[alert show];
} else {
if (user.isNew) {
NSLog(#"User with facebook signed up and logged in!");
} else {
NSLog(#"User with facebook logged in!");
}
}
}];
}
My logout code is:
- (IBAction)loginButton:(id)sender {
if([PFUser currentUser]) {
[PFUser logOut];
NSLog(#"User is %#", [PFUser currentUser]);
NSLog(#"Facebook session is %#", [PFFacebookUtils session]);
NSLog(#"Facebook session is %#", FBSession.activeSession.observationInfo);
} else {
[self performSegueWithIdentifier:#"loginScreenSegue" sender:self];
}
}
Everything logs (null).
I assumed that as the behaviour was the same with Twitter, that it might be related to the OAuth related methods in my AppDelegate:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
withSession:[PFFacebookUtils session]];
}
- (BOOL) application:(UIApplication *)application
handleOpenURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
withSession:[PFFacebookUtils session]];
}
But have done a lot of research and don't seem to be missing anything...
I also have these in the AppDelegate:
- (void)applicationWillResignActive:(UIApplication *)application {
[[PFFacebookUtils session] close];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[[PFFacebookUtils session] close];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBAppCall handleDidBecomeActiveWithSession:[PFFacebookUtils session]];
}
Any help would be very gratefully received!
In you logout method, you should clear the Facebook session token as well like below:
[[FBSession activeSession]closeAndClearTokenInformation];
This will clear the active Facebook session's token.
OK, I finally solved this. The problem was that in my app setup on twitter, the permissions were set to 'Read only', not 'Read and Write'.
Changing this setting solved the problem with both facebook and twitter.
I have problem with facebook Login..I integrated Facebook into my app.First time when user wants to login it is showing login page,but from next time it is going to page "You have already authorised APP_NAME".
I checked for Scrumptious tutorial which is the sample which I got from Facebook SDK.and it is also working like same.
I want to login from different user,I dont want that page "You have already authorised APP_NAME" every time.
How to solve this.
Try this code in your app delegate class.
It checks whether the session is open or not and clears your login credentials from the device cache when you close the connection.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LoginVC *loginScreen=[[LoginVC alloc]initWithNibName:#"LoginVC" bundle:nil];
navigationController=[[UINavigationController alloc]initWithRootViewController:loginScreen];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.window.rootViewController=self.navigationController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// See if we have a valid token for the current state.
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
[self openSession];
// To-do, show logged in view
} else {
// No, display the login page.
[self showLoginView];
}
return YES;
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
UIViewController *topViewController =
[self.navigationController topViewController];
if ([[topViewController presentedViewController]
isKindOfClass:[LoginVC class]]) {
[topViewController dismissViewControllerAnimated:YES completion:nil];
}
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Once the user has logged in, we want them to
// be looking at the root view.
[self.navigationController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
[self showLoginView];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (void)openSession
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// We need to properly handle activation of the application with regards to Facebook Login
// (e.g., returning from iOS 6.0 Login Dialog or from fast app switching).
[FBSession.activeSession handleDidBecomeActive];
}
I hope this code will be helpful.
I have implemented Google Plus Api and login as it is given in the site. But it is giving problem after login when I click on allow Access as shown below:
The following message appears :
My Code is as follows,pls tell me If anything left:
- (void)viewDidLoad
{
self.signInButton.delegate = self;
self.signInButton.clientID = kClientId;
self.signInButton.scope = [NSArray arrayWithObjects:
#"https://www.googleapis.com/auth/plus.me",
nil];
SLNetworkAppDelegate *appDelegate = (SLNetworkAppDelegate*)
[[UIApplication sharedApplication] delegate];
appDelegate.signInButton = self.signInButton;
share =[[GPPShare alloc] initWithClientID:kClientId];
share.delegate = self; // optional
appDelegate.share=share;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction) didTapShare: (id)sender {
[[[[share shareDialog]
setURLToShare:[NSURL URLWithString:#"https://developers.google.com/+/mobile/ios/getting-started"]]
setPrefillText:#"testing share via google plus"] open];
// Or, without a URL or prefill text:
[[share shareDialog] open];
}
In App delegate file:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([signInButton handleURL:url
sourceApplication:sourceApplication
annotation:annotation]) {
return YES;
}
if ([self.share handleURL:url
sourceApplication:sourceApplication
annotation:annotation]) {
return YES;
}
return NO;
}
You have to assign redirect uri in your project with your bundle ID like this so after successful authentication your application will be launched from safari if your handle your url properly:
Place bundle Id as identifier and URLSchemes...