首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google +在应用程序中注册

Google +在应用程序中注册
EN

Stack Overflow用户
提问于 2014-10-09 05:20:48
回答 2查看 407关注 0票数 0

我想将Google+登录功能集成到我的应用程序中。我使用过Google+ sdk,但它重定向到safari登录到google,我想在我的应用程序中打开这个网页对话框。有人能帮我做那件事吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-13 06:57:04

我做到了。继承UIApplication类,然后在web视图中处理url。这是代码:

将Google+ FrameWork添加到项目中。

现在,在身份验证调用中,通过继承UIAPPLICATION类来捕获该URL。

代码语言:javascript
复制
- (IBAction)btnGooglePlusTap:(id)sender
{
   [[GPPSignIn sharedInstance] authenticate];
}

SubClassUIApplication.h 

#import <UIKit/UIKit.h>

#define ApplicationOpenGoogleAuthNotification @"GoogleFriendInvitationPosted"

@interface SubClassUIApplication : UIApplication

@end


#import "SubClassUIApplication.h"

@implementation SubClassUIApplication

- (BOOL)openURL:(NSURL *)url
{
    if([[url absoluteString] hasPrefix:@"googlechrome-x-callback:"])
    {
        return NO;//This will prevent call to Google Chrome App if installed.
    }
    else if([[url absoluteString] hasPrefix:@"https://accounts.google.com/o/oauth2/auth"])
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:ApplicationOpenGoogleAuthNotification object:url];

        return NO;// Here we will pass URL to notification and from notification observer , we will load web view.
    }
    else if ([[url absoluteString] hasPrefix:@"com.google"])
    {
        return NO;
    }

    return [super openURL:url];
}

@end

将此子类设置为info.plist文件中的主体类。

现在打开到web视图的URL

代码语言:javascript
复制
- (void)catchNotificationforGooglePlusSharing:(NSNotification *)notiofication
{
    NSURL *u=(NSURL *)notiofication.object;

    UINavigationController *navWebView =(UINavigationController *) [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewNavController"];

    WebViewController *vcWebView = navWebView.viewControllers[0];
    vcWebView.webURL = u;
    [self.navigationController presentViewController:navWebView animated:YES completion:Nil];
}

现在在webviewcontroller.m .m中。

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURLRequest *req= [[NSURLRequest alloc]initWithURL:webURL];

    [webvGoogle loadRequest:req];
}
#pragma mark - UIWebViewDelegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    BOOL canHandle = [GPPURLHandler handleURL:request.URL sourceApplication:@"com.apple.mobilesafari" annotation:nil];

    if(canHandle == YES)
    {


        [self dismissViewControllerAnimated:YES completion:^
        {

        }];
    }

    return YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"Error in loading : %@",error.description);
}

不要忘记为ApplicationOpenGoogleAuthNotification通知注册类。

票数 2
EN

Stack Overflow用户

发布于 2015-02-24 13:18:09

您可以使用来自Google Mac - OAuth 2控制器工具箱Google Mac - OAuth 2控制器工具箱

代码语言:javascript
复制
#import <GTMOAuth2ViewControllerTouch.h>

GTMOAuth2ViewControllerTouch *googleAuthViewController = [[GTMOAuth2ViewControllerTouch alloc]
                       initWithScope:@"https://www.googleapis.com/auth/userinfo#email"
                       clientID:kClientId
                       clientSecret:kSecretId
                       keychainItemName:@"GooglePlus_Sample_App"
                       delegate:self
                       finishedSelector:@selector(viewController:finishedWithAuth:error:)];

[self.navigationController pushViewController:googleAuthViewController animated:YES];

这是一个在http://blog.krzyzanowskim.com/2015/02/22/google-sign-on-with-1password/中找到的示例,您可以在这里找到完整的示例项目。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26270868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档