使用 google plus 登录,无需在 iPhone 中打开外部浏览器 [英] Login with google plus with out opening external browser in iPhone
问题描述
我的应用最近被拒绝了,因为我使用 Google Plus 登录与外部浏览器的集成.于是看了几篇帖子,找到了创建UIApplication
类来处理外部浏览器,并使用默认的UIWebView
的解决方案.我已完成所有操作,但在 UIWebView
中打开 google 登录页面时遇到问题.这是我的代码.
My app was recently rejected, because I am using Google Plus Login integration with external browser. So read a few threads, and found a solution to create UIApplication
class to handle external browser, and use default UIWebView
. I have done everything, but I have a problem to open google login page inUIWebView
. Here is my code.
LoginWithGoogle.h
#import <UIKit/UIKit.h>
#define ApplicationOpenGoogleAuthNotification @"ApplicationOpenGoogleAuthNotification"
@interface LoginWithGoogle : UIApplication
@end
LoginWithGoogle.m
#import "LoginWithGoogle.h"
@implementation LoginWithGoogle
- (BOOL)openURL:(NSURL*)url {
if ([[url absoluteString] hasPrefix:@"googlechrome-x-callback:"]) {
return NO;
} else if ([[url absoluteString] hasPrefix:@"https://accounts.google.com/o/oauth2/auth"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:ApplicationOpenGoogleAuthNotification object:url];
return NO;
}
return [super openURL:url];
}
@end
并且我在 info.plist
中添加了 LoginWithGoogle 作为主体类.我捕获通知并将 UIWebView
打开到另一个类中.现在我想在我自己的 UIWebView 中打开 Google Plus 登录视图,就像谷歌在 UIWebView 的 shouldStartLoadWithRequest
委托中自动打开到 Safari 或外部浏览器一样.我尝试了以下代码:
And I added LoginWithGoogle as the principal Class in info.plist
.
I catch notification and Open UIWebView
into another class.
Now I want to open Google Plus login view in my own UIWebView, like Google's auto-open into Safari or external browser in shouldStartLoadWithRequest
delegate of UIWebView.
I tried following code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] absoluteString] hasPrefix:@"com.XXX.XXX:/oauth2callback"]) {
[GPPURLHandler handleURL:url sourceApplication:@"com.google.chrome.ios" annotation:nil];
[self.navigationController popViewControllerAnimated:YES];
return NO;
}
return YES;
}
但是上面代码中的 URL 应该是什么?如果我错了,请指导我如何在 UIWebView 中打开登录页面.
But what should be the URL in above code? If I am wrong, then kindly guide me how to open Login Page in a UIWebView.
推荐答案
1) 创建 UIApplication 的子类,该子类覆盖 openURL:
1) create a subclass of UIApplication, which overrides openURL:
.h
#import <UIKit/UIKit.h>
#define ApplicationOpenGoogleAuthNotification @"ApplicationOpenGoogleAuthNotification"
@interface Application : UIApplication
@end
.m
#import "Application.h"
@implementation Application
- (BOOL)openURL:(NSURL*)url {
if ([[url absoluteString] hasPrefix:@"googlechrome-x-callback:"]) {
return NO;
} else if ([[url absoluteString] hasPrefix:@"https://accounts.google.com/o/oauth2/auth"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:ApplicationOpenGoogleAuthNotification object:url];
return NO;
}
return [super openURL:url];
}
@end
- 这基本上会阻止从 iOS 上的 Chrome 打开任何内容
- 我们捕获 auth 调用并将其重定向到我们的内部 UIWebView
2) 到 info.plist,添加 Principal 类,并为其添加 Application(或任何您命名的类)
2) to info.plist, add the Principal class, and for it Application (or whatever you named the class)
3) 捕获通知并打开内部 webview
3) catch the notification and open an internal webview
4) 在 webview 内部
4) inside the webview
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] hasPrefix:@"com.XXX.XXX:/oauth2callback"]) {
[GPPURLHandler handleURL:url sourceApplication:@"com.google.chrome.ios"n annotation:nil];
[self.navigationController popViewControllerAnimated:YES];
return NO;
}
return YES;
}
- 或类似的代码,用于处理响应
- 您可能希望使用 @"com.apple.mobilesafari" 作为 sourceApplication 参数
这篇关于使用 google plus 登录,无需在 iPhone 中打开外部浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!