Skip to content

HTTP Request Customization in .NET iOS

For the iOS platform for .NET MAUI, you will be required to conform to RFSURLRequestInterceptingDelegate which provides you with a callback that receives URLRequest right before requests are fired. In this example, we will utilize FaceSdkInit to intercept our requests.

If you want to intercept HTTP requests, make sure to specify the listener before startLiveness or matchFaces:

using FaceApi.iOS;
using FaceSample.iOS;
using Foundation;

[assembly: Xamarin.Forms.Dependency(typeof(FaceSdkInit))]
namespace FaceSample.iOS
{
    public class FaceSdkInit : RFSURLRequestInterceptingDelegate, IFaceSdkInit
    {
        public FaceSdkInit()
        {
        }

        public void InitFaceSdk()
        {
            RFSFaceSDK.Service.InitializeWithCompletion((bool success, NSError error) =>
            {
                RFSFaceSDK.Service.RequestInterceptingDelegate = this;
            });
        }

        public override NSUrlRequest InterceptorPrepareRequest(NSUrlRequest request)
        {
            NSString key = new NSString("Authorization");
            NSString value = new NSString("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmYWNlLXNkayI6IkhhdmUgYSBncmVhdCBkYXkhIn0.IPoW0D0LnMv_pL4U22MuIhDNGIdK34TaHhqhKBAaBEs");
            NSMutableUrlRequest interceptedRequest = request.MutableCopy() as NSMutableUrlRequest;
            NSMutableDictionary headers = interceptedRequest.Headers.MutableCopy() as NSMutableDictionary;
            headers.Add(key, value);
            interceptedRequest.Headers = headers;

            return interceptedRequest;
        }
    }
}