更新时间:2023-12-03 10:20:58
在斯威夫特应用程序中集成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.
LinkedIn-sdk.framework
库复制到您的应用程序。确保文件拷贝如果有必要和创建文件夹引用组被选中。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>
在您AppDelegate.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 code>将包含在认证的用户信息:
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.