这是我的代码,
如何仅通过Chrome浏览器打开链接?
NSString* url = @"some url";
NSURL *inputURL = [NSURL URLWithString:url];
NSString *scheme = inputURL.scheme;
// Replace the URL Scheme with the Chrome equivalent.
NSString *chromeScheme = nil;
if ([scheme isEqualToString:@"http"]) {
chromeScheme = @"googlechrome";
} else if ([scheme isEqualToString:@"https"]) {
chromeScheme = @"googlechromes";
}
// Proceed only if a valid Google Chrome URI Scheme is available.
if (chromeScheme) {
NSString *absoluteString = [inputURL absoluteString];
NSRange rangeForScheme = [absoluteString rangeOfString:@":"];
NSString *urlNoScheme =
[absoluteString substringFromIndex:rangeForScheme.location];
NSString *chromeURLString =
[chromeScheme stringByAppendingString:urlNoScheme];
NSURL *chromeURL = [NSURL URLWithString:chromeURLString];
// Open the URL with Chrome.
[[UIApplication sharedApplication] openURL:chromeURL];
}
我什至在.plist中添加了以下内容,
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechrome</string>
</array>
但是仍然无法正常工作。
首先确定您已在测试的iPhone设备中安装了Google Chrome浏览器应用程序。如果您正在将此代码测试到模拟器中,则它将无法工作,因为无法在Xcode模拟器中安装Google Chrome浏览器应用。在iPhone设备上,您可以通过以下方式检查chrome应用程序的安装
//Check if Google Chrome is Instaled
if ([[UIApplication sharedApplication] canOpenURL:chromeURL]) {
//open URL in Google chrome browser app
[[UIApplication sharedApplication] openURL:chromeURL];
}
else
{
//Remove Google Chrome Scheme at start of application and open link in safari append http or https at start
[[UIApplication sharedApplication] openURL:safariURL];
}
您的代码运行正常
结果一
运行形式的申请
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句