来自谷歌的更新
今天,我们发布了新的 Google Sign In iOS SDK,具有完整的内置功能
支持通过 WebView 登录:
Developers.google.com/identity/sign-in/ios SDK 支持调度
到任何一个处理登录的 Google 应用程序(如果存在),使用
之后的WebView回退。在所有情况下,都避免使用 Safari 开关,
我们认为这是避免应用程序被拒绝的关键因素。
我们期待从使用新功能的人们那里获得反馈
SDK,希望它的使用能够取代(巧妙且勤奋)
人们同时实施了解决方法。
不再需要下面的方法
此方法使用自定义 UIWebView 处理登录内部
此方法可行并已获得 Apple 批准
我的应用程序因此被从审核中踢出
"The app opens a web page in mobile Safari for logging in to Google plus,
then returns the user to the app. The user should be able log in without opening
Safari first."
请参阅此链接https://code.google.com/p/google-plus-platform/issues/detail?id=900 https://code.google.com/p/google-plus-platform/issues/detail?id=900我确实通过以下步骤解决了这个问题
1)创建UIApplication的子类,它重写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(或任何您命名的类)
添加 plist 键“NSPrincipalClass”并将主应用程序的类作为值(扩展 UIApplication 的类,在本例中为应用程序(请参见上面的代码))
3)捕获通知并打开内部webview
当您的自定义应用程序类发送 ApplicationOpenGoogleAuthNotification 时,在某个地方(可能在 AppDelegate 中)监听它,当您捕获此通知时,打开一个 UIWebView(使用通知传递的 URL 作为 Web 视图的 url)(在我的情况下,LoginViewController 监听对于此通知,当收到时,它会打开一个视图控制器,其中仅包含连接到委托的 Web 视图)
4)网络视图内部
- (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];
// Looks like we did log in (onhand of the url), we are logged in, the Google APi handles the rest
[self.navigationController popViewControllerAnimated:YES];
return NO;
}
return YES;
}
- 或类似的代码,处理响应
- 上面代码中的 com.XXX.XXX:/oauth2callback,替换为您的公司和应用程序标识符,例如“com.company.appname:/oauth2callback”
- 您可能想使用 @"com.apple.mobilesafari" 作为 sourceApplication 参数