1

I need a clarification that Is it possible to open a URL in Google Chrome and Opera browsers like Safari in iOS from iOS Application.

Anyone's help will be deeply appreciated.

trejder
  • 17,148
  • 27
  • 124
  • 216
Monish Kumar
  • 2,788
  • 4
  • 38
  • 54
  • 1
    Um... Yes you can open links in any browser... Are you asking for an animation? – CodaFi Jan 19 '12 at 05:21
  • No I am not asking for animation. I want to know how to open the links in chrome and safari. – Monish Kumar Jan 19 '12 at 05:26
  • If we open a link in safari it open's into separate application in safari right? Like wise does the link opens into google chrome and Opera? – Monish Kumar Jan 19 '12 at 05:27
  • Oh, then to answer your first request. Possible dupe: http://stackoverflow.com/questions/2899699/uiwebview-open-links-in-safari – CodaFi Jan 19 '12 at 05:29
  • Hey sorry the link u provided is for safari. I want to know how to open the link in google chrome browser from our application – Monish Kumar Jan 19 '12 at 05:31
  • Not. Possible. What would you open it in? Opera and Google Chrome are against apple guidelines, and not even on the store. Do you want to send this link to a computer and have it open there? If so, that'll take some server-client action. – CodaFi Jan 19 '12 at 05:35

3 Answers3

3

For Google Chrome you can look here for some example code from google showing how to load a URL in Chrome instead of Safari https://developers.google.com/chrome/mobile/docs/ios-links

Opera supports the "ohttp" scheme so a similar thing can be done for that too.

orta
  • 4,225
  • 1
  • 26
  • 34
  • 1
    Just FYI, this class is now included in the Google Open Source framework. So if you already use that in your app, just `#import ` to get access to the `OpenInChromeController` class. – JAL Feb 05 '15 at 19:09
  • @orta Is it just me or is the link in this answer now dead? – Liron Yahdav Mar 14 '17 at 05:12
1

Here is a URLOpener class to use,

Usage,

NSString * url = @"http://www.apple.com";
NSString * userAgent = BROWSER_CHROME;
URLOpener * opener = [[URLOpener alloc] initWithURLString:url browser:userAgent];
[opener openURL];

URLOpener.h

#define BROWSER_CHROME  @"chrome"
#define BROWSER_OPERA   @"opera"

@interface URLOpener : NSObject

@property (nonatomic, retain) NSURL * url;
@property (nonatomic, retain) NSString * browser;

- (id) initWithURL:(NSURL *)u;
- (id) initWithBrowser:(NSString *)b;
- (id) initWithURL:(NSURL *)u browser:(NSString *)b;
- (id) initWithURLString:(NSString *)us browser:(NSString *)b;

- (BOOL)openURL;

@end

URLOpener.m

#import "URLOpener.h"

@implementation URLOpener

@synthesize url, browser;

- (id) initWithURL:(NSURL *)u
{
    self = [super init];
    if (self) {
        self.url = u;
    }
    return self;
}

- (id) initWithBrowser:(NSString *)b
{
    self = [super init];
    if (self) {
        self.browser = b;
    }
    return self;
}

- (id) initWithURL:(NSURL *)u browser:(NSString *)b
{
    self = [super init];
    if (self) {
        self.url = u;
        self.browser = b;
    }
    return self;
}

- (id) initWithURLString:(NSString *)us browser:(NSString *)b
{
    NSURL * u = [NSURL URLWithString:us];
    return [self initWithURL:u browser:b];
}


- (BOOL)openURL
{
    if ([BROWSER_CHROME compare:self.browser options:NSCaseInsensitiveSearch] == NSOrderedSame) {
        return [self openInChrome];
    } else  if ([BROWSER_OPERA compare:self.browser options:NSCaseInsensitiveSearch] == NSOrderedSame) {
        return [self openInOperaMini];
    }else  if ([[UIApplication sharedApplication] canOpenURL:self.url] )
    {
        return [[UIApplication sharedApplication] openURL:self.url];
    } else {
        NSLog(@"Could not open url: %@", self.url);
        return NO;
    }
}

- (BOOL) openInChrome
{
    // is chrome installed??
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome://"]])
    {
        NSString *scheme = self.url.scheme;

        // Replace the URL Scheme with the Chrome equivalent.
        NSString * chromeScheme = nil;
        if ([scheme compare:@"http" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
            chromeScheme = @"googlechrome";
        } else if ([scheme compare:@"https" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
            chromeScheme = @"googlechromes";
        }

        if (chromeScheme) {
            NSString *absoluteString = [self.url absoluteString];
            NSRange rangeForScheme = [absoluteString rangeOfString:@":"];
            NSString *urlNoScheme = [absoluteString substringFromIndex:rangeForScheme.location];
            NSString *chromeURLString = [chromeScheme stringByAppendingString:urlNoScheme];
            NSURL *chromeURL = [NSURL URLWithString:chromeURLString];
            return [[UIApplication sharedApplication] openURL:chromeURL];
        } else {
            return [[UIApplication sharedApplication] openURL:self.url];
        }

    } else {
        return [[UIApplication sharedApplication] openURL:self.url];
    }
}

- (BOOL) openInOperaMini
{
    // is opera mini installed??
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"ohttp://"]])
    {
        NSString *scheme = self.url.scheme;

        // Replace the URL Scheme with the opera equivalent.
        NSString * operaScheme = nil;
        if ([scheme compare:@"http" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
            operaScheme = @"ohttp";
        } else if ([scheme compare:@"https" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
            operaScheme = @"ohttps";
        }

        if (operaScheme) {
            NSString *absoluteString = [self.url absoluteString];
            NSRange rangeForScheme = [absoluteString rangeOfString:@":"];
            NSString *urlNoScheme = [absoluteString substringFromIndex:rangeForScheme.location];
            NSString *operaURLString = [operaScheme stringByAppendingString:urlNoScheme];
            NSURL *operaURL = [NSURL URLWithString:operaURLString];
            return [[UIApplication sharedApplication] openURL:operaURL];
        } else {
            return [[UIApplication sharedApplication] openURL:self.url];
        }

    } else {
        return [[UIApplication sharedApplication] openURL:self.url];
    }
}

- (void) dealloc {
    [url release];
    [browser release];

    [super dealloc];
}

@end
karim
  • 15,408
  • 7
  • 58
  • 96
  • Why bother checking for the scheme for Chrome? Can't you just replace http with googlecrome, and if the 's' is left there it's there already? – shim Oct 24 '14 at 06:15
0

Stumbled on this... @karim provided awesome code here. I ran into similar problem and eventually wrote Choosy - you can use it in your project to add support for many 3rd-party apps.

Alex the Ukrainian
  • 4,528
  • 3
  • 24
  • 25