I am updating my Uno based app and in testing-the app tracking prompt no longer appears. I had implemented this and it works correctly in iOS 14. In iOS 14 I am able to go into the privacy settings and see that the user has selected if they want to be tracked or not. I dont see any of this in iOS 15. In my research, it appears that I need to override the onActivated method in appDelegate and put the prompt code in there. I dont have an appDelegate class in my project so I tried adding one in app.iOS but it could not find the formsApplicationdelegate the example had. Also- when I look in settings in iOS it does not show the app as having a tracking/no tracking choice. I am guessing this will show once I get the prompt to show? I did verify the setting is in my info.plist. What is the best way to do this?
Here is my current implementation:
info.plist:
<key>NSUserTrackingUsageDescription</key>
<string>The App would like to access the identifier for analytics purposes</string>
MainPage.xaml.cs:
#if __IOS__
using AppTrackingTransparency;
#endif
public sealed partial class MainPage : Page
{
public MainPage()
{
#if __IOS__
try
{
ATTrackingManager.RequestTrackingAuthorization((status) => {
if (status == ATTrackingManagerAuthorizationStatus.Authorized)
{
//do something
}
else if (status == ATTrackingManagerAuthorizationStatus.Denied)
{
//do something
}
});
}
catch (Exception e)
{
//do something
}
#endif
}
}
In my research, it appears that I need to override the onActivated method in appDelegate and put the prompt code in there. I dont have an appDelegate class in my project (...)
The App class (in App.xaml.cs) inherits from Windows.UI.Xaml.Application which in turn inherits from UIApplicationDelegate.
This means you should be able to do the following in App.xaml.cs.
#if __IOS__
public override void OnActivated(UIKit.UIApplication application)
{
// Your code.
}
#endif
Related
I am kind of new to Xamarin development. I tried to change the back button behavior with a binding command, but that didn't seem to work. This is the code for the view:
<Shell.BackButtonBehavior>
<BackButtonBehavior Command="{Binding GoBack}"/>
</Shell.BackButtonBehavior>
And this is the code for the view model:
public CreatePasswordVM()
{
_goBack = new Command(GoBackButton);
}
private ICommand _goBack;
public ICommand GoBack
{
get { return _goBack; }
}
public async void GoBackButton()
{
await Shell.Current.GoToAsync("../..");
}
When I pressed the back button, the method "GoBackButton" didn't call. I want to mention that on Android works.
Shell BackButtonBehavior command binding not working in UWP
Derive from Xamarin Form source code. BackButtonBehavior has not implemented for UWP platform, we could find Android and IOS implementation here and here. But for uwp there is not such tracker and there is not such value in the UWP ShellRenderer. For this scenario, we suggest your post new feature request in the Xamarin Forms github.
I have an application that I added a launch page to in the iOS and Android code. However when the app starts there is still quite a long delay while it fetches data. At this time there's a blank screen where I assume the app is still setting up the constructor.
I am trying to have an in-between page where that appears that loads the data. Not sure if this is the best way to do this but so far it's all that I have.
Here's the code that I have so far:
public App()
{
InitializeComponent;
MainPage = new NavigationPage(new Test.LoginPage())
{
};
}
My Test.LoginPage is a simple empty Xaml page with this C# back end:
public partial class LoginPage : ContentPage
{
public LoginPage()
{
InitializeComponent();
}
protected async override void OnAppearing()
{
await LongRunningTask();
App.MainPage = new AppShell(); // I want to start a shell app
}
}
public partial class AppShell : Shell
{
public AppShell()
{
Routing.RegisterRoute("HomeTab/QHPage", typeof(QHPage));
// etc
But the code has issues in that first of all I am not sure I am doing it correctly and secondly it says an object reference is required for App.MainPage.
Can anyone point me in the right direction and suggest how I could display this intermediate page and then display the real app pages?
Note that at some point I would also like to have a button on the login page that when clicked takes me to the app. But at this time I just want to get even the most simple version working so I am looking for some advice with that.
The easy way is to set the AppShell as MainPage. The code below works for me.
Application.Current.MainPage = new AppShell();
I have created a forms page in Xamarin and used that page as a fragment using Xamarin Forms Embedding.
var fragment = new FormsPage().CreateFragment(context);
I would like to override the OnBackPressed() control in Android version of the app.
What's the best possible way to do it as I can't override it inside the Xamarin Forms Page.
I would like to override the OnBackPressed() control in Android version of the app.
You could override OnBackButtonPressed in Xamarin.Forms, this event also be raised when the hardware back button is pressed in Android and this event is not raised on iOS.
If you want remove fragment from the stack in OnBackPressed() method, you could override this method in your Activity like this :
public override void OnBackPressed()
{
base.OnBackPressed();
if (FragmentManager.BackStackEntryCount != 0)
{
FragmentManager.PopBackStack();
}
else
{
base.OnBackPressed();
}
}
I am not sure if it is just me, or everyone else out there, but every time i debug my Xamarin app on my mobile device i get a blank screen with an actionbar. The screen is displayed for about 2 seconds before it actually loads my specified screen.
Here is my PCL App.cs file:
public class App : Application
{
public App()
{
// The root page of your application
MainPage = new LogInPage();
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
The blank screen:
and then finally, after a few seconds:
What you are seeing is the default theme of the Activity being shown while Xamarin Forms is being initialized. You will tend to see this during a cold start. One way to work around it is to add a Splash Screen to show during initialization (Xamarin Android Splash Screen doc).
I have recently updated xamarin forms to 1.5.1-pre1 so that I can use the beautiful AppCompat themes. It works and looks very nice.
I do have one problem, in my old FormsApplicationActivity I used to override the OnOptionsItemSelected method to intercept when the user was clicking on the back arrow icon and do some viewmodel cleanup. Apparently this method is not being called after using the FormsAppCompatActivity.
How can I intercept the "soft" back button press (toolbar icon not hard back button) ?
I also tried to override the Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer but i can't seem to override it :(
Does anyone have a clue how I can intercept this?
You can add the following to your custom renderer. You can either user current activity plugin or cast your context to activity.
var toolbar = CrossCurrentActivity.Current?.Activity?.FindViewById<Toolbar>(Resource.Id.toolbar);
toolbar.NavigationClick += ToolbarNavigationClick;
Have a look at the last two lines of the OnCreate() After adding them OnOptionsItemSelected was called as expected).
https://raw.githubusercontent.com/UdaraAlwis/Xamarin-Playground/master/XFNavBarBackBtnClickOverride/XFNavBarBackBtnClickOverride/XFNavBarBackBtnClickOverride.Droid/MainActivity.cs
Toolbar toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
In Xamarin.Forms there's a better way to intercept the NavigationBar back button pressed and the hardware back button pressed, which is creating your own NavigationRenderer and overriding the method OnPopViewAsync:
[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationRenderer))]
namespace YourApp.Droid
{
public class CustomNavigationRenderer : NavigationPageRenderer
{
public CustomNavigationRenderer(Context context) : base(context)
{
}
protected override async Task<bool> OnPopViewAsync(Page page, bool animated)
{
// Write your code here
}
}
}
Hope this helps