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应用程序崩溃。
我已经更新了显示我所做的事情和显示的错误的问题。 – user5394344
你没有使用你的方法来安全地使用'SFSafariViewController'。不仅如此,你的'openLink:'方法是错误的。您尝试使用'SFSafariViewController'而不检查类是否可用。 – Avi
@Avi如果您为iOS 9或更高版本开发应用程序,那么您不需要检查Api可用性权限? – Supertecnoboff