Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
user5394344 Avatar asked Nov 25 '15 23:11

user5394344


People also ask

How do I view a controller in Safari?

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.

What is Safari web view?

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.

Does SFSafariViewController share cookies?

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.


1 Answers

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.
    }
}
like image 79
Avi Avatar answered Sep 27 '22 20:09

Avi