CleverSDK 2.0.0

CleverSDK 2.0.0

测试已测试
Lang语言 Obj-CObjective C
许可 Apache-2.0
发布最新发布2024年1月

Clever 维护。



CleverSDK 2.0.0

CleverSDK

CleverSDK 是一个简单的 iOS 库,使 iOS 开发者能够轻松地将 Clever Instant Login 集成到他们的应用程序中。您可以在此处了解更多关于在您的应用程序中集成 Clever Instant Login 的信息:这里

安装

CleverSDK 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod "CleverSDK"

要简单地将 SDK 导入到您的代码库,请添加以下头文件

#import <CleverSDK/CleverSDK.h>

使用方法

CleverSDK 利用 通用链接 在登录流程中打开您的应用程序。要使用 CleverSDK,您首先需要配置您的应用程序以支持通用链接。具体来说,您需要配置应用程序以通过通用链接处理您的首要 Clever 重定向 URI。这意味着,如果用户在登录流程中(无论是从 Clever 门户还是从 通过 Clever 登录按钮)被导向您的重定向 URI,您的应用程序将打开并可以完成登录。

一旦您的应用程序已配置为处理主要的重定向 URI,您就可以实例化 CleverSDK

[CleverSDK startWithClientId:@"YOUR_CLIENT_ID" // Your Clever client ID
            RedirectURI:@"http://example.com" // A valid Clever redirect URI (that your app is configured to open with universal links)
            successHandler:^(NSString *code, BOOL validState) {
                // At this point your application has a code, which it should send to your backend to exchange for whatever information
                // is needed to complete the login into your application.
                // Additionally you're given the "validState" param which indicates that the CleverSDK initiated the login and that the
                // state param was validated. The Clever SDK generates and validates the state param when it initiates a login.
                // However if the login comes from the Clever Portal it will not have a state param. If your application needs extra
                // guarantees that the user who is logging in is who they say they are you can start another login in the case that 
                // validState is false. This will result in a slower and more disruptive login experience (since users will be redirected
                // back to Clever), but will provide an extra layer of security during the login flow. You can learn more about this here
                // https://dev.clever.com/docs/il-design#section-protecting-against-cross-site-request-forgery-csrf
            }
            failureHandler:^(NSString *errorMessage) {
                // If an unexpected error happened during the login you'll receive it here.
            }
];

您还需要配置应用程序在收到通用链接时调用 CleverSDK。这是通过实现 AppDelegate 的 application:continueUserActivity:restorationHandler: 方法完成的

- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
    // handleURL returns a boolean indicating if the url was handled by Clever. If your application has other universal links you
    // can check to see if Clever handled the url with this boolean, and if it's false continue to handle the url in your application.
    return [CleverSDK handleURL:[userActivity webpageURL]];
}

一旦实例化 CleverSDK,您可以通过调用 login 方法启动登录。

[CleverSDK login];

或者,如果您在登录之前知道用户的 Clever 地区 ID,您可以在开始登录时提供它,以简化他们的登录体验。

[CleverSDK loginWithDistrictId:@"CLEVER_DISTRICT_ID"];

登录按钮

要渲染 登录按钮,您可以使用提供的 CleverLoginButton 类。在 UIViewController 中,只需将以下代码添加到 viewDidLoad 方法即可

loginButton = [CleverLoginButton createLoginButton];
[self.view addSubview:loginButton];

按钮以特定的宽度和高度来进行实例化。您可以通过调用按钮上的 setWidth: 方法来更新按钮的宽度,而高度将自动调整以保持设计。

[self.loginButton setWidth:300.0];

支持旧版 iOS 即时登录

在 Clever 发布 v2.0.0 版本的 CleverSDK 即时登录之前,iOS 上的即时登录是通过自定义协议网址(如 com.clever://oauth/authorize)实现的,而不是通用链接。如果您的应用程序使用了这些自定义网址(或旧版本的 CleverSDK),SDK 的 v2.0.0 版本提供了额外的功能,您可以使用这些功能保持向后兼容。

在实例化 SDK 时,您还应该提供 LegacyIosClientId 客户端 ID(这是您在 iOS 应用中使用的特定客户端 ID)。

[CleverSDK startWithClientId:@"YOUR_CLIENT_ID" // Your Clever client ID
            LegacyIosClientId:@"YOUR_IOS_SPECIFIC_LEGACY_CLIENT_ID"
            RedirectURI:@"http://example.com" // A valid Clever redirect URI (that your app is configured to open with universal links)
            successHandler:^(NSString *code, BOOL validState) {
                // ...
            }
            failureHandler:^(NSString *errorMessage) {
                // ...
            }
];

除了上述更改之外,您还需要添加一些代码来处理 iOS 重定向 URI。这是通过实现 AppDelegate 的 application:openURL:sourceApplication:annotation: 方法完成的

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    return [CleverSDK handleURL:url];
}

您还需要将一些信息添加到您的 Info.plist 以支持自定义 URI 方案。

  1. com.clever添加到您的LSApplicationQueriesSchemes中,以便可以直接重定向到Clever应用。
  2. 将您的自定义Clever重定向URI(应类似于clever-YOUR_CLIENT_ID)添加到URL类型中,这样Clever应用就可以打开您的应用。

示例

CleverSDK项目附带一个简单的示例应用来展示SDK的用法。您可以在/Example目录中查看此示例的代码,或者您可以在Xcode中打开项目并运行Example目标。

许可

Apache 2.0