Search code examples
iosobjective-ciphonesafarisfsafariviewcontroller

Open safari view controller from table view on iOS 9 and open in safari on iOS 8 or 7


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.


Solution

  • 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.
        }
    }