Search code examples
iosobjective-cwebviewnsurlsfsafariviewcontroller

SFSafariViewController done button not working


I have a test ViewController which is embedded in a NavigationController. In test ViewController I have a WebView button. When I click on that button it navigates to SafariViewController and it's loading given URL. In that WebView when I click on "Done" button it's calling delegate method called safariViewControllerDidFinish. But that SafariViewController is not being dismissed.

Here is my code.

- (IBAction)bannerWebviewAction:(id)sender {
    NSURL *urls = [NSURL URLWithString:@"http://www.google.com"];
    SFSafariViewController safariVC = [[SFSafariViewController alloc] initWithURL:urls];
    [self showViewController:safariVC sender:nil];
    safariVC.delegate = self;
}

#pragma SFSafariViewControllerDelegate
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
    // Done button pressed
    [controller dismissViewControllerAnimated:YES completion:nil];  
}

Done button is calling, view is not dismissing. I checked log for the self and controller, both are correct.


Solution

  • You should not say

    [self showViewController:safariVC sender:nil];
    

    You should say

    [self presentViewController:safariVC animated:YES completion:nil];
    

    SFSafariViewControllers are meant to be presented modally and will dismiss itself when done is pressed.