About App Tracking Transparency in Flutter and implementation
Hello Guys, Recently Apple has announced that starting with iOS 14.5, iPad 14.5, and tvOS 14.5, you’ll be required to ask users for their permission to track them across apps and websites owned by other companies. So what it is all about and how to implement it in Flutter. Let’s see…..
So from now on if your app collects data about end-users and shares it with other companies for purposes of tracking across apps and websites you need to ask for authorization from end-users.
When it comes to implementation in Flutter you can use a package available on pub.dev i.e. app_tracking_transparency.
What is IDFA?
It is a unique identifier for mobile devices which is used to target and measure effectiveness of advertsing on a user at app level and from IOS 14.5 in order to access it app must ask explicit permission from the user as shown in the image above.
How to implement it in Flutter Apps?
Just open file Info.plist from ios>Runner and include:
<key>NSUserTrackingUsageDescription</key>
<string>This identifier will be used to deliver new offers for you.</string>
After this, last step left is to show user dialog for permission and get ID when user accept the permission.
String _authStatus = 'Unknown';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) {
final TrackingStatus status =
await AppTrackingTransparency.requestTrackingAuthorization();
setState(() => _authStatus = '$status');
}
} on PlatformException {
setState(() => _authStatus = 'PlatformException was thrown');
}
final uuid = await AppTrackingTransparency.getAdvertisingIdentifier();
print("UUID: $uuid");
}
The above code promt a permission dialog and once user allow Tracking you will get uuid i.e idfa(explained above).
Lastly,
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => initPlugin());
}
That all done. Now your app is ready for deployment.
Happy Coding.