我已将Google+ iOS SDK集成到iPhone应用程序中。
我想在用户输入帐户详细信息后让用户登录。
问题是如果应用程序终止并打开再一次,用户已退出,我需要让用户登录。
有没有办法存储访问令牌之类的内容并使用它而不是在屏幕上显示登录?或者是因为Google本身在申请终止时签署了用户?
答案 0 :(得分:1)
就我而言,使GPPSignIn
对象全局解决了问题。
以下是如何操作:
在Supporting Files
文件夹 - > appname-Prefix.pch
文件中,添加以下行:
#import <GooglePlus/GooglePlus.h>
GPPSignIn * signIn;
在这一行之下:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
答案 1 :(得分:1)
iOS 9,xcode 7.0.1的此问题的解决方案的当前版本是
(void) signInSilently
使用 GIDSignIn 对象引用来调用它。
由谷歌提供: 尝试在没有交互的情况下登录以前经过身份验证的用户。 Reference Link
作为回应,它将返回
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error{}
如果用户已过期或已过期,(GIDGoogleUser *)用户将 nill ,详情将与(NSError *)错误如果是用户,则反之亦然。
希望它会对某人有所帮助。
答案 2 :(得分:0)
登录Google+。以下是几个步骤
1&GT;首先,点击“API访问窗格”
,添加Google+ SDK和create Google plus client id插入 GoogleOpenSource.framework ,* GooglePlus.bundle *, GooglePlus.framework
2&GT;现在全局为ClientID
分配一个宏
#define kClientID @"//paas client id which your app provided by developer account portal";
3&GT;在AppDelegate.h
文件
#import <GooglePlus/GooglePlus.h>
extern NSString *const FBSessionStateChangedNotification;
@interface AppDelegate : UIResponder <UIApplicationDelegate,GPPDeepLinkDelegate>
{
}
-(void)clearApplicationCaches;
4.&GT;在AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[GPPSignIn sharedInstance].clientID = kClientID;
[GPPDeepLink setDelegate:self];
[GPPDeepLink readDeepLinkAfterInstall];
[self.window makeKeyAndVisible];
return YES;
}
- (void)didReceiveDeepLink:(GPPDeepLink *)deepLink
{
}
#pragma mark - Memory management methods
-(void)clearApplicationCaches
{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
[self clearApplicationCaches];
}
- (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication: (NSString *)sourceApplication annotation: (id)annotation
{
return [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation];
}
提示:如果您希望测试到此时为止,可以在[signIn authenticate]
方法结束时致电viewDidLoad
。应用程序将在启动时将您重定向到登录对话框
5&GT;现在转到您的视图控制器和yourViewController.h
文件
#import <GooglePlus/GooglePlus.h>
@class GPPSignInButton;
@interface yourViewController:UIViewController <GPPSignInDelegate>
{
}
@property(nonatomic, strong) IBOutlet GPPSignInButton *signInButton;
如果需要,合成GPPSignInButton
属性,尽管在较新版本的Xcode中不再需要此步骤。
@synthesize signInButton;
- (void)viewDidLoad
{
[super viewDidLoad];
[GPPSignInButton class];
GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.clientID = kClientID;
signIn.delegate = self;
[signIn trySilentAuthentication]; /// this calls finishedWithAuth automatically
}
将GPPSignInButton
添加到故事板,XIB文件或以编程方式对其进行实例化。 GPPSignInButton
继承自UIButton
,因此如果您使用的是Storyboard或XIB文件,则可以将Round Rect按钮或View拖到视图控制器上,并将其自定义类设置为GPPSignInButton
。 / p>
将按钮连接到视图控制器的signInButton
属性。
#pragma mark -
#pragma mark - GooglePluseDelegate Implementation
- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth error: (NSError *) error
{
NSLog(@"email %@ ",[NSString stringWithFormat:@"Email: %@",[GPPSignIn sharedInstance].authentication.userEmail]);
NSLog(@"Received error %@ and auth object %@",error, auth);
// 1. Create a |GTLServicePlus| instance to send a request to Google+.
GTLServicePlus* plusService = [[GTLServicePlus alloc] init] ;
plusService.retryEnabled = YES;
// 2. Set a valid |GTMOAuth2Authentication| object as the authorizer.
[plusService setAuthorizer:[GPPSignIn sharedInstance].authentication];
GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"];
// *4. Use the "v1" version of the Google+ API.*
plusService.apiVersion = @"v1";
[plusService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLPlusPerson *person,
NSError *error) {
if (error) {
GTMLoggerError(@"Error: %@", error);
[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication]keyWindow] animated:TRUE];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
} else
{
NSLog(@"userId = %@",query.activityId);
NSLog(@"person = %@",[person.name.familyName stringByAppendingFormat:@" %@",person.name.givenName]);
NSLog(@"person = %@", person);
NSDictionary *tempDic = [NSDictionary dictionaryWithObjectsAndKeys:[GPPSignIn sharedInstance].authentication.userEmail,@"email",person.identifier,@"googleID",[person.name.givenName stringByAppendingFormat:@" %@",person.name.familyName],@"name",[person.name.givenName stringByAppendingFormat:@" %@",person.name.familyName],@"username",person.gender,@"gender", nil];
[NSThread detachNewThreadSelector:@selector(parsingLoginWithGoogleApi:) toTarget:self withObject:tempDic];
}
}];
}
// ---- its call a web service to login with google+ info
-(void) parsingLoginWithGoogleApi:(NSDictionary*) googleUserInfoDic
{
NSLog(@"google User Info Dic = %@", googleUserInfoDic );
NSString *name = @"";
NSString *username = @"";
NSString *emailId = @"";
NSString *gender = @"";
NSString *apiURLStr =@"";
if ([DEFAULTS objectForKey:@"isLogin"])
{
name =[DEFAULTS objectForKey:@"g_name"];
emailId =[DEFAULTS objectForKey:@"g_emailId"];
username =[DEFAULTS objectForKey:@"g_username"];
gender =[DEFAULTS objectForKey:@"g_gender"];
apiURLStr =[NSString stringWithFormat:@"%@google_login/%@/%@/%@/%@",SiteAPIURL,emailId,[name stringByURLEncode],[username stringByURLEncode],gender];
}
else
{
name =[googleUserInfoDic valueForKey:@"name"];
username =[googleUserInfoDic valueForKey:@"username"];
emailId = @"0";
if ([googleUserInfoDic valueForKey:@"email"])
emailId=[googleUserInfoDic valueForKey:@"email"];
gender = @"0";
if ([googleUserInfoDic valueForKey:@"gender"])
gender =[googleUserInfoDic valueForKey:@"gender"];
apiURLStr =[NSString stringWithFormat:@"%@google_login/%@/%@/%@/%@",SiteAPIURL,emailId,[name stringByURLEncode],[username stringByURLEncode],gender];
}
NSLog(@"login Google apiURLStr==>>>%@",apiURLStr);
NSString *outputStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiURLStr] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"login Google response==>>>%@",outputStr);
if (outputStr == NULL)
{
NSDictionary *infoDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"There was a small problem",@"title",@"The network doesn't seem to be responding, please try again.",@"message",@"OK",@"cancel",@"1",@"tag",self,@"delegate", nil];
[CommonFunctions performSelectorOnMainThread:@selector(showAlertWithInfo:) withObject:infoDic waitUntilDone:NO];
[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication]keyWindow] animated:TRUE];
}
else
{
NSDictionary *jsonResponse = [outputStr JSONValue];
if (jsonResponse != nil)
{
NSString *replycode=[jsonResponse objectForKey:@"replyCode"];
if ([replycode isEqualToString:@"success"])
{
NSUserDefaults *userDefs = [NSUserDefaults standardUserDefaults];
[userDefs setObject:name forKey:@"g_name"];
[userDefs setObject:emailId forKey:@"g_emailId"];
[userDefs setObject:username forKey:@"g_username"];
[userDefs setObject:gender forKey:@"g_gender"];
[userDefs setObject:[jsonResponse objectForKey:@"sesId"] forKey:@"sessionId"];
sessionId = [jsonResponse objectForKey:@"sesId"];
[userDefs setObject:username forKey:@"username"];
[userDefs setObject:[jsonResponse objectForKey:@"userId"] forKey:@"userId"];
[userDefs setObject:@"YES" forKey:@"isLogin"];
[userDefs setObject:@"Google" forKey:@"LoginType"];
userId = [jsonResponse objectForKey:@"userId"];
[userDefs synchronize];
[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication]keyWindow] animated:TRUE];
[self performSelectorOnMainThread:@selector(pushProfileVC:) withObject:[NSNumber numberWithBool:YES] waitUntilDone:NO];
}
else
{
[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication]keyWindow] animated:TRUE];
NSDictionary *infoDic = [[NSDictionary alloc] initWithObjectsAndKeys:replycode,@"title",[jsonResponse objectForKey:@"replyMsg"],@"message",@"OK",@"cancel",@"1",@"tag",nil,@"delegate", nil];
[CommonFunctions performSelectorOnMainThread:@selector(showAlertWithInfo:) withObject:infoDic waitUntilDone:NO];
}
}
else
{
[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication]keyWindow] animated:TRUE];
NSDictionary *infoDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"There was a small problem",@"title",@"The network doesn't seem to be responding, please try again.",@"message",@"OK",@"cancel",@"1",@"tag",self,@"delegate", nil];
[CommonFunctions performSelectorOnMainThread:@selector(showAlertWithInfo:) withObject:infoDic waitUntilDone:NO];
}
}
}
我希望它会对你有所帮助。感谢