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.
This is the code I currently use which 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 this code should open the safari view controller with my website. But I am unsure 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 this is the code used to determine what iOS version it is
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
I have 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 added this code under my table view cell 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;
I am getting the error "Use of undeclared identifier url" on this line of code
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]];
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.
Opening Content in Safari View Controller Run the app and tap the bottom button, "Open with safari view controller", to see the content now being displayed inside a SFSafariViewController instance. We've now let the user stay inside of our app and they have all the advantages of Safari.
Safari View Controller's job is to make it fast, easy and enjoyable for users to tap on a link within your app, view a web page and press done to go right back to your app. Safari View Controller eliminates distractions. The URL field that you see up there, it's read only. You can't change it.
Cookies. In iOS9, cookies between the Safari system browser and the SFSafariViewController are only shared if they are persistent. Session cookies are not shared and can result in different authentication sessions between the system browser and the Safari view controller.
The standard and recommended approach is to check for capability, not OS version. In this instance, you can check for the existence of the SFSafariViewController class.
if ([SFSafariViewController class] != nil) {
// Use SFSafariViewController
} else {
// Open in Mobile Safari
}
edit
Your implementation of openLink:
is wrong.
- (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.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With