Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS objective c中打开链接时启动chrome浏览器_Ios_Objective C_Google Chrome_Nsurl_Launch - Fatal编程技术网

如何在iOS objective c中打开链接时启动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:

这是我的密码

如何仅通过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
谷歌色素

但它仍然不起作用。

首先,确保在测试的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中,我该怎么做@HafizShahzadAliIn
LSApplicationQueriesSchemes
您声明了
googlechrome
,但未包含变体
googlechrome
类似问题,并在此处找到类似解决方案: