Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Unable to get password autofill effect to work

$
0
0

I am developing a Xamarin.Forms app for iOS and Android. So far i only tested on iOS since that is the client priority.

i have followed the guide here to setup the password autofill but it does not seems to work. I follow the guide till the Domain association section since from what i read it does not seems to be needed for my case as my app do not have a registration function in the website or app.

I am assuming the linking of AutofillEffect.cs and the AppleAutofillEffect.cs is not link properly.

Code for AutofillEfect.cs
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace MyProject
{
    public class AutofillEffect : RoutingEffect
    {
        public AutofillContentType Type { get; set; }

        public AutofillEffect() : base("PasswordAutofill." + nameof(AutofillEffect))
        {
        }
    }
}

Code for AppleAutofillEffect.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using Foundation;
using UIKit;

[assembly: Xamarin.Forms.ResolutionGroupName("PasswordAutofill")]
[assembly: Xamarin.Forms.ExportEffect(typeof(MyProject.iOS.AppleAutofillEffect), "AutofillEffect")]
namespace MyProject.iOS
{
    public class AppleAutofillEffect : PlatformEffect
    {
        protected override void OnAttached()
        {
            var effect = (AutofillEffect)Element.Effects
                .FirstOrDefault(e => e is AutofillEffect);
            if (effect != null
                && UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
                && Control is UITextField textField)
            {
                switch (effect.Type)
                {
                    case AutofillContentType.None:
                        textField.TextContentType = NSString.Empty;
                        break;
                    case AutofillContentType.Username:
                        textField.TextContentType = UITextContentType.Username;
                        break;
                    case AutofillContentType.Password:
                        textField.TextContentType = UITextContentType.Password;
                        break;
                }
            }
        }

        protected override void OnDetached()
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
                && Control is UITextField textField)
            {
                textField.TextContentType = NSString.Empty;
            }
        }
    }
}

Login.xaml

<StackLayout x:Name="loginForm">
         <Label Text="Username" TextColor="#0071BB" FontSize="16" FontFamily="Roboto" HorizontalTextAlignment="Center"></Label>                            
          <inputLayout:SfTextInputLayout OutlineCornerRadius="20" ContainerBackgroundColor="#000" >
                  <Entry x:Name="txtUsername" BackgroundColor="#FFF" TextColor="#FFF" FontSize="20" FontFamily="Roboto">
                           <Entry.Effects>
                                  <local1:AutofillEffect Type="Username" />
                           </Entry.Effects>
                     </Entry>
            </inputLayout:SfTextInputLayout>
           <Label Text="Password" TextColor="#0071BB" FontSize="16" FontFamily="Roboto" HorizontalTextAlignment="Center"></Label>
           <inputLayout:SfTextInputLayout OutlineCornerRadius="20"  ContainerBackgroundColor="#000">
                    <Entry x:Name="txtPassword" BackgroundColor="#FFF" TextColor="#FFF" FontSize="20" FontFamily="Roboto" IsPassword="True">
                            <Entry.Effects>
                                    <local1:AutofillEffect Type="Password" />
                            </Entry.Effects>
                     </Entry>
             </inputLayout:SfTextInputLayout>
            <buttons:SfButton Text="Login" CornerRadius="20" x:Name="btnLogin" Clicked="btnLogin_Clicked" FontFamily="Roboto-Light" FontSize="20" 
                                                          BackgroundColor="#0071BB" TextColor="#FFF" WidthRequest="150"/>
</StackLayout>

Directory Structure

  • MyProject.sln
    ---------- MyProject
    ----------------AutofillEffect .cs
    ----------------Views
    ---------------------Login.xaml
    -----------MyProject.iOS
    -----------------AppleAutofillEffect .cs

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>