【发布时间】:2026-01-19 03:40:01
【问题描述】:
这是我的代码,
如何只通过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>
但还是不行。
【问题讨论】:
-
在
LSApplicationQueriesSchemes中,您声明了googlechrome,但未包含变体googlechromes -
在此处找到类似解决方案的类似问题:*.com/questions/34149611/…
标签: ios objective-c google-chrome nsurl launch