Wir*_*jPS 7 facebook ios google-plus swift google-signin
早上好,
我正在尝试实施Facebook登录(工作正常)以及同一视图中的google plus登录.我正在关注官方网站(谷歌)的指南,但Facebook按钮和谷歌加号按钮之间存在问题:
Facebook告诉我要输入以下代码:
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication];
}
Run Code Online (Sandbox Code Playgroud)
Google还告诉我要做以下事情:
- (BOOL)application: (UIApplication *)application
openURL: (NSURL *)url
sourceApplication: (NSString *)sourceApplication
annotation: (id)annotation {
return [GPPURLHandler handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
Run Code Online (Sandbox Code Playgroud)
并且我不能使用相同的名称具有相同的功能,并且我无法将一个功能集成到另一个中.这就是为什么我需要你的帮助,因为我不知道如何在这些指导方针之间处理这个问题.
提前致谢.
Sre*_*att 15
在返回之前,您必须检查[url scheme].示例代码如下.
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog("%@", [url scheme]);
if([[url scheme] isEqualToString:FACEBOOK_SCHEME])
{
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
}
else if([[url scheme] isEqualToString:GOOGLE_PLUS_SCHEME])
{
return [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation];
}
}
Run Code Online (Sandbox Code Playgroud)
希望它能解决你的问题..
这是Swift中的版本.
首先,您应该了解如何查看Google和Facebook返回网址之间的区别.检测算法是:
Facebook网址格式:fb1106884526040279://authorize/...带有fb1106884526040279您的应用ID.所以你只需要检查这个url是否以你的app id开头,是否等于authorize(字符串后面//和第一个/)
谷歌网址格式:com.googleusercontent.apps.803921065829-m34o26vcj57oirk1oa8pqve5o22qdihn所以你只需要检查这个网址是否以com.googleusercontent.apps.
底座上面的解释,我已经泛化为GoogleAuth和FacebookAuthutils的类的一些小方法,如注销,查询登录...
这是 Google Authentication Utils
import Google
class GoogleAuth {
static func getInstance() -> GIDSignIn {
return GIDSignIn.sharedInstance()
}
static func isLogin() -> Bool {
return getInstance().hasAuthInKeychain()
}
static func signOut() {
getInstance().signOut()
}
static func isValidatedWithUrl(url: NSURL) -> Bool {
return url.scheme.hasPrefix(NSBundle.mainBundle().bundleIdentifier!) || url.scheme.hasPrefix("com.googleusercontent.apps.")
}
}
Run Code Online (Sandbox Code Playgroud)
其次是 Facebook Authentication Utils
import FBSDKLoginKit
class FacebookAuth {
static func isLogin() -> Bool {
return FBSDKAccessToken.currentAccessToken() != nil
}
static func signOut() {
FBSDKLoginManager().logOut()
}
static func isValidatedWithUrl(url: NSURL) -> Bool {
return url.scheme.hasPrefix("fb\(FBSDKSettings.appID())") && url.host == "authorize"
}
}
Run Code Online (Sandbox Code Playgroud)
这是主要部分.在AppDelegate,您调用isValidateWithUrl方法来检查此网址是来自Google还是Facebook.
func application(application: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {
print("step 2 of OAuth2. Url: \(url)")
// url from google
if GoogleAuth.isValidatedWithUrl(url) {
return GIDSignIn.sharedInstance().handleURL(
url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
// url from facebook
else if FacebookAuth.isValidatedWithUrl(url) {
return FBSDKApplicationDelegate.sharedInstance().application(application,
openURL: url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String,
annotation: options [UIApplicationOpenURLOptionsAnnotationKey])
}
// application hasn't supported this url yet
else {
return false
}
}
Run Code Online (Sandbox Code Playgroud)
希望这有帮助:)
| 归档时间: |
|
| 查看次数: |
3697 次 |
| 最近记录: |