Google+ Sign-In button customization - ios

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 ).

Related

Determine whether Dropbox or Facebook SDKs should handle an application delegate call

I have to integrate the latest FB-SDK into my iOS app - and the nightmare starts with the app delegate.
The docs say to implement this in the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return [[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
];
}
But my app isn't for FB only, it does Dropbox and YouTube uploads as well.
So my application:openURL: method looks like this now:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
// Facebook
if([FBSession.activeSession handleOpenURL:url]){
BOOL result = [FBSession.activeSession handleOpenURL:url];
return result;
}
// Dropbox
if ([[DBSession sharedSession] handleOpenURL:url]) {
if ([[DBSession sharedSession] isLinked]) {
NSLog(#"App linked successfully!");
}
return YES;
}
// Add whatever other url handling code your app requires here
return NO;
}
FBSession doesn't exist anmyore, though. So how do I tell the openURL: calls from Facebook and Dropbox apart?

Facebook login: persisting across app launches

I'm logging in users with the following method, which works fine. However, each time the app relaunches, I need to run this method again to regain access.
The Facebook documentation seems to be very limited, and all the advice I can find (including on Stack Overflow) is related to the old Facebook SDK.
From what I can understand, a FBSDKAccessToken should be being cached when the user is first logged in. However, when I check [FBSDKAccessToken currentAccessToken] on subsequent launches, nothing is returned.
Any advice very welcome.
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithPublishPermissions:#[#"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
} else if (result.isCancelled) {
// Handle cancellations
} else {
if ([result.grantedPermissions containsObject:#"publish_actions"]) {
// success
}
}
}];
Lol, searched for hours then came here 5 minutes too soon. The problem was that I needed to add the following in AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return [[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
}
You ALSO want to add this in the following method:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
BOOL facebookCheck = [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
if (!facebookCheck){
...
}
return true;
}
You probably wont need it now, but it doesn't hurt. :)

Facebook and twitter login with Parse iOS only works once

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.

Google sign-in finishedWithAuth gets called only after clicking twice

I am trying to set up Google login on my app and I have setup the following function for it:
-(void) performGoogleLogin{
GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.shouldFetchGooglePlusUser = YES;
signIn.shouldFetchGoogleUserEmail = YES;
signIn.clientID = kClientId;
signIn.scopes = #[ #"email" ];
signIn.delegate = self;
[signIn authenticate];
}
When I do this the first time, the app opens Safari where I grant permissions. After returning to my app, this function in my AppDelegate gets called:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [GPPURLHandler handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
But then, the -(void) finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error function does not get called immediately. It only gets called when I invoke the performGoogleLogin function by clicking my button a second time. Any idea why this might be happening?
Looks like a duplicate of finishedWithAuth not called after authenticate method
Make GPPSignIn a class-level variable to avoid it getting garbage collected when the device switches to Safari for the login.

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

Resources