shieldfraud-spm

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

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

开始使用 SHIELD SDK 有四个步骤

  1. 集成 SDK

  2. 初始化 SDK

  3. 获取会话 ID

  4. 获取设备结果

  5. 发送自定义属性

集成 SDK

SHIELD SDK 与使用 Swift Package Manager 支持 iOS 12 及以上版本的应用兼容。它使用 Swift 构建。您可以使用以下格式安装它

SPM

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

XCODE

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

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

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

初始化 SDK

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

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

对于 Objective-C

@import ShieldFraud;
...
- (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 ShieldFraud
...

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 删除此标志以获取实际响应。

获取会话 ID

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

会话 ID 遵循操作系统生命周期管理,符合行业最佳实践。这意味着用户的会话在设备维护它的时间内保持活动状态,除非用户终止应用或设备内存耗尽且必须终止应用。

如果您想使用后端 API 检索设备结果,则必须在您的系统中存储会话 ID。您将需要使用此会话 ID 调用 SHIELD 后端 API。

要检索会话 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 ShieldFraud;
...
- (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 ShieldFraud;

@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 ShieldFraud
...

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_idactivity_id,以进行增强的分析。此函数接受两个参数:触发该函数的 screenName,以及用于在键值对中提供任何自定义字段的 data

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

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

对于 Objective-C

@import ShieldFraud;
...
- (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 ShieldFraud
...
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 ShieldFraud;
...
- (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 ShieldFraud
...
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)
    }
}