Xamarin SFSafariViewController DidFinish not called

1.2k Views Asked by At

I'm trying to use SFSafariViewController and this works just gr8. Browser opens and loads my site. The problem is I'm not able to catch the event when user clicks on 'Done' button. I'm implemeting ISFSafariViewControllerDelegate and exporting DidFinish method as API suggests. Here is code sample.

This method is called from ViewModel.

public async Task LaunchSafariWebViewAsync(string url, Action action)
{
    var destination = new NSUrl(url);
    var sfViewController = new SFSafariViewController(destination);

    var window = UIApplication.SharedApplication.KeyWindow;
    var controller = window.RootViewController;
    await controller.PresentViewControllerAsync(sfViewController, true);

    //Note that I'm able to invoke method manualy with this call
    SFSafariViewControllerDelegate_Extensions.DidFinish(this, sfViewController);

}

And then here I export the method that I want to be called when user closes the view.

[Foundation.Export("safariViewControllerDidFinish:")]
public void DidFinish(SFSafariViewController controller)
{
    var temp = "test";
}

As I stated in comment, when I use SFSafariViewControllerDelegate_Extensions I'm able to invoke the method manually so I guess it gets exported correctly. But it is not invoked when I click on 'Done' button.

Any suggestions why this should be?

1

There are 1 best solutions below

1
On BEST ANSWER

From your code it looks like you didn't set the delegate.

If you implement the method DidFinish in this viewmodel, add the code after you init the SFSafariViewController:

public async Task LaunchSafariWebViewAsync(string url, Action action)
{
    var destination = new NSUrl(url);
    var sfViewController = new SFSafariViewController(destination);

    sfViewController.weakDelegate=this;

    var window = UIApplication.SharedApplication.KeyWindow;
    var controller = window.RootViewController;
    await controller.PresentViewControllerAsync(sfViewController, true);
}