gpt4 book ai didi

iOS linkedin 认证

转载 作者:可可西里 更新时间:2023-10-31 23:51:41 30 4
gpt4 key购买 nike

我开始使用 Swift 开发 iOS 应用。现在我正处于需要创建登录系统的部分。但是,我们需要人们提供的 LinkedIn 信息。

我如何在 iOS 中使用 OAuth2 API 来实现这一点?

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

最佳答案

在 Swift 应用程序中集成 LinkedIn 登录

首先,下载 LinkedIn iOS SDK .我将在此示例中使用 1.07 稳定版。我将遵循集成指南 here .

  1. 创建一个新的 Developer Application .
  2. 将您的 iOS 应用程序的捆绑标识符添加到移动设备下的 LinkedIn 应用程序。
  3. 将您的 LinkedIn 应用 ID 和 URL Scheme 添加到您应用的 Info.plist 文件中。
  4. 将指定的 LinkedIn URL 方案和 ATS URL 列入白名单。
  5. linkedin-sdk.framework 库复制到您的应用程序。确保选中“必要时复制文件”和“为文件夹引用创建组”。

项目设置完成,现在让我们写一些代码吧!

创建一个名为 BridgingHeader.h 的新头文件。在 Targets -> YourApp -> Build Settings -> Swift Compiler - Code Generation 下,将 MyApp/BridgingHeader.h 添加到“Objective-C Bridging Header”。

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

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

在您的 AppDelegate.swift 中,添加此代码以处理 OAuth URL 回调:

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
}

现在是登录用户的时候了。在您的 View Controller 中,假设您有一个“登录”按钮。您的 IBAction 可能如下所示:

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

登录时,系统会要求用户对您的应用程序进行身份验证:

LinkedIn

如果用户允许,将调用成功 block ,您可以获得有关已通过身份验证的用户的信息。如果登录失败或用户不允许访问,则会调用失败 block ,您可以就发生的问题提醒用户。

要获取有关我们通过身份验证的用户的信息,请对用户的个人资料调用 GET 请求:

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 将包含有关经过身份验证的用户的信息:

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

阅读docs进一步了解您可以使用 API 执行的更多操作。

可以找到一个示例项目(我的 App ID 被混淆了)here .

关于iOS linkedin 认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28491280/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com