I'm trying to add Requesting Tracking Authorization to an older app developed using Xamarin.iOS (native, not forms).
I believe that I've done everything correctly, but whenever the app makes a call to ATTrackingManager.RequestTrackingAuthorization(...), the app crashes out. Crash log shows a crash in libsystem_kernel.dylib:
Extract from crash log:
Incident Identifier: 232ea144-6daf-49fd-a667-fe2b0b793977
CrashReporter Key: 900B8799-DDED-4E90-826D-58841C364052
Hardware Model: iPhone10,6
Process: ***** [397]
Path: /private/var/containers/Bundle/Application/*****
Identifier: *******
Version: 4.2.0 (529)
Code Type: arm64
Parent Process: [1]
Date/Time: 2021-06-24T09:15:46.999Z
Launch Time: 2021-06-24T09:15:36Z
OS Version: iPhone OS 14.6 (18F72)
Report Version: 104
Exception Type: SIGABRT
Exception Codes: #0 at 0x1b37ec154
Crashed Thread: 4
Thread 4 Crashed:
0 libsystem_kernel.dylib 0x00000001b37ec154 0x1b37c6000 + 155988
1 libsystem_kernel.dylib 0x00000001b37f095c 0x1b37c6000 + 174428
2 TCC 0x00000001c84b1848 0x1c84af000 + 10312
3 TCC 0x00000001c84b21c0 0x1c84af000 + 12736
4 TCC 0x00000001c84b626c 0x1c84af000 + 29292
5 libxpc.dylib 0x00000001cfea54b8 0x1cfe8d000 + 99512
6 libxpc.dylib 0x00000001cfe99bb0 0x1cfe8d000 + 52144
7 libdispatch.dylib 0x00000001876f3318 0x187692000 + 398104
8 libdispatch.dylib 0x00000001876c88c8 0x187692000 + 223432
9 libdispatch.dylib 0x00000001876d8f74 0x187692000 + 290676
10 libsystem_pthread.dylib 0x00000001cfe795f4 0x1cfe6e000 + 46580
11 libsystem_pthread.dylib 0x00000001cfe7c86c 0x1cfe6e000 + 59500
I've added the NSUSerTrackingUsageDescription key and string to info.plist. I'm debugging to an iPhoneX with iOS 14.6 Privacy->Tracking->Allow Apps to Request to Track is switched ON. Visual Studio 16.10.2 (on Windows) Build MAC has MacOS 11.4 and XCode 12.5.1.
My code is:
public static void RequestTracking(UIViewController vc)
{
if (GlobalItems.FlagHasRequestedTracking)
{
return;
}
#if DEBUG
Console.WriteLine("iOS version: {0}", UIDevice.CurrentDevice.SystemVersion);
#endif
if (!UIDevice.CurrentDevice.CheckSystemVersion(14, 5))
{
return;
}
try
{
//Facebook.CoreKit.Settings.AdvertiserTrackingEnabled = false;
var status = AppTrackingTransparency.ATTrackingManager.TrackingAuthorizationStatus;
#if DEBUG
Console.WriteLine("TrackingAuthorizationStatus: ==> {0}", status);
#endif
if (status == AppTrackingTransparency.ATTrackingManagerAuthorizationStatus.NotDetermined)
{
AppTrackingTransparency.ATTrackingManager.RequestTrackingAuthorization((_status) =>
{
#if DEBUG
Console.WriteLine("RequestTrackingAuthorization: ==> {0}", _status);
#endif
GlobalItems.FlagHasRequestedTracking = true;
if (_status == AppTrackingTransparency.ATTrackingManagerAuthorizationStatus.Authorized)
{
Facebook.CoreKit.Settings.AdvertiserTrackingEnabled = true;
}
});
}
else if (status == AppTrackingTransparency.ATTrackingManagerAuthorizationStatus.Authorized)
{
Facebook.CoreKit.Settings.AdvertiserTrackingEnabled = true;
}
}
catch (Exception e)
{
#if DEBUG
Console.WriteLine(e.ToString());
#endif
}
}
AppTrackingTransparency.ATTrackingManager.TrackingAuthorizationStatus is always reading as NotDetermined. So it tries to request tracking authorization.
But, the moment it makes that call, the app crashes. It doesn't even trigger an exception that can be caught - the app just crashes out instantly. The popup does not display and the execution does not reach the callback code.
I've tried moving this code block to various different parts of the app. Initially I had it in the AppDelegate.OnActivated() function. But then moved it to the ViewDidAppear() function of a view controller. Then I also tried delaying the call by 5 seconds - and also ensuring that the call is done from the main (UI) thread.
I also tried the Async version of the call: ATTrackingManager.RequestTrackingAuthorizationAsync but that has the same outcome: instant crash.
To be sure that it isn't a problem with the actual iphone itself, I also ran the code on an iPhone12 simulator with iOS 14.5 -- again it crashes when calling RequestTrackingAuthorization(...).
Something is going wrong but I just do not know what. I've searched for similar reports but didn't find anything - so I guess I'm doing something wrong.
Any hints welcome!
I don't really understand why, but the problem was with the Info.plist file. Although it had the required key:
<key>NSUserTrackingUsageDescription</key>
<string>Placeholder text...</string>
... seems like this was being ignored. Once I've edited the string in Visual Studio, it all started working again.
Mysterious crash when requesting tracking authorization == Check/update the "NSUserTrackingUsageDescription" string in the Info.plist file.