shieldcredit-spm

shieldcredit-spm 是 ShieldFraud 的 Swift 包管理器 (www.shield.com)

ShieldCredit 帮助开发者评估移动设备上的恶意活动,并根据用户的行为返回风险情报。它收集设备的指纹、社交指标和网络信息。

使用 SHIELD SDK 有四个步骤:

  1. 集成 SDK

  2. 初始化 SDK

  3. 获取 Session ID

  4. 获取设备结果

  5. 发送自定义属性

集成 SDK

SHIELD SDK 兼容支持 iOS 12 及更高版本的应用程序,使用 Swift Package Manager 构建。它是用 Swift 构建的。您可以使用以下格式安装它:

SPM

.package(url: "https://github.com/shield-ai-technology/shieldcredit-spm.git", branch: "main"),

XCODE

File -> Add Package -> https://github.com/shield-ai-technology/shieldcredit-spm.git (Point to master branch)

注意:我们不断增强我们的欺诈库和检测能力,包括新功能、错误修复和安全更新。我们建议更新到最新的 SDK 版本,以防止快速演变的欺诈风险。

您可以参考变更日志以查看有关我们更新的更多详细信息。

初始化 SDK

SDK 初始化应在 AppDelegate 子类的 didFinishLaunchingWithOptions 中配置,以确保成功生成和处理设备指纹。 SDK 只能初始化一次,如果初始化多次,将会抛出异常。

您需要 SHIELD_SITE_IDSHIELD_SECRET_KEY 才能初始化 SDK。您可以在页面顶部找到它们。

对于 Objective-C

@import ShieldCredit;
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
  Configuration *config = [[Configuration alloc] initWithSiteId:@"SHIELD_SITE_ID" secretKey:@"SHIELD_SECRET_KEY"];
  [Shield setUpWith:config];
  return YES;
}

对于 Swift

import ShieldCredit
...

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let config = Configuration(withSiteId: "SHIELD_SITE_ID", secretKey:"SHIELD_SECRET_KEY")
    Shield.setUp(with: config)
    return true
}

配置类具有以下可选参数:

  1. deviceShieldCallback 目的: 注册回调以监听设备结果在后台中的更改。
    描述: 当 SDK 从服务器接收到更新的结果时,接收设备结果。
  2. logLevel
    目的: 将您的日志级别设置为 debuginfonone
    描述: 接收有关 SDK 在 SDK 集成期间如何处理网络请求、响应或错误信息的高级信息。 默认 日志级别为 none
  3. enableMocking 目的: 为 DeviceShieldCallback 发送模拟 JSON 响应。
    描述: 将其设置为 true 以在测试期间接收预期的 JSON 响应。默认设置为 false。删除此标志以用于 production,以获取实际响应。

获取 Session ID

Session ID 是用户应用会话的唯一标识符,并充当检索该会话设备结果时的参考点。

Session ID 遵循 OS 生命周期管理,符合行业最佳实践。这意味着,只要设备保持会话,用户的会话就会处于活动状态,除非用户终止应用或设备内存不足,并且必须杀死该应用。

如果您想使用后端 API 检索设备结果,重要的是将 Session ID 存储在您的系统中。您需要使用此 Session ID 调用 SHIELD 后端 API。

要检索 Session ID,请调用

对于 Objective-C

NSString *sessionId = [[Shield shared]sessionId];

对于 Swift

let sessionId = Shield.shared().sessionId

获取设备结果

SHIELD 为您提供可操作的设备情报,您可以从 SDK 通过优化的监听器或自定义拉取方法来检索。或者,您也可以通过后端 API 检索结果。

警告:为避免崩溃和意外行为,应用程序应仅使用 SDK 中经过正式文档记录的方法和类部分。

在任何时间点只能生效一种获取设备结果的方法(优化的监听器或自定义拉取)

通过优化的监听器检索设备结果

SHIELD 建议使用优化的监听器方法来减少 API 调用次数。

我们的 SDK 将在 SDK 初始化时捕获初始设备指纹,并且仅当设备指纹在一个会话中发生变化时才返回另一组设备情报。这确保了对您的生态系统的真正优化端到端保护。

如果您希望在会话期间设备属性发生更改时收到通知(例如,用户在启动页面后立即激活恶意工具),则可以注册回调。

在初始化期间添加一个额外的参数以注册回调。

对于 Objective-C

@import ShieldCredit;
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
  Configuration *config = [[Configuration alloc] initWithSiteId:@"SHIELD_SITE_ID" secretKey:@"SHIELD_SECRET_KEY"];
  config.deviceShieldCallback = [[ShieldCallback alloc] init]; // callback for to get updated result real time
  [Shield setUpWith:config];
  return YES;
}

...
@import ShieldCredit;

@interface ShieldCallback : NSObject<DeviceShieldCallback>
@end

@implementation ShieldCallback
- (void)didErrorWithError:(NSError *)error
{
  //Something went wrong, log the exception
}
- (void)didSuccessWithResult:(NSDictionary<NSString *,id> *)result
{
    //do something with device result
}
@end

对于 Swift

import ShieldCredit
...

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let config = Configuration(withSiteId: "SHIELD_SITE_ID", secretKey:"SHIELD_SECRET_KEY")
    config.deviceShieldCallback = ShieldCallback()  // callback for to get updated result real time
    Shield.setUp(with: config)
    return true
}

...
class ShieldCallback: DeviceShieldCallback {

    func didError(error: NSError) {
      //Something went wrong, log the exception
    }

    func didSuccess(result: [String: Any]) {
      //do something with device result
    }
}

单击此处获取设备结果响应的示例。

通过自定义拉取检索设备结果

您可以在特定的用户检查点或活动(例如帐户注册、登录或结帐)检索设备结果。 这是为了确保有足够的时间来生成设备指纹。

要通过自定义拉取检索设备结果,请调用

对于 Objective-C

[[Shield shared] setDeviceResultStateListener:^{ // check whether device result assessment is complete
    NSDictionary<NSString *, id> *result = [[Shield shared] getLatestDeviceResult];
    if (result != NULL) {
      //do something with the result
    }

    NSError *error = [[Shield shared] getErrorResponse];
    if (error != NULL) {
      // log error
    }
}];

对于 Swift

Shield.shared().setDeviceResultStateListener {  // check whether device result assessment is complete
    if let deviceResult = Shield.shared().getLatestDeviceResult() {
        //do something with the result
    }

    if let error = Shield.shared().getErrorResponse() {
        // log error
    }
}

setDeviceResultStateListener(isDeviceStateReady: (() -> Void)?) 检查 DeviceResult 是否已准备好检索。 getLatestDeviceResult() 检索最新的 DeviceResult。如果在评估完成之前调用此函数,则可能会返回 null。 getErrorResponse() 可以为不成功的 DeviceResult 检索进行故障排除。

单击此处获取设备结果响应的示例。

警告:在任何时间点只能生效一种获取设备结果的方法(优化的监听器或自定义拉取)。

如果设备结果检索不成功,getLatestDeviceResult 可能会返回 null。

发送自定义属性

使用 sendAttributes 函数发送基于事件的属性,例如 user_id 或 activity_id,以进行增强的分析。此函数接受两个参数:触发该函数的 screenName 和数据,以在键值对中提供任何自定义字段。

发送带回调的自定义属性

如果您希望在 SHIELD 成功收到自定义属性时收到通知,则可以注册回调。

对于 Objective-C

@import ShieldCredit;
...
- (void) sendAttributes
{
      NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
                          @"value_1",@"key_1",  // add keys and values as described in custom attributes table in the Dictionary
                          @"value_2",@"key_2", 
                          nil];
    [[Shield shared] setDeviceResultStateListener:^{ // check whether device fingerprinting is completed
        [[Shield shared] sendAttributesWithScreenName:@"Login" data: attributes :^(BOOL status, NSError * error) {
            if (error != nil) {
                NSLog(@"%@", error.description);
            } else {
                NSLog(@"%@", status ? @"true" : @"false");
                // true if collection is successful, false if something with insertion to database
            }
        }];
    }];
}

对于 Swift

import ShieldCredit
...
func sendAttributes() {
    let attributes = ["key_1": "value_1", // add keys and values as described in custom attributes table in the Dictionary
                      "key_2": "value_2"]
    Shield.shared().setDeviceResultStateListener { // check whether device fingerprinting is completed
        Shield.shared().sendAttributes(withScreenName: "login", data: attributes) { (status, error) in
            if let error = error {
                print(error.localizedDescription)
            } else {
                print(status)
            }
        }
    }
}

发送不带回调的自定义属性

该任务将在后台线程中运行,无需回调。

对于 Objective-C

@import ShieldCredit;
...
- (void) sendAttributes

{
      NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
                          @"value_1",@"key_1",  // add keys and values as described in custom attributes table in the Dictionary
                          @"value_2",@"key_2", 
                          nil];
    [[Shield shared] setDeviceResultStateListener:^{ // check whether device fingerprinting is completed
        [[Shield shared] sendAttributesWithScreenName:@"Login", data: attributes]];
    }];
}

对于 Swift

import ShieldCredit
...
func sendAttributes() {
    let attributes = ["key_1": "value_1", // add keys and values as described in custom attributes table in the Dictionary
                      "key_2": "value_2"]
    Shield.shared().setDeviceResultStateListener { // check whether device fingerprinting is completed
        Shield.shared().sendAttributes(withScreenName: "Login", data: attributes)
    }
}