I started developing an app for iOS in Swift. Now I am at the part where I need to create a login system. However we need the LinkedIn information from people.
How can I use the OAuth2 API in iOS to achieve this?
I already created an app in the LinkedIn developers area, but now I am stuck. I got some advice from someone that I need to use the UIWebView
but I have no clue how this works.
Integrating LinkedIn Login in a Swift application
First, download the LinkedIn iOS SDK. I'll be using the 1.07 stable version for this example. I'll be following the integration guide here.
linkedin-sdk.framework
library to your application. Make sure "copy files if necessary" and "create groups for folder references" are selected.Project setup complete, now let's write some code!
Create a new Header file called
BridgingHeader.h
. Under Targets -> YourApp -> Build Settings -> Swift Compiler - Code Generation, addMyApp/BridgingHeader.h
to "Objective-C Bridging Header."In your
BridgingHeader.h
, add these two lines:In your AppDelegate.swift, add this code to handle the OAuth URL callback:
Swift 3:
Swift 2.x:
Now it's time to log in the user. In your view controller, say you have a "Login" button. Your
IBAction
might look like this:When logging in, the user will be asked to authenticate with your application:
If the user allows, the success block will be called, and you can get information about the authenticated user. If the login fails or the user does not allow access, then the failure block will be called, and you can alert the user on the issue that occurred.
To get information about the user we authenticated with, call a GET request on the user's profile:
The
response.data
will contain information on the authenticated user:Read the docs further for more things you can do with the API.
A sample project (with my App ID obfuscated) can be found here.
In Swift 3.0,
UIApplicationOpenURLOptionsKey
should be add forFacebook
andLinkedIn
.LinkedIn is an interesting beast, since their mobile SDKs have two flaws:
So while JAL's answer is sufficient, you may want to look into implementing LinkedIn's
authorization_code
OAuth flow in your mobile app instead of the LinkedIn SDK. This would look roughly like the following flow:Sound complicated? It's actually more straightforward than it seems. I actually wrote some demo code for this flow using Express.js & Swift. This example ultimately sends the access token to Stormpath to ultimately authenticate the user, but you can always replace it with your own code that calls the LinkedIn REST API to grab the profile's information.
Update for Edward Jiang's answer; changes is token access on server + mobile side. Source: https://developer.linkedin.com/docs/ios-sdk-auth
I know this has already been answered but I faced this issue as well and had done everything set in the Accepted answer, but for whatever reason the code still was not hitting success or failure. It turned out that with iOS 9 the following is deprecated.
The solution was to use this instead :
For example, you could do: