Flutter广告管理插件sourcepoint_unified_cmp_flutter_webview_extension的使用
Flutter广告管理插件sourcepoint_unified_cmp_flutter_webview_extension的使用
本插件用于将Sourcepoint统一CMP集成到flutter_webview_plugin
中。此包提供了一个扩展方法preloadConsent()
给WebViewController
,以便在WebView中注入从Flutter端获取的同意信息。
贡献项目
请参阅贡献指南以了解详细信息。
示例用法
为了使用此扩展,请确保在项目中实现sourcepoint_unified_cmp
。有关更多信息,请参阅sourcepoint_unified_cmp包。
示例代码
为了运行示例,请查看示例应用,该示例应用可以通过以下命令运行(Android):
melos run run:example:flutter_webview -- -d sdk
或(iOS):
melos run run:example:flutter_webview -- -d IPhone
以下是实现的关键部分:
导入必要的包
import 'package:sourcepoint_unified_cmp/sourcepoint_unified_cmp.dart';
import 'package:sourcepoint_unified_cmp_flutter_webview_extension/sourcepoint_unified_cmp_flutter_webview_extension.dart';
import 'package:webview_flutter/webview_flutter.dart';
初始化并配置Sourcepoint控制器
final config = SPConfig(
accountId: 22,
propertyId: 31007,
propertyName: 'sdks-auth-consent-test-page',
pmId: '122058',
campaigns: [CampaignType.gdpr],
);
_controller = SourcepointController(config: config)
..setEventDelegate(
SourcepointEventDelegate(
onConsentReady: (SPConsent consent) {
debugPrint('DELEGATE onConsentReady: Consent string: '
'${consent.gdpr?.euconsent}');
},
onSpFinished: (SPConsent consent) {
debugPrint(
'DELEGATE SpFinished: Consent string: ${consent.gdpr?.euconsent}',
);
},
),
);
加载网站并在页面加载完成后注入同意信息
_webViewController
..setNavigationDelegate(
NavigationDelegate(
onPageFinished: (String url) {
_webViewController.preloadConsent(consent: consent!);
},
),
)
..loadRequest(
Uri.parse(
'https://sourcepointusa.github.io/sdks-auth-consent-test-page/?_sp_version=4.9.0&_sp_pass_consent=true',
),
);
return WebViewWidget(controller: _webViewController);
重要提示:网页需要实现Sourcepoint统一CMP脚本,并且必须使用?_sp_version=4.9.0&_sp_pass_consent=true
来指示JS SDK监听外部同意信息。
结果
完整示例代码
import 'package:flutter/material.dart';
import 'package:sourcepoint_unified_cmp/sourcepoint_unified_cmp.dart';
import 'package:sourcepoint_unified_cmp_flutter_webview_extension/sourcepoint_unified_cmp_flutter_webview_extension.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const SourcepointUnifiedCMPBuilderExample(),
);
}
}
class SourcepointUnifiedCMPBuilderExample extends StatefulWidget {
const SourcepointUnifiedCMPBuilderExample({super.key});
[@override](/user/override)
State<SourcepointUnifiedCMPBuilderExample> createState() => _SourcepointUnifiedCMPBuilderExampleState();
}
class _SourcepointUnifiedCMPBuilderExampleState extends State<SourcepointUnifiedCMPBuilderExample> {
late final SourcepointController _controller;
late final WebViewController _webViewController;
[@override](/user/override)
void initState() {
super.initState();
final config = SPConfig(
accountId: 22,
propertyId: 31007,
propertyName: 'sdks-auth-consent-test-page',
pmId: '122058',
campaigns: [CampaignType.gdpr],
);
_controller = SourcepointController(config: config)
..setEventDelegate(
SourcepointEventDelegate(
onConsentReady: (SPConsent consent) {
debugPrint('DELEGATE onConsentReady: Consent string: '
'${consent.gdpr?.euconsent}');
},
onSpFinished: (SPConsent consent) {
debugPrint(
'DELEGATE SpFinished: Consent string: ${consent.gdpr?.euconsent}',
);
},
),
);
_webViewController = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(Colors.white);
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('webview sample'),
),
body: Center(
child: SourcepointUnifiedCMPBuilder(
controller: _controller,
builder: (BuildContext context, AsyncSnapshot<SPConsent> snapshot) {
List<Widget> children;
if (snapshot.hasData) {
final consent = snapshot.data;
debugPrint('用户同意信息已加载:');
debugPrint(' 授权:${consent?.gdpr?.grants}');
debugPrint('EU同意信息:${consent?.gdpr?.euconsent}');
_webViewController
..setNavigationDelegate(
NavigationDelegate(
onPageFinished: (String url) {
_webViewController.preloadConsent(consent: consent!);
},
),
)
..loadRequest(
Uri.parse(
'https://sourcepointusa.github.io/sdks-auth-consent-test-page/?_sp_version=4.9.0&_sp_pass_consent=true',
),
);
return WebViewWidget(controller: _webViewController);
} else if (snapshot.hasError) {
children = [
const Icon(
Icons.error_outline,
color: Colors.red,
size: 60,
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Text('错误:${snapshot.error}'),
),
];
} else {
children = const [
SizedBox(
width: 60,
height: 60,
child: CircularProgressIndicator(),
),
Padding(
padding: EdgeInsets.only(top: 16),
child: Text('等待同意...'),
),
];
}
return SingleChildScrollView(
child: Column(
children: children,
),
);
},
),
),
);
}
}
更多关于Flutter广告管理插件sourcepoint_unified_cmp_flutter_webview_extension的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter广告管理插件sourcepoint_unified_cmp_flutter_webview_extension的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中集成和使用sourcepoint_unified_cmp_flutter_webview_extension
插件的示例代码。这个插件允许你在Flutter应用中实现广告管理功能,特别是与Sourcepoint的CMP(Consent Management Platform)集成。
1. 添加依赖
首先,你需要在pubspec.yaml
文件中添加sourcepoint_unified_cmp_flutter_webview_extension
依赖:
dependencies:
flutter:
sdk: flutter
sourcepoint_unified_cmp_flutter_webview_extension: ^最新版本号 # 请替换为实际最新版本号
然后运行flutter pub get
来安装依赖。
2. 配置iOS和Android
根据插件的官方文档,你可能需要在iOS和Android项目中进行一些配置,例如添加必要的权限和设置。这里不详细展开,因为具体步骤可能会随着插件版本更新而变化。请参考插件的官方文档获取最新配置指南。
3. 使用插件
下面是一个简单的示例,展示如何在Flutter中使用这个插件来显示CMP界面。
import 'package:flutter/material.dart';
import 'package:sourcepoint_unified_cmp_flutter_webview_extension/sourcepoint_unified_cmp_flutter_webview_extension.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Sourcepoint CMP Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
// 初始化CMP配置(示例配置,请根据实际情况调整)
final cmpConfig = CmpConfig(
cmpId: '你的CMP ID',
endpoint: '你的CMP Endpoint',
gdprApplies: true, // 是否适用GDPR
ccpaApplies: true, // 是否适用CCPA
// 其他配置参数...
);
// 显示CMP界面
try {
await SourcepointUnifiedCmpFlutterWebviewExtension.showCmp(cmpConfig);
// 用户同意后可能会触发的事件
print('CMP界面已显示');
} catch (e) {
print('显示CMP界面失败: $e');
}
},
child: Text('显示CMP'),
),
),
),
);
}
}
class CmpConfig {
final String cmpId;
final String endpoint;
final bool gdprApplies;
final bool ccpaApplies;
// 其他可能的配置参数
CmpConfig({
required this.cmpId,
required this.endpoint,
required this.gdprApplies,
required this.ccpaApplies,
// 初始化其他参数
});
}
注意事项
- CMP ID和Endpoint:确保你使用的是Sourcepoint提供的正确的CMP ID和Endpoint。
- 权限和配置:确保在iOS和Android项目中正确配置了所有必要的权限和设置。
- 用户隐私:处理用户隐私数据时,请确保遵守所有相关的法律法规,如GDPR和CCPA。
这个示例代码提供了一个基本的框架,你可以根据需要进行扩展和自定义。请参考官方文档获取更多详细信息和高级用法。