Flutter隐私同意管理插件flutter_aepedgeconsent的使用

Flutter隐私同意管理插件flutter_aepedgeconsent的使用

flutter_aepedgeconsent 是一个为 iOS 和 Android 设计的 Flutter 插件,用于集成 Adobe Experience Platform Edge Consent SDK。通过此插件,可以在 Flutter 应用程序中启用隐私同意管理功能。下面是详细的使用指南。

目录

  1. 前提条件
  2. 安装
  3. 测试
  4. 使用

前提条件

在安装 flutter_aepedgeconsent 之前,需要确保已经安装了以下依赖项:


安装

安装 flutter_aepedgeconsent 的步骤可以在这里找到:安装指南

注意事项

在安装完 SDK 后,别忘了在你的 ios 目录下运行 pod install 来将库链接到你的 Xcode 项目。

flutter pub add flutter_aepedgeconsent

测试

你可以通过运行以下命令来执行单元测试:

flutter test

使用

更多关于 Consent APIs 的详细信息可以查看 文档

注册扩展到 AEPCore

在初始化 SDK 时,必须在原生代码中进行(iOS 中的 AppDelegate 和 Android 中的 MainApplication 类)。

确保在启动 SDK 之前设置 SDK 包装类型为 Flutter

初始化示例

iOS

// AppDelegate.h
@import AEPCore;
@import AEPEdge;
@import AEPEdgeIdentity;
@import AEPEdgeConsent;

@implementation AppDelegate

// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [AEPMobileCore setWrapperType:AEPWrapperTypeFlutter];

    // 设置你的环境文件 ID
    NSString* ENVIRONMENT_FILE_ID = @"YOUR-APP-ID";
    
    NSArray *extensionsToRegister = @[AEPMobileEdgeIdentity.class, 
                                      AEPMobileEdge.class,                                              
                                      AEPMobileEdgeConsent.class
                                      ];

    [AEPMobileCore registerExtensions:extensionsToRegister completion:^{
        [AEPMobileCore configureWithAppId: ENVIRONMENT_FILE_ID];
    }];
    return YES;   
} 

Android

import com.adobe.marketing.mobile.MobileCore;
import com.adobe.marketing.mobile.Edge;
import com.adobe.marketing.mobile.edge.identity.Identity;
import com.adobe.marketing.mobile.edge.consent.Consent;

public class MainApplication extends FlutterApplication {
  private final String ENVIRONMENT_FILE_ID = "YOUR-APP-ID";

  @Override
  public void onCreate() {
    super.onCreate();
    ...
    MobileCore.setApplication(this);
    MobileCore.setWrapperType(WrapperType.FLUTTER);
    MobileCore.configureWithAppID(ENVIRONMENT_FILE_ID);

    MobileCore.registerExtensions(
      Arrays.asList(Consent.EXTENSION, Identity.EXTENSION, Edge.EXTENSION),
      o -> Log.d("MainApp", "Adobe Experience Platform Mobile SDK was initialized")
    );
  }
}

导入SDK

在 Dart 文件中导入 flutter_aepedgeconsent

import 'package:flutter_aepedgeconsent/flutter_aepedgeconsent.dart';

API 参考

extensionVersion

返回 Consent 扩展的 SDK 版本。

语法

static Future<String> get extensionVersion

示例

String version = await Consent.extensionVersion;

getConsents

检索存储在 Consent 扩展中的当前同意偏好。

语法

static Future<Map<String, dynamic>> get consents

示例

Map<String, dynamic> result = {};
try {
  result = await Consent.consents;
} on PlatformException {
  print("Failed to get consent info");
}

updateConsents

将现有的同意与给定的同意合并。重复的键将采用 API 中传递的值。

语法

static Future<void> update(Map<String, dynamic> consents)

示例

Map<String, dynamic> collectConsents = allowed
    ? {
        "collect": {"val": "y"}
      }
    : {
        "collect": {"val": "n"}
      };
Map<String, dynamic> currentConsents = {"consents": collectConsents};

Consent.update(currentConsents);

更多关于Flutter隐私同意管理插件flutter_aepedgeconsent的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter隐私同意管理插件flutter_aepedgeconsent的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


关于flutter_aepedgeconsent插件的使用,这是一个用于在Flutter应用中管理隐私同意的插件。以下是一个基本的使用案例,展示了如何集成和使用该插件来管理隐私同意。

1. 添加依赖

首先,在你的pubspec.yaml文件中添加flutter_aepedgeconsent的依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_aepedgeconsent: ^最新版本号  # 请替换为实际发布的最新版本号

然后运行flutter pub get来安装依赖。

2. 导入插件

在你的Flutter项目中,导入flutter_aepedgeconsent插件:

import 'package:flutter_aepedgeconsent/flutter_aepedgeconsent.dart';

3. 初始化插件

在你的应用启动时(例如在MainActivity.ktAppDelegate.swift中,以及main.dartMain函数中),初始化flutter_aepedgeconsent插件。这通常涉及设置隐私策略的配置信息。

在Dart代码中初始化

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化flutter_aepedgeconsent插件
  AEPEdgeConsent.instance.initialize(
    config: AEPEdgeConsentConfig(
      // 配置你的隐私策略URL、隐私条款版本等
      privacyPolicyUrl: "https://your-privacy-policy-url.com",
      privacyPolicyVersion: "1.0",
      // 其他配置项...
    ),
  ).then((_) {
    runApp(MyApp());
  }).catchError((error) {
    // 处理初始化错误
    print("AEP Edge Consent initialization failed: $error");
  });
}

4. 显示隐私同意对话框

在你的应用中,当需要用户同意隐私政策时,可以调用插件提供的函数来显示同意对话框。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Privacy Consent Demo'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              try {
                bool isAccepted = await AEPEdgeConsent.instance.showConsentDialog();
                if (isAccepted) {
                  // 用户同意了隐私政策
                  print("User accepted the privacy consent.");
                } else {
                  // 用户拒绝了隐私政策
                  print("User rejected the privacy consent.");
                }
              } catch (error) {
                // 处理显示对话框时的错误
                print("Failed to show consent dialog: $error");
              }
            },
            child: Text('Show Privacy Consent Dialog'),
          ),
        ),
      ),
    );
  }
}

5. 检查隐私同意状态

你可以在应用的任何位置检查用户是否已经同意了隐私政策。

void checkConsentStatus() async {
  try {
    bool isAccepted = await AEPEdgeConsent.instance.isConsentAccepted();
    if (isAccepted) {
      print("User has already accepted the privacy consent.");
    } else {
      print("User has not accepted the privacy consent.");
    }
  } catch (error) {
    // 处理检查状态时的错误
    print("Failed to check consent status: $error");
  }
}

注意事项

  • 确保你已经按照插件的文档正确配置了隐私策略和其他必要的信息。
  • 在生产环境中,务必处理所有可能的错误和异常情况,以确保应用的健壮性。
  • 根据你的应用需求,可能需要进一步自定义隐私同意对话框的内容和样式。

这个基本案例展示了如何在Flutter应用中集成和使用flutter_aepedgeconsent插件来管理隐私同意。根据你的具体需求,你可能需要调整和扩展这个基础实现。

回到顶部