我需要澄清一下,是否可以在iOS应用中在谷歌浏览器和Opera浏览器中打开URL,例如iOS中的Safari。
任何人的帮助都将深受感激。
答案 0 :(得分:3)
对于谷歌浏览器,您可以在此处查看谷歌的一些示例代码,其中显示了如何在Chrome中加载网址而不是Safari https://developers.google.com/chrome/mobile/docs/ios-links
Opera支持“ohttp”方案,因此也可以做类似的事情。
答案 1 :(得分:1)
这是一个要使用的URLOpener类,
用法,
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
答案 2 :(得分:0)
偶然发现...... @karim在这里提供了很棒的代码。我遇到了类似的问题并最终写了Choosy - 您可以在项目中使用它来添加对许多第三方应用的支持。