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.
Related
I have some trouble when I try to implement FBSDKShareDialog to perform facebook sharing via my app. If official fb app is installed it shows blank dialog (where user allows to type some text etc). If user doesn't have fb app it shows correct info but FBSDKSharingDelegate methods doesn't work. (It doesn't work even when user has fb application)
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results
{
NSLog(#"complete");
}
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer
{
NSLog(#"did cancel");
}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error
{
NSLog(#"%#", error);
}
It's never called even when I cancel or share via FB application.
My code is:
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentTitle = _place.title;
content.imageURL = [NSURL URLWithString:_place.logoUrl];
content.contentDescription = _place.info;
[FBSDKShareDialog showFromViewController:_viewController withContent:content delegate:self];
I have a delegate inside #interface declaration.
#interface FacebookHandler () < FBSDKSharingDelegate >
#end
My app delegate has
#import <FBSDKCoreKit/FBSDKCoreKit.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return [[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
Where I was wrong? And is it good idea to post some image and title via FBSDKShareLinkContent. Thanks in advance.
Are you calling [FBSDKShareAPI shareWithContent:content delegate:self] from the main thread?
I've already fixed this issue. The problem was that I've delegate to file inherited from NSObject, which can't be a delegate
I'm implementing the share dialog using the Facebook SDK for iOS.
Everything works fine except for the callback.
This is the function that displays the dialog:
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = <my content url>;
content.contentTitle = <my title>;
content.contentDescription = my description;
content.imageURL = <my image url>;
[FBSDKShareDialog showFromViewController:self
withContent:content
delegate:self];
The view controller implements the FBSDKSharingDelegate and the three required methods:
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results;
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error;
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer;
Basically I have to detect if users pressed the cancel button because I give them a reward only if they effectively share the content.
The problem is that even if I press the cancel button the only callback that is invoked is:
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results;
and not
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer;
as expected. Furthermore using iOS8 the variable results is empty if the cancel button is pressed, otherwise it contains the post_id, but this does not happen with iOS7, where the result is always empty.
What am I doing wrong? What am I supposed to do to have the sharerDidCancel callback working properly?
Thanks for any help!
Try entering this to your appdelegate. It worked for me to call the right delegate but the result dictionary is still empty and I can't get the post_id.
#import <FBSDKCoreKit/FBSDKCoreKit.h>
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
- (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];
}
#class GPPSignInButton;
I don't want use the GPPSignInButton for login and authentication i want to use my custom button when i pressed then the login process will start and the login with the Forcefully webview like facebook did it is possible in Google+ login too?
like the "Using the OAuth 2 Controllers"
Thanks in advance
anyhelp will be appreciated.
Yes you can use any UIButton instead of using GPPSignInButton.
Initialise the Google+ sdk in viewDidLoad using :
GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.shouldFetchGooglePlusUser = YES;
signIn.shouldFetchGoogleUserEmail = YES; // Uncomment to not get the user's email
signIn.clientID = kClientId;
signIn.scopes = [NSArray arrayWithObjects:kGTLAuthScopePlusMe,kGTLAuthScopePlusLogin, nil];
signIn.delegate = self;
Now in the uibutton action
- (IBAction)googlePlusSignIn:(id)sender
{
GPPSignIn *signIn = [GPPSignIn sharedInstance];
if([[GPPSignIn sharedInstance] authentication])
{
[signIn trySilentAuthentication];
}
else
{
// This is method that opens the G+ sign view
[signIn authenticate];
}
}
Add URL type and URL scheme in Targets -> Info -> URL Types .
Then add the following in App Delegate
- (BOOL)application: (UIApplication *)application
openURL: (NSURL *)url
sourceApplication: (NSString *)sourceApplication
annotation: (id)annotation {
return [GPPURLHandler handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
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 ).
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...