简体   繁体   中英

How can I add a lock screen to my Xamarin.Forms iOS app?

I am trying to add a lock screen to my iOS app such that "DidEnterBackground" will enable the lock "OnActivated". I've seen other apps do this using a PIN number, just wondering how to add that functionality to my app.

I'm using Xamarin.Forms Xamarin Studio

I suspect there are many ways to do that, here's one suggestion.

Implement a service IDidDeactivate with an event in it, register it with the Resolver and when your Forms are initialized (App.cs) hook to that event and manipulate the navigation as needed, such as Navigation.PushModalAsync(new PinPad())

The code below is obviously trimmed down to the essence, I hope you get the idea

// this goes in the Forms project
public interface IDidDeactivate{
   event EventHandler Deactivated;
   event EventHandler Activated;
}

//this goes in the iOS project 
public class WhoDeactivated: IDidDeactivate{ 
  public event EventHandler Deactivated;
  public event EventHandler Activated;

  public void SendEvent (bool isActivated){
    if (isActivated)
       Activated(this, new EventArgs());
    else
       Dectivated(this, new EventArgs());
  }
}

// back to the Forms project:
public class SensitivePage: ContentPage{

  public SensitivePage(){
     var deactivateService = Resolver.Resolve<IDidDeactivate>();
     deactivateService.Deactivated += (s,e)=> 
              Navigation.PushModalAsync(new PinPadPage()); 
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM