Google plus authentication fails - swift

I am trying to integrate Google plus authentication in my app. Below is my code:
class ViewController: UIViewController, GPPSignInDelegate
override func viewDidLoad() {
super.viewDidLoad()
let signIn = GPPSignIn.sharedInstance()
signIn.shouldFetchGooglePlusUser = true
signIn.clientID = kClientId
signIn.scopes = [kGTLAuthScopePlusLogin]
signIn.delegate = self
signIn.authenticate()
}
func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) {
println("Received error \(error) and authentication \(auth)")
}
In AppDelegate
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return GPPURLHandler.handleURL(url, sourceApplication: sourceApplication, annotation: annotation)
}
Problem is that, it redirect me to safari for Google plus authentication. After authentication is shows a message "Safari cannot open page because address is invalid". Also the control does not return to the app. openURL() method is not called in appDelegate.
Please help me. I had followed the Google tutorial. I don't know where am I making mistake.
Any help will be appreciated. Thanks in advance

Related

400 Invalid_request You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy for keeping apps secure

I am getting this error when I am trying to register with Google in my IOS app. I have got the REVERSED-CLIEND_ID which looks like something like this: com.googleusercontent.apps..... So far I can open the google window and I get the 400 error.
AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
ApplicationDelegate.shared.application(application,didFinishLaunchingWithOptions: launchOptions)
GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
if error != nil || user == nil {
// Show the app's signed-out state.
} else {
// Show the app's signed-in state.
}
}
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
ApplicationDelegate.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
var handled: Bool
handled = GIDSignIn.sharedInstance.handle(url)
if handled {
return true
}
// Handle other custom URL types.
// If not handled by this app, return false.
return false
}
}
ViewController:
let signInConfig = GIDConfiguration.init(clientID: "REVERSED_URL_THING")
#IBAction func googleRegister(_ sender: UIButton) {
GIDSignIn.sharedInstance.signIn(
with: signInConfig,
presenting: self
) { user, error in
guard error == nil else { return }
guard let user = user else { return }
// Your user is signed in!
}
}
If anyone came across the same issue, in Xcode's URL Types add iOS URL scheme and do not use reversed url like it was said in many tutorials. Use the CLIEND ID which Google provides:
let signInConfig = GIDConfiguration.init(clientID: "CLIENT ID ")

Oauth2 URL callback Mac Swift 4

I am using OAuth 2 into my macOS swift 4 app.
I have successfully implemented the setup of the redirect URI and when the user grants permission, the redirect URI open my mac app using the URL Schemes.
However, there is information in the URL call back that I have been unable to get a method working to catch the redirect.
I've tried these methods in the App Delegate but they don't work:
func application(_ application: NSApplication,
open url: URL,
sourceApplication: String?,
annotation: Any) -> Bool {
//Extract information from URL here
}
and:
func application(_ app: NSApplication, open url: URL) -> Bool {
//Extract information from URL here
}
What is wrong with these methods?
What method should be used in Swift 4 for a Mac app to catch the call back?
Thanks in advance
Found the answer to the question:
func application(_ application: NSApplication,
open urls: [URL]) {
}
Source: https://developer.apple.com/documentation/appkit/nsapplicationdelegate/2887193-application

LinkedIn Swift session creation completion handler not being called

I'm trying to make a basic LinkedIn login for my app and obtain the user's profile url. I installed the LinkedIn SDK in my project. A couple of notes: In the info.plist, I have put the linkedin URL Scheme, the LSApplicationQueriesScheme and the ExceptionDomains attributes appropriately. In my app delegate, I have
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
if LISDKCallbackHandler.shouldHandle(url) {
return LISDKCallbackHandler.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
}
So, in one of my views I am trying to have the user login with linkedin. I put this code in to do that:
LISDKSessionManager.createSession(withAuth: [LISDK_BASIC_PROFILE_PERMISSION], state: nil, showGoToAppStoreDialog: true, successBlock: { (returnState) -> Void in
print("success called!")
print(LISDKSessionManager.sharedInstance().session)
}) { (error) -> Void in
print("Error: \(error)")
}
This code will bring the user to the linkedin app, prompt them to authorize the permissions for my app, and then returns back to my app. However, the success and error blocks never get called. I put breakpoints on both of them to make sure they just weren't printing empty strings, but they are just never called. I have tried waiting several minutes, and still nothing. What am I doing wrong?
I too had this issue. Finally solved by doing this :-
In the app delegate part you need to implement this part of code
if LISDKCallbackHandler.shouldHandle(url) {
return LISDKCallbackHandler.application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
return true
}
in the following delegate method
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
}
insted of
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
It should be like this
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if LISDKCallbackHandler.shouldHandle(url) {
return LISDKCallbackHandler.application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
return true
}
I maybe have a solution for you. Do you maybe have multiple apps on your phone that use the same Linkedin Application ID? If you do, you should just delete the other ones.

FB login using Swift

I was following this tutorial
https://developers.facebook.com/docs/facebook-login/ios.
I am in step 5. I'm trying to convert
BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
to swift, Here's what i did
let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: [UIApplicationOpenURLOptionsKey.sourceApplication], annotation: options[UIApplicationOpenURLOptionsKey.annotation])
return handled
I tried another
let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication://no idea what to put here , annotation: //and here)
How to resolve this issue?
P.S. I am a newbie in swift, just want to share a site where it could help https://objectivec2swift.com converting from obj-C to Swift.
I was working on Facebook login too. And the problem is here there are two very similar methods. The correct one for Facebook login is your second method which should look like this;
FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: [:])
By the way if you're a beginner like me i suggest you to subscribe and watch LetsBuildThatApp channel on Youtube. There is a tuttorial for Facebook Log-in too.
Have nice coding.
Just write this -
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

application openURL in Swift

I am having an issue with the Appdelegate method OpenURL.
I have setup my Imported UTI's and Document Type. But when opening my app from a mail attachment, the app crashes immediately when I have the method implemented.
The depreciated handleOpenURL works, but not OpenURL?
At the moment I have no code in the implementation, and am just returning true.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
return true
}
The crash says Thread 1: EXC_BAD_ACCESS (code-1, address-0x0)
I don't really want to have to use the deprecated method.
I have my head blow for a week with this issue.
My app keep crashing after Login Using Social Media Such as Wechat / LinkedIn.But Facebook and Google Sign in Works Fine.
I have notice my app will keep crash after confirm sign in on Wechat Apps and will enter foreground.and Getting BAD EXCESS error. I have try to remove my application open url method on AppDelegate and the app wont crash but the action for Social Media Login are not functioning. so I detect that my issue was on the specific method. after search the web I found that im using an deprecated method of ApplicationOpenUrl as reference from https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623073-application
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return true
} // this method is deprecated in iOS 9 https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623073-application
notice that the deprecated version are using annotation:Any which will cause issue if you had bridging to an Obj-c framework such as wechat.
So what I do was, I swap my code into a the new format
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let sourceApplication = options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String
let annotation = options[UIApplicationOpenURLOptionsKey.annotation]
let application = app
return true
}
Hope this help. it will became my reference in feature also. thanks StackOverflow
This is fairly typical of a signature mismatch between the method signatures automatically generated by the Swift compiler and the actual signature. It happens when you try to pass nil from Objective-C into a Swift explicitly unwrapped optional. Change the annotation parameter to be implicitly unwrapped and you should be gtg.
Swift 5 version of Muhammad Asyraf's answer:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let sourceApplication = options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String
let annotation = options[UIApplication.OpenURLOptionsKey.annotation]
return true
}