Facebook and twitter login with Parse iOS only works once - ios

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.

Related

facebook didCompleteWithResult

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

domain: #"com.facebook.sdk" - code 5 error using PFFacebookUtils

I'm trying to retrieve Facebook user friend list using this code:
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"I have a friend named %# with id %#", friend.name, friend.id);
}
}];
The login as Facebook user has been implemented using parse "PFFacebookUtils"
From this tutorial https://parse.com/tutorials/login-and-signup-views
and here exactly what I've done:
1. configured my .plist file.
2. In the AppDelegate.m I've added these methods:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Parse setApplicationId:#"your_application_id" clientKey:#"your_client_key"];
[PFFacebookUtils initializeFacebook];
}
// Facebook oauth callback
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [PFFacebookUtils handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [PFFacebookUtils handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Handle an interruption during the authorization flow, such as the user clicking the home button.
[FBAppEvents activateApp];
[FBSession.activeSession handleDidBecomeActive];
}
3.In the logInViewController.m:
logInViewController.fields = PFLogInFieldsLogInButton | PFLogInFieldsUsernameAndPassword | PFLogInFieldsFacebook | PFLogInFieldsSignUpButton | PFLogInFieldsPasswordForgotten;
logInViewController.facebookPermissions = #[ #"public_profile",#"email",#"user_friends"];
I can Login successfully, but when I retrieve the user friend I get this error: (Note: I'm using test users and created a friend list for them)
Please I would appreciate any help.

How to Get URL when application launch via facebook?

I am sharing a facebook link using this method presentShareDialogWithLink in my application. When user tap on the post in facebook application then this opens my application automatically, at that time i want to get the url on what user tap in facebook application. How do i get that url and where i'll get that url?
You can get the URL by method handleOpenURL in AppDelegate
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
/* Do something with the url */
}
If you've set up a handler during the present* call, something like:
[FBDialogs presentShareDialogWithLink:url
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
NSLog(#"Error: %#", error.description);
} else {
NSLog(#"Success!");
}
}];
Then in your AppDelegate, override the application:openURL:sourceApplication:annotation: method:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
BOOL urlWasHandled = [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
fallbackHandler:^(FBAppCall *call) {
NSLog(#"Unhandled deep link: %#", url);
}];
return urlWasHandled;
}
Then your handler should get a callback.
For more info, see https://developers.facebook.com/docs/ios/share-dialog/, in particular, https://developers.facebook.com/docs/ios/share-dialog/#handling-responses

Google+ Sign-In button customization

How to customize Google+ Sign-In button ios ?
is there a way to go directly for sign in with out clicking Google+ Sign-In button ?
Yes, there is way to directly sign in Google+.
In AppDelegate, add this,
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [GPPURLHandler handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
And your login view controller this code parts should be added.
- (void)loginWithGooglePlus
{
[GPPSignIn sharedInstance].clientID = kClientID;
[GPPSignIn sharedInstance].scopes= [NSArray arrayWithObjects:kGTLAuthScopePlusLogin, nil];
[GPPSignIn sharedInstance].shouldFetchGoogleUserID=YES;
[GPPSignIn sharedInstance].shouldFetchGoogleUserEmail=YES;
[GPPSignIn sharedInstance].delegate=self;
[[GPPSignIn sharedInstance] authenticate];
}
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error
{
if (!error)
{
NSLog(#"Google+ login successful");
}
else
{
NSLog(#"Error: %#", error);
}
}
kClientID is your app client id taken from google your registered apps. Of course you need to set the delegate ( GPPSignInDelegate ).

Facebook log in error 400

I am logging in into facebook in this way:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
return YES;
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
if (!error) {
// We have a valid session
//NSLog(#"User session found");
// Initiate a Facebook instance
self.facebook = [[Facebook alloc]
initWithAppId:FBSession.activeSession.appID
andDelegate:nil];
// Store the Facebook session information
self.facebook.accessToken = FBSession.activeSession.accessToken;
self.facebook.expirationDate = FBSession.activeSession.expirationDate;
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
// Clear out the Facebook instance
self.facebook = nil;
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
self.openedURL = url;
// attempt to extract a token from the url
return [FBSession.activeSession handleOpenURL:url];
}
...and I get the error: Error: HTTP status code: 400
I have set up the URL scheme in the plist. Why am I getting this?
Check in the App Dashboard. Look at your Basic Settings. Your Sandbox Mode may be set to Enabled. The errors should go away when your app is out of Sandbox mode.

Resources