2015-11-25 48 views
5

嗨如果用户使用iOS 9或更高版本,我想从Safari视图控制器中的表格视图单元打开我的网站。如果用户使用iOS 7或8,则该网站应该在标准的Safari浏览器应用程序中打开。从iOS 9上的表格视图打开Safari浏览器视图控制器,并在iOS 8或7上Safari浏览器中打开

这是我目前使用的打开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; 

我相信这段代码应该打开我的网站的Safari浏览器视图控制器。但我不确定如何组合两组代码。

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

我理解,这是用来确定什么的iOS版本,它是代码

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

我按照你的建议

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

然后在我的表视图单元格didSelectRowAtIndexPath方法添加该代码

 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; 

我收到错误“未声明的标识符网址的使用”在这行代码

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

在NSStringWithFormat年底拆除的网址,使Safari浏览器视图控制器的工作。但是,在iOS 9.0以下,例如8.4应用程序崩溃。

回答

18

标准和推荐的方法是检查功能,而不是操作系统版本。在这种情况下,您可以检查SFSafariViewController类的存在。

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

编辑

你实现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. 
    } 
} 
+0

我已经更新了显示我所做的事情和显示的错误的问题。 – user5394344

+0

你没有使用你的方法来安全地使用'SFSafariViewController'。不仅如此,你的'openLink:'方法是错误的。您尝试使用'SFSafariViewController'而不检查类是否可用。 – Avi

+0

@Avi如果您为iOS 9或更高版本开发应用程序,那么您不需要检查Api可用性权限? – Supertecnoboff

相关问题