iOS版LinkedIn认证 [英] iOS linkedin authentication

查看:382
本文介绍了iOS版LinkedIn认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始开发用于iOS版雨燕的应用程序。现在,我在哪里,我需要创建一个登录系统的一部分。但是我们需要的人LinkedIn的信息。

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.

我怎么可以使用的OAuth2 API在iOS中实现这一目标?

How can I use the OAuth2 API in iOS to achieve this?

我已经创建了LinkedIn开发区域的应用程序,但现在我卡住了。我从别人一些建议,我需要使用的UIWebView ,但我不知道这是如何工作的。

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.

推荐答案

在斯威夫特应用程序中集成LinkedIn登录

首先,下载 LinkedIn的iOS SDK 。我将使用1.07稳定版的这个例子。我会在这里继集成指南

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. 创建一个新的开发应用程序

  2. 添加iOS应用的捆绑标识符到您的LinkedIn移动应用程序下。

  3. 添加您的LinkedIn应用ID和URL计划,您的应用程序的Info.plist文件。

  4. 白名单中指定的URL LinkedIn计划和ATS的URL。

  5. LinkedIn-sdk.framework 库复制到您的应用程序。确保文件拷贝如果有必要和创建文件夹引用组被选中。

  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.

项目设置齐全,现在让我们写一些code!

Project setup complete, now let's write some code!

创建一个新的名为头文件 BridgingHeader.h 。在目标 - > YourApp - >构建设置 - >雨燕编译器 - Objective-C的桥接报头code代,加入 MyApp的/ BridgingHeader.h

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."

在你的 BridgingHeader.h ,添加以下两行:

In your BridgingHeader.h, add these two lines:

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

在您A​​ppDelegate.swift,添加此code来处理OAuth网址回调:

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

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
}

现在是时候登录的用户。在您的视图控制器,说你有一个登录按钮。你的 IBAction为可能是这样的:

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:

如果用户允许,成功块将被调用,您可以获取有关身份验证的用户信息。如果登录失败,或者用户不允许访问,那么失败块将被调用,您可以提醒所发生问题的用户。

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.

要获得有关我们与认证的用户信息,呼吁用户的配置文件一个GET请求:

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

response.data 将包含在认证的用户信息:

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

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

阅读文档进一步了解的事情你可以使用API​​做的。

Read the docs further for more things you can do with the API.

一个示例项目(含混淆我的应用程序ID),都可以在这里找到

A sample project (with my App ID obfuscated) can be found here.

这篇关于iOS版LinkedIn认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆