Reputation: 2848
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.
Upvotes: 1
Views: 4971
Reputation: 4568
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.
Upvotes: 0
Reputation: 15599
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];
#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
#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
Upvotes: 1
Reputation: 4295
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.
Upvotes: 3