Good morning,
I'm trying to implement the Facebook login (which is working fine) and also the google plus login in the same view. I'm following the guides from the official site (Google) but there is an issue between the Facebook button and the google plus button:
Facebook tells me to put the following code:
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication];
}
And also Google tells me to put the following:
- (BOOL)application: (UIApplication *)application
openURL: (NSURL *)url
sourceApplication: (NSString *)sourceApplication
annotation: (id)annotation {
return [GPPURLHandler handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
And I can't have the same functions with the same name and I can't integrate one inside the other. That's why I need your help because I don't know how to deal with this issue between these guidelines.
Thanks in advance.
Here you will have to check [url scheme] before returning. Example code is below.
-(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];
}
}
Hope it will solve your problem..
Here is the version in Swift.
Firstly, you should understand how to see difference between Google and Facebook return url. Algorithm for detection is:
Facebook URL pattern: fb1106884526040279://authorize/...
with fb1106884526040279
is your app id. So you just need to check if this url start with your app id, is host equals to authorize
(the string after //
and first /
)
Google URL Pattern: com.googleusercontent.apps.803921065829-m34o26vcj57oirk1oa8pqve5o22qdihn
So you just need to check if this url start with com.googleusercontent.apps
.
Base on above explanation, I have generalized into GoogleAuth
and FacebookAuth
utils class with some small methods such as logout, check login ...
Here is 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.")
}
}
Secondly is 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"
}
}
Here is the main part. In AppDelegate
, you call isValidateWithUrl
method for checking if this url is from Google or 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
}
}
Hope this help :)