如何在 iOS objective c 中打开 link 时启动 chrome 浏览器

How to launch chrome browser while opening a link in iOS objective c

这是我的代码,

如何仅通过 chrome 浏览器打开 link?

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 浏览器应用程序。如果您正在将此代码测试到模拟器中,那么它将无法运行,因为 Google Chrome 浏览器应用程序无法安装在 Xcode simulator.On 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];

    }

您的代码运行良好

Code Spinet

结果一

Screeen Demo

运行 形式的申请

Final Result