app_tracking_transparency 2.0.0+1 app_tracking_transparency: ^2.0.0+1 copied to clipboard
This Flutter plugin allows you to display ios tracking authorization dialogue and request permission to collect data.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:app_tracking_transparency/app_tracking_transparency.dart';
void main() {
runApp(MaterialApp(home: HomePage()));
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _authStatus = 'Unknown';
@override
void initState() {
super.initState();
// Can't show a dialog in initState, delaying initialization
WidgetsBinding.instance!.addPostFrameCallback((_) => initPlugin());
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlugin() async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
final TrackingStatus status =
await AppTrackingTransparency.trackingAuthorizationStatus;
setState(() => _authStatus = '$status');
// If the system can show an authorization request dialog
if (status == TrackingStatus.notDetermined) {
// Show a custom explainer dialog before the system dialog
if (await showCustomTrackingDialog(context)) {
// Wait for dialog popping animation
await Future.delayed(const Duration(milliseconds: 200));
// Request system's tracking authorization dialog
final TrackingStatus status =
await AppTrackingTransparency.requestTrackingAuthorization();
setState(() => _authStatus = '$status');
}
}
} on PlatformException {
setState(() => _authStatus = 'PlatformException was thrown');
}
final uuid = await AppTrackingTransparency.getAdvertisingIdentifier();
print("UUID: $uuid");
}
Future<bool> showCustomTrackingDialog(BuildContext context) async =>
await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Dear User'),
content: const Text(
'We care about your privacy and data security. We keep this app free by showing ads. '
'Can we continue to use your data to tailor ads for you?\n\nYou can change your choice anytime in the app settings. '
'Our partners will collect data and use a unique identifier on your device to show you ads.',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text("I'll decide later"),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Allow tracking'),
),
],
),
) ??
false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('App Tracking Transparency Example'),
),
body: Center(
child: Text('Tracking status: $_authStatus\n'),
),
);
}
}