OLD | NEW |
(Empty) | |
| 1 # Opening links in Chrome for iOS</h1> |
| 2 |
| 3 The easiest way to have your iOS app open links in Chrome is to use the |
| 4 [OpenInChromeController](https://github.com/GoogleChrome/OpenInChrome) class. |
| 5 This API is described here along with the URI schemes it supports. |
| 6 |
| 7 ## Using OpenInChromeController to open links |
| 8 |
| 9 The **OpenInChromeController** class provides methods that |
| 10 encapsulate the URI schemes and the scheme replacement process also described |
| 11 in this document. Use this class to check if Chrome is installed or to specify |
| 12 the URL to open. |
| 13 |
| 14 ### Methods |
| 15 |
| 16 * `isChromeInstalled`: returns YES if Chrome is installed</li> |
| 17 * `openInChrome`: opens a given URL in Chrome</li> |
| 18 |
| 19 For example, use the OpenInChromeController class as follows: |
| 20 |
| 21 ``` |
| 22 if ([openInController_ isChromeInstalled]) { |
| 23 [openInController_ openInChrome:urlToOpen]; |
| 24 } |
| 25 ``` |
| 26 |
| 27 ## Downloading the class file |
| 28 |
| 29 The OpenInChromeController class file is available |
| 30 [here](https://github.com/GoogleChrome/OpenInChrome). Copy it into |
| 31 your Xcode installation. |
| 32 |
| 33 The rest of this document describes the underpinnings of this API. |
| 34 |
| 35 ## URI schemes |
| 36 |
| 37 Chrome for iOS handles the following URI Schemes: |
| 38 |
| 39 * `googlechrome` for http |
| 40 * `googlechromes` for https |
| 41 |
| 42 To 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 |
| 49 This step is useful in case an app would like to change the UI depending |
| 50 on if Chrome is installed or not. For instance the app could add an |
| 51 option to open URLs in Chrome in a share menu or action sheet. |
| 52 |
| 53 To actually open a URL in Chrome, the URI scheme provided in the URL |
| 54 must be changed from `http` or `https` to the Google Chrome equivalent of |
| 55 `googlechrome` or `googlechromes` respectively. |
| 56 The following sample code opens a URL in Chrome: |
| 57 |
| 58 ``` |
| 59 NSURL *inputURL = <the URL to open>; |
| 60 NSString *scheme = inputURL.scheme; |
| 61 |
| 62 // Replace the URL Scheme with the Chrome equivalent. |
| 63 NSString *chromeScheme = nil; |
| 64 if ([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. |
| 71 if (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 |
| 85 If Chrome is installed, the above code converts the URI scheme found in |
| 86 the URL to the Google Chrome equivalent. When Google Chrome opens, the |
| 87 URL passed as a parameter will be opened in a new tab. |
| 88 |
| 89 If Chrome is not installed the user can be prompted to download it from the App
Store. |
| 90 If the user agrees, the app can open the App Store download page using the follo
wing: |
| 91 |
| 92 ``` |
| 93 [[UIApplication sharedApplication] openURL:[NSURL URLWithString: |
| 94 @"itms-apps://itunes.apple.com/us/app/chrome/id535886823"]]; |
| 95 ``` |
OLD | NEW |