Open the safari view controller from the tabular view on iOS 9 and open the safari on iOS 8 or 7

Hi, I would like to open my site from a cell in the viewing table in the safari viewing controller if the user is on iOS 9 or higher. If the user is on iOS 7 or 8, the website should open in the standard Safari application.

This is the code that I am currently using that opens safari.

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; 

I believe that this code should open the safari view controller with my site. But I'm not sure how to combine both sets of 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]; } 

I understand that this is the code used to determine the version of iOS.

 if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) { 

I followed your advice

 - (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]); } } 

Then this code is added under my didSelectRowAtIndexPath table view cell

  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; 

I get the error "Use undeclared identifier" in this line of code

 NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]]; 

Removing the url at the end of NSStringWithFormat does the work of the Safari view controller. However, on iOS below 9.0, for example. 8.4 application crash.

+5
source share
1 answer

A standard and recommended approach is to test the capabilities, not the OS version. In this case, you can check for the presence of the SFSafariViewController class.

 if ([SFSafariViewController class] != nil) { // Use SFSafariViewController } else { // Open in Mobile Safari } 

change

Invalid openLink: implementation openLink:

 - (void)openLink:(NSString *)url { NSURL *URL = [NSURL URLWithString:url]; if (URL) { if ([SFSafariViewController class] != nil) { SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL]; sfvc.delegate = self; [self presentViewController:sfvc animated:YES completion:nil]; } else { if (![[UIApplication sharedApplication] openURL:url]) { NSLog(@"%@%@",@"Failed to open url:",[url description]); } } } else { // will have a nice alert displaying soon. } } 
+18
source

Source: https://habr.com/ru/post/1236860/


All Articles