Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the SafariViewController in Objective-c?

Apple rejected my app today suggesting, among other things, I shouldn't force my users out to Safari to visit my web page. They suggested using SafariWebController which is new in iOS9. I went searching for guidance and only found Swift tutorials.

I had been using the following to launch Safari, associated with a button:

NSURL *url = [NSURL URLWithString:WEBSITE_REGISTRATION_URL];
[[UIApplication sharedApplication] openURL:url];

So, I'll share my simple configuration for those of us trying to keep up who are not yet on Swift.

like image 700
SteveSTL Avatar asked Dec 11 '15 21:12

SteveSTL


1 Answers

There are 5 steps:

  1. Click on your project target and then select "Build Phases". Under "Build Phases", click on the arrow by "Link Binary with Libraries" and click the plus. Search for SafariServices in the window that will open. Highlight it in the list and click "Add". enter image description here

  2. Import the library into .m file where your button resides:

    #import <SafariServices/SafariServices.h>
    
  3. Define a class extension in your .m file to conform to Safari VC protocol:

    @interface SKWelcomeViewController () <SFSafariViewControllerDelegate>
    
  4. Implement the following delegate method that will be called when the Safari View Controller needs to be dismissed dismissed:

    - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
        [self dismissViewControllerAnimated:true completion:nil];
    }
    
  5. And finally, the code inside the IBAction:

    SFSafariViewController *svc = [[SFSafariViewController alloc] initWithURL:url];
    svc.delegate = self;
    [self presentViewController:svc animated:YES completion:nil];
    

Enjoy!

like image 110
SteveSTL Avatar answered Nov 15 '22 12:11

SteveSTL