title: Open safari view controller from table view on iOS 9 and open in safari on iOS 8 or 7
body: <p>Hi I would like to open my website from my table view cell in the safari view controller if the user is on iOS 9 or above. If the user is on iOS 7 or 8 the website should open up in the standard safari app.</p>
  
  <p>This is the code I currently use which opens safari.</p>
  
  <pre><code>    case 3: { // Follow us section
          switch (indexPath.row) {
              case 0: { //Website
                  NSURL *url = [NSURL URLWithString:@"http://www.scanmarksapp.com"];
                  if (![[UIApplication sharedApplication] openURL:url]) {
                      NSLog(@"%@%@",@"Failed to open url:",[url description]);
                  }
              }
                  break;
  
              default:
                  break;
          }
  
      }
          break;
  </code></pre>
  
  <p>I believe this code should open the safari view controller with my website. But I am unsure how to combine both sets of code.</p>
  
  <pre><code>- (void)openLink:(NSString *)url {
  
  NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]];
  if (URL) {
      SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
      sfvc.delegate = self;
      [self presentViewController:sfvc animated:YES completion:nil];
  }
  
  #pragma Safari View Controller Delegate
  
  - (void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller {
  [controller dismissViewControllerAnimated:YES completion:nil];
  }
  </code></pre>
  
  <p>I understand this is the code used to determine what iOS version it is</p>
  
  <pre><code>if ([[[UIDevice currentDevice] systemVersion] floatValue] &lt; 9.0) {
  </code></pre>
  
  <p>I have followed your advice</p>
  
  <pre><code>- (void)openLink:(NSString *)url {
  
  NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]];
  if (URL) {
      SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
      sfvc.delegate = self;
      [self presentViewController:sfvc animated:YES completion:nil];
  } else {
      // will have a nice alert displaying soon.
  }
  
  if ([SFSafariViewController class] != nil) {
      // Use SFSafariViewController
  } else {
      NSURL *url = [NSURL URLWithString:@"http://www.scanmarksapp.com"];
      if (![[UIApplication sharedApplication] openURL:url]) {
          NSLog(@"%@%@",@"Failed to open url:",[url description]);
      }
  }
  </code></pre>
  
  <p>Then added this code under my table view cell didSelectRowAtIndexPath</p>
  
  <pre><code>        case 3: { // Follow us section
          switch (indexPath.row) {
              case 0: { //Website
                  NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]];
                  if (URL) {
                      SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
                      sfvc.delegate = self;
                      [self presentViewController:sfvc animated:YES completion:nil];
                  } else {
                      // will have a nice alert displaying soon.
                  }
  
                  if ([SFSafariViewController class] != nil) {
                      // Use SFSafariViewController
                  } else {
                      NSURL *url = [NSURL URLWithString:@"http://www.scanmarksapp.com"];
                      if (![[UIApplication sharedApplication] openURL:url]) {
                          NSLog(@"%@%@",@"Failed to open url:",[url description]);
                      }
  
                  }
              }
                  break;
  
              default:
                  break;
          }
  
      }
          break;
  </code></pre>
  
  <p>I am getting the error "Use of undeclared identifier url" on this line of code</p>
  
  <pre><code>NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]];
  </code></pre>
  
  <p>Removing url at the end of NSStringWithFormat makes the Safari view controller work. However on iOS below 9.0 e.g. 8.4 the app crashes.</p>