LinkedIn Integration in Swift 3.0

 

Social media is everywhere and integrating any social media platform upgrades your application in every way. LinkedIn is one such platform widely used & client-demanding both at the same time.

Integrating Sign in from LinkedIn into your app is not a difficult process. Just follow the simple steps so that you can have an easy access to your linkedIn account.

Your xcode projects needs to be added to the linkedIn account in order to use LinkedIn sdk in your xcode project.

Add app to your LinkedIn account:

  1.  Open your browser and search for link https://developer.linkedin.com/
  2.  Now click on My Apps from the tab.
  3.  Sign in with your LinkedIn existing account or create a new LinkedIn account.
  4.  Under My Apps section, you’ll find all those apps which are linked to your account. You can review settings for your existing apps or you can Create a new application just on a click of Create Application button.Screen Shot 2017-02-10 at 3.22.05 PM
  5.  Here you’ll find a form where you have to fill all the details related to your app including App name, App logo etc.
  6.  After filling all the mandatory details, click on submit button.
  7.  Now under the Authentication section of your newly created App, you’ll find your app’s client ID and client Secret keys which you need to add to your xcode project which we’ll discuss later.ScreenShot2017-02-10at3.36.31PM
  8.  If you wan’t to fetch email address from the user’s account, make sure you have checked the r_emailaddress option. If it is unchecked, your app will not be able to get the user’s email address.
  9.  Also add a valid Authorized URL for redirection under the following fields:ScreenShot2017-02-10at4.03.10PM
  10. Now goto Mobile section and in iOS Settings add your Bundle identifier and a url suffix and also note down the Application Id given at this page.9qRhf

Finally click on Update button to update all the settings. This completes the process of adding your app to your LinkedIn account.

Creating a new xcode project

  1.  Create your xcode project.
  2.  Create a podfile of your project inside your project’s directory using terminal commands:
    1. Open a terminal window, and $ cd into your project directory.
    2. Create a Podfile. This can be done by running $ pod init command.
    3. Open your Podfile from finder.
  3.  Add ‘LinkedinSwift’ and SwiftyJSON pods just like you are seeing in an image below:Screen Shot 2017-02-10 at 5.25.46 PM
  4.  Save the podfile
  5.  Now install the podfile:
    • Open a terminal window, and $ cd into your project directory.
    • Install a Podfile. This can be done by running $ pod install command.
  6.  Close your project and go to your project directory folder where you’ll find your project file with .xcworkspace extension. Open <your project>.xcworkspace file.
  7. Now create a bridging-header file and add the following code before #endif.
    #import <LinkedinSwift/LSHeader.h>
  8.  Find your view controller file and at the top add statement import LinkedInSwift.
  9.  If no errors occurs, you have successfully installed all the required pods. Else your linkedInSwift pod might be missing in your project directory or might be misplaced from the required location.
  10.  Now open your info.plist file as a source code to add LinkedIn Application ID. For this copy the code below and add to your info.plist file at any place.
    <key>LIAppId</key>
        <string><your App ID></string>
        
        <key>CFBundleURLTypes</key>
        <array>
            <dict>
                <key>CFBundleURLSchemes</key>
                <array>
                    <string>li<your App ID></string>
                </array>
            </dict>
        </array>
        <key>LSApplicationQueriesSchemes</key>
        <array>
            <string>linkedin</string>
            <string>linkedin-sdk2</string>
            <string>linkedin-sdk</string>
        </array>
  11.  Make sure you have added App Transport key to enable web services in your App. If it is not present, add the following key to your info.plist file:
    <key>NSAppTransportSecurity</key>
        <dict>
            <key>NSExceptionDomains</key>
            <dict>
                <key>linkedin.com</key>
                <dict>
                    <key>NSExceptionAllowsInsecureHTTPLoads</key>
                    <true/>
                    <key>NSIncludesSubdomains</key>
                    <true/>
                    <key>NSExceptionRequiresForwardSecrecy</key>
                    <false/>
                </dict>
            </dict>
        </dict>

Now open your view controller file from where you need to authenticate with LinkedIn. Under your view controller class, define a variable of type LinkedInSwiftHelper and pass all the requred parameters given below:

private let linkedinHelper = LinkedinSwiftHelper(configuration: LinkedinSwiftConfiguration(clientId: "<your client ID>", clientSecret: "<your client Secret>", state: "DLKDJF46ikMMZADfdfds", permissions: ["r_basicprofile", "r_emailaddress"], redirectUrl: "<your redirection URL>"))
  • client ID and client secret key can be found in your app console.
  • Mention only those permissions which you have check marked under Default Application Permissions section in your app console.
  • your redirection URL would be replaced by the authorization URL you have added in your app console.

Now call authorizeSuccess() method of LinkedInSwiftHelper class in order to get the access token which will be useful for fetching profile info from LinkedIn server as defined below:

linkedinHelper.authorizeSuccess({ (token) in

            print(token)
            //This token is useful for fetching profile info from LinkedIn server
            }, error: { (error) in

            print(error.localizedDescription)
            //show respective error
        }) {
            //show sign in cancelled event
        }

If you receive an access token, only then you can request LinkedIn server to send user profile info by calling requestUrl() method of LinkedInSwiftHelper class along with the following parameters:

linkedinHelper.requestURL("https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,picture-url,picture-urls::(original),positions,date-of-birth,phone-numbers,location)?format=json", requestType: LinkedinSwiftRequestGet, success: { (response) -> Void in
            
            print(response)
            //parse this response which is in the JSON format
        }) {(error) -> Void in
            
            print(error.localizedDescription)
            //handle the error
           }

Mention only those information related parameters in URL which you want from the LinkedIn server. Also mention the response format like JSON, xml.

Now you have to add following method in your `AppDelegate.swift` file:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
		
    if LinkedinSwiftHelper.shouldHandle(url) {
        return LinkedinSwiftHelper.application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
    }
    
    return true
}

 

If you want the number of connections from the users, add “num-connections” key to the request url.

Now you have all the information related to the user. Use it as per your own requirement.

Thank You for reading us!

Leave a Reply