blob: 329f299643809ea4e640f5d7cc0f32d1302c8fa1 [file] [log] [blame] [view]
jasonkliu00a6d2f92017-03-28 09:30:111# Opening links in Chrome for iOS
jasonkliube0bf2f2017-03-28 00:10:312
3The easiest way to have your iOS app open links in Chrome is to use the
4[OpenInChromeController](https://github.com/GoogleChrome/OpenInChrome) class.
5This API is described here along with the URI schemes it supports.
6
7## Using OpenInChromeController to open links
8
9The **OpenInChromeController** class provides methods that
10encapsulate the URI schemes and the scheme replacement process also described
11in this document. Use this class to check if Chrome is installed or to specify
12the URL to open.
13
14### Methods
15
jasonkliu00a6d2f92017-03-28 09:30:1116* `isChromeInstalled`: returns YES if Chrome is installed
17* `openInChrome`: opens a given URL in Chrome
jasonkliube0bf2f2017-03-28 00:10:3118
19For example, use the OpenInChromeController class as follows:
20
21```
22if ([openInController_ isChromeInstalled]) {
23 [openInController_ openInChrome:urlToOpen];
24}
25```
26
27## Downloading the class file
28
29The OpenInChromeController class file is available
30[here](https://github.com/GoogleChrome/OpenInChrome). Copy it into
31your Xcode installation.
32
33The rest of this document describes the underpinnings of this API.
34
35## URI schemes
36
37Chrome for iOS handles the following URI Schemes:
38
39* `googlechrome` for http
40* `googlechromes` for https
41
42To check if Chrome is installed, an app can simply check if either of these URI schemes is available:
43
44```
45[[UIApplication sharedApplication] canOpenURL:
46 [NSURL URLWithString:@"googlechrome://"]];
47```
48
49This step is useful in case an app would like to change the UI depending
50on if Chrome is installed or not. For instance the app could add an
51option to open URLs in Chrome in a share menu or action sheet.
52
53To actually open a URL in Chrome, the URI scheme provided in the URL
54must be changed from `http` or `https` to the Google Chrome equivalent of
55`googlechrome` or `googlechromes` respectively.
56The following sample code opens a URL in Chrome:
57
58```
59NSURL *inputURL = <the URL to open>;
60NSString *scheme = inputURL.scheme;
61
62// Replace the URL Scheme with the Chrome equivalent.
63NSString *chromeScheme = nil;
64if ([scheme isEqualToString:@"http"]) {
65 chromeScheme = @"googlechrome";
66} else if ([scheme isEqualToString:@"https"]) {
67 chromeScheme = @"googlechromes";
68}
69
70// Proceed only if a valid Google Chrome URI Scheme is available.
71if (chromeScheme) {
72 NSString *absoluteString = [inputURL absoluteString];
73 NSRange rangeForScheme = [absoluteString rangeOfString:@":"];
74 NSString *urlNoScheme =
75 [absoluteString substringFromIndex:rangeForScheme.location];
76 NSString *chromeURLString =
77 [chromeScheme stringByAppendingString:urlNoScheme];
78 NSURL *chromeURL = [NSURL URLWithString:chromeURLString];
79
80 // Open the URL with Chrome.
81 [[UIApplication sharedApplication] openURL:chromeURL];
82}
83```
84
85If Chrome is installed, the above code converts the URI scheme found in
86the URL to the Google Chrome equivalent. When Google Chrome opens, the
87URL passed as a parameter will be opened in a new tab.
88
89If Chrome is not installed the user can be prompted to download it from the App Store.
90If the user agrees, the app can open the App Store download page using the following:
91
92```
93[[UIApplication sharedApplication] openURL:[NSURL URLWithString:
94 @"itms-apps://itunes.apple.com/us/app/chrome/id535886823"]];
95```