Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

swift - iOS linkedin authentication

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

  1. Create a new Developer Application.
  2. Add your iOS app's Bundle Identifier to your LinkedIn App under Mobile.
  3. Add your LinkedIn app Id and URL Scheme to your app's Info.plist file.
  4. Whitelist the specified LinkedIn URL schemes and ATS URLs.
  5. Copy the 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, add MyApp/BridgingHeader.h to "Objective-C Bridging Header."

In your BridgingHeader.h, add these two lines:

#import <Foundation/Foundation.h>
#import <linkedin-sdk/LISDK.h>

In your AppDelegate.swift, add this code to handle the OAuth URL callback:

Swift 3:

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)
    }
    return true
}

Swift 2.x:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    if LISDKCallbackHandler.shouldHandleUrl(url) {
        return LISDKCallbackHandler.application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
    }
    return true
}

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:

@IBAction func doLogin(sender: AnyObject) {
    LISDKSessionManager.createSessionWithAuth([LISDK_BASIC_PROFILE_PERMISSION], state: nil, showGoToAppStoreDialog: true, successBlock: { (returnState) -> Void in
        print("success called!")
        let session = LISDKSessionManager.sharedInstance().session
        }) { (error) -> Void in
            print("Error: (error)")
    }
}

When logging in, the user will be asked to authenticate with your application:

LinkedIn

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:

let url = "https://api.linkedin.com/v1/people/~"

if LISDKSessionManager.hasValidSession() {
    LISDKAPIHelper.sharedInstance().getRequest(url, success: { (response) -> Void in
        print(response)
        }, error: { (error) -> Void in
            print(error)
    })
}

The response.data will contain information on the authenticated user:

"{
  "firstName": "Josh",
  "headline": "Senior Mobile Engineer at A+E Networks",
  ... }"

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...