1

Here is my code,

How to open the link only via chrome browser?

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];
}

I have even added the following in .plist,

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>googlechrome</string>
    </array>

But still it is not working.

James Z
  • 12,209
  • 10
  • 24
  • 44
Akshara
  • 141
  • 1
  • 11
  • In `LSApplicationQueriesSchemes` you declared `googlechrome` but did not include the variation `googlechromes` – christopherdrum May 15 '18 at 07:23
  • Similar problem with similar solutions found here: https://stackoverflow.com/questions/34149611/canopenurl-returning-true-for-custom-url-scheme-even-if-app-is-not-installed – christopherdrum May 15 '18 at 07:25

1 Answers1

2

First Of All Make Sure You have installed Google Chrome browser application in your test iPhone device. If you are testing this code into simulator, then it will not work, because Google Chrome browser app can not be installed in Xcode simulator.On iPhone Device You can check the installation of chrome application in following way

//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];

    }

Your code is working fine

Code Spinet

Result One

Screeen Demo

Applicarion in running form

Final Result

  • Ok.. Thanks a lot – Akshara May 15 '18 at 04:47
  • I don't think this answer is quite correct. Apple documentation is very clear about the need for `LSApplicationQueriesSchemes` when using `canOpenURL:` From the documentation: `If your app is linked on or after iOS 9.0, you must declare the URL schemes you want to pass to this method. Do this by adding the LSApplicationQueriesSchemes key to your app's Info.plist file. ` – christopherdrum May 15 '18 at 07:21
  • Yeah you are right. For canOpenURL we need to add key in info plist file according to Apple Documentation. Actually i practically tested above code even on iOS 11 With out adding that key and that was also working fine. I have updated answer and thanks for correction. – Hafiz Shahzad Ali Khurram May 15 '18 at 11:03
  • How do i do this, in Xcode 11.5 and swift 5? @HafizShahzadAli – Ali Feb 22 '21 at 08:06