SFSafari "Done" Button throwing error in Xamarin forms

32 Views Asked by At

IF I click on the "Done" button twice on the SafariViewView ... this code throws a "InvalidOperationException" exception...

The error says "Malformed callback URL." Not sure why ?

public class NativeBrowser : INativeBrowser
{
    UIViewController _rootViewController;
    SFSafariViewController _safari;

    Task<string> INativeBrowser.LaunchBrowserAsync(string url)
    {
        var tcs = new TaskCompletionSource<string>();
        try
        {
            StaticAuthResources.AuthCallbackHandler = async (response) =>
            {
                Device.BeginInvokeOnMainThread(async () =>
                {
                    await _safari.DismissViewControllerAsync(true);
                });
                tcs.SetResult(response);
            };

            Device.BeginInvokeOnMainThread(()=>
            {
                _rootViewController = UIApplication.SharedApplication?.KeyWindow?.RootViewController;
                _safari = new SFSafariViewController(new NSUrl(url));
                _safari.WeakDelegate = new SFSafariViewControllerDelegateToCallback();
                if (_rootViewController == null)
                {
                    tcs.SetResult(null);
                }
                else
                {
                    _rootViewController?.PresentViewController(_safari, true, null);
                }
            });
        }
        catch (Exception ex)
        {
            tcs.SetException(ex);
        }
        return tcs.Task;
    }
   
}

The DELEGATE BELOW HANDLES THE "DONE" button click

public class SFSafariViewControllerDelegateToCallback : SFSafariViewControllerDelegate
{
    [Export("safariViewControllerDidFinish:")]
    public override void DidFinish(SFSafariViewController controller)
    {
        try
        {
            //DONE BUTTON CLICKED EVENT
            if (StaticAuthResources.AuthCallbackHandler != null)
            {
                StaticAuthResources.AuthCallbackHandler("Done");
            }
        }
        catch (Exception ex)
        {

        }
        
    }
}

How do I handle the "Done" button click to relaunch the browser with the same URl ?

0

There are 0 best solutions below