如何在iOS objective c中打开链接时启动chrome浏览器
这是我的密码 如何仅通过chrome浏览器打开链接如何在iOS objective c中打开链接时启动chrome浏览器,ios,objective-c,google-chrome,nsurl,launch,Ios,Objective C,Google Chrome,Nsurl,Launch,这是我的密码 如何仅通过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:
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
谷歌色素
但它仍然不起作用。首先,确保在测试的iPhone设备中安装了Google Chrome浏览器应用程序。如果您正在模拟器中测试此代码,那么它将无法工作,因为Xcode模拟器中无法安装Google 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];
}
你的代码运行良好
结果一
运行状态下的应用程序
好的。。谢谢你,我认为这个答案不太正确。Apple文档非常清楚在使用文档中的
canOpenURL:
时需要lsapplicationqueryschemes
如果您的应用程序在iOS 9.0上或之后链接,则必须声明要传递给此方法的URL方案。为此,请将LSApplicationQueriesSchemes密钥添加到应用程序的Info.plist文件中代码>是的,你是对的。对于canOpenURL,我们需要根据苹果的文档在info plist文件中添加密钥。事实上,我甚至在iOS 11上测试了上述代码,但没有添加该键,而且运行良好。我已经更新了答案,感谢您的更正。在Xcode 11.5和swift 5中,我该怎么做@HafizShahzadAliInLSApplicationQueriesSchemes
您声明了googlechrome
,但未包含变体googlechrome
类似问题,并在此处找到类似解决方案: