app_tracking_transparency 2.0.6 app_tracking_transparency: ^2.0.6 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:app_tracking_transparency/app_tracking_transparency.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _authStatus = 'Unknown';
@override
void initState() {
super.initState();
// It is safer to call native code using addPostFrameCallback after the widget has been fully built and initialized.
// Directly calling native code from initState may result in errors due to the widget tree not being fully built at that point.
WidgetsFlutterBinding.ensureInitialized().addPostFrameCallback((_) => initPlugin());
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlugin() async {
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
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');
}
final uuid = await AppTrackingTransparency.getAdvertisingIdentifier();
print("UUID: $uuid");
}
Future<void> showCustomTrackingDialog(BuildContext context) async =>
await showDialog<void>(
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),
child: const Text('Continue'),
),
],
),
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('App Tracking Transparency Example'),
),
body: Center(
child: Text('Tracking status: $_authStatus\n'),
),
);
}
}