├── .circleci └── config.yml ├── .gitignore ├── .npmrc ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── package.json ├── plugin.xml └── src ├── android └── com │ └── ionicframework │ └── cordova │ └── webview │ ├── AndroidProtocolHandler.java │ ├── IonicWebView.java │ ├── IonicWebViewEngine.java │ ├── UriMatcher.java │ └── WebViewLocalServer.java ├── ios ├── CDVWKProcessPoolFactory.h ├── CDVWKProcessPoolFactory.m ├── CDVWKWebViewEngine.h ├── CDVWKWebViewEngine.m ├── CDVWKWebViewUIDelegate.h ├── CDVWKWebViewUIDelegate.m ├── IONAssetHandler.h ├── IONAssetHandler.m ├── LICENSE └── wk-plugin.js └── www ├── ios └── ios-wkwebview-exec.js └── util.js /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | aliases: 4 | - &restore-cache 5 | keys: 6 | - dependency-cache-{{ checksum "package.json" }}-1 7 | 8 | - &save-cache 9 | key: dependency-cache-{{ checksum "package.json" }}-1 10 | paths: 11 | - node_modules 12 | 13 | defaults: &defaults 14 | docker: 15 | - image: circleci/node:10 16 | working_directory: /tmp/workspace 17 | 18 | jobs: 19 | build: 20 | <<: *defaults 21 | steps: 22 | - checkout 23 | - restore_cache: *restore-cache 24 | - run: npm install 25 | - save_cache: *save-cache 26 | - persist_to_workspace: 27 | root: /tmp/workspace 28 | paths: 29 | - "*" 30 | 31 | deploy: 32 | <<: *defaults 33 | environment: 34 | GIT_AUTHOR_NAME: Ionitron 35 | GIT_AUTHOR_EMAIL: hi@ionicframework.com 36 | GIT_COMMITTER_NAME: Ionitron 37 | GIT_COMMITTER_EMAIL: hi@ionicframework.com 38 | steps: 39 | - add_ssh_keys: 40 | fingerprints: 41 | - "ae:6d:3a:f1:cf:39:e1:94:6e:22:2a:9f:54:f9:b0:1b" # ionitron user key 42 | - checkout 43 | - attach_workspace: 44 | at: /tmp/workspace 45 | - run: npx semantic-release 46 | 47 | 48 | workflows: 49 | version: 2 50 | build: 51 | jobs: 52 | - build 53 | - deploy: 54 | requires: [build] 55 | filters: 56 | branches: 57 | only: stable 58 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #If ignorance is bliss, then somebody knock the smile off my face 2 | 3 | *.csproj.user 4 | *.suo 5 | *.cache 6 | Thumbs.db 7 | *.DS_Store 8 | 9 | *.bak 10 | *.cache 11 | *.log 12 | *.swp 13 | *.user 14 | 15 | node_modules 16 | xcuserdata 17 | package-lock.json 18 | 19 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # [5.0.1](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v5.0.0...v5.0.1) (2023-08-29) 2 | 3 | ### Features 4 | 5 | * **ios:** iOS 16.4 Webkit inspection enabled correctly https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/677 6 | 7 | # [5.0.0](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v4.2.1...v5.0.0) (2020-05-27) 8 | 9 | 10 | ### Features 11 | 12 | * **android:** remove RequiresApi annotation and drop support for API 19 ([#540](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/540)) ([17d2ada](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/17d2ada038cbd6548ed14887b1b7a0cbc98f1d83)) 13 | * **ios:** remove normalizeURL function ([#576](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/576)) ([d5bdbaa](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/d5bdbaaf128bdd49cebe50fff52fbcf226998e7d)) 14 | * **ios:** remove wkRewriteURL function ([#577](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/577)) ([02c18ea](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/02c18ea359e344ab904733b8dc582ceb4e25d581)) 15 | 16 | 17 | ### BREAKING CHANGES 18 | 19 | * **ios:** wkRewriteURL was deprecated and has been removed, use 20 | window.Ionic.WebView.convertFileSrc instead 21 | * **ios:** normalizeURL was deprecated and has been removed, use 22 | window.Ionic.WebView.convertFileSrc instead 23 | * **android:** Drop Android 4.4 support (SDK API level 19). Min required is 21. 24 | 25 | ## [4.2.1](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v4.2.0...v4.2.1) (2020-04-28) 26 | 27 | 28 | ### Bug Fixes 29 | 30 | * **ios:** release userAgentLock for IAB compat ([#558](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/558)) ([4c027f3](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/4c027f3e1dadf790b1d699936b90b670b401db9e)), closes [#551](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/551) 31 | 32 | # [4.2.0](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v4.1.3...v4.2.0) (2020-04-14) 33 | 34 | 35 | ### Bug Fixes 36 | 37 | * **ionassethandler.m:** fix startPath is getting null ([#463](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/463)) ([0bf16f1](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/0bf16f1b73b853f40781c5de83964457cc4493d5)) 38 | * **ios:** avoid app scrolling to top on keyboard hide ([#533](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/533)) ([7974eb4](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/7974eb4160f5e83cf4b3e98905beba1f874464a6)) 39 | * **ios:** Replace deprecated APIs ([#539](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/539)) ([27b9021](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/27b9021d5d76b3e6dc6bfc83ab46b98cd301e694)) 40 | 41 | 42 | ### Features 43 | 44 | * **android:** proxy service worker requests through local server ([#452](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/452)) ([c672175](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/c672175b7527d64b077f7715b2ff145325524add)) 45 | * **ios:** implement custom userAgent handling ([#537](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/537)) ([8587114](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/85871147ba8e5b23b693e518bf5ea800cccce8cc)) 46 | 47 | ## [4.1.3](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v4.1.2...v4.1.3) (2019-10-30) 48 | 49 | 50 | ### Bug Fixes 51 | 52 | * **android:** return proper mimeType for .mjs files ([#455](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/455)) ([173a313](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/173a313)) 53 | * **ios:** mitigate media memory usage ([#459](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/459)) ([cbd526d](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/cbd526d)) 54 | * **ios:** remove itms-services private scheme ([#464](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/464)) ([d7d2600](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/d7d2600)) 55 | 56 | ## [4.1.2](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v4.1.1...v4.1.2) (2019-09-25) 57 | 58 | 59 | ### Bug Fixes 60 | 61 | * **android:** allow schemes that start by https ([#437](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/437)) ([fab9d1f](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/fab9d1f)) 62 | * **Android:** return proper mimeType for wasm files ([0eb8a37](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/0eb8a37)) 63 | * **ios:** make programmatically focus work on iOS 13 ([#438](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/438)) ([7a514b0](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/7a514b0)), closes [#435](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/435) 64 | 65 | ## [4.1.1](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v4.1.0...v4.1.1) (2019-06-26) 66 | 67 | 68 | ### Bug Fixes 69 | 70 | * **ios:** show error message when app fails to load ([#382](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/382)) ([cb1f026](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/cb1f026)) 71 | 72 | # [4.1.0](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v4.0.1...v4.1.0) (2019-06-10) 73 | 74 | 75 | ### Features 76 | 77 | * **ios:** Add WKSuspendInBackground preference ([#356](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/356)) ([3613602](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/3613602)) 78 | 79 | ## [4.0.1](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v4.0.0...v4.0.1) (2019-03-26) 80 | 81 | 82 | ### Bug Fixes 83 | 84 | * **ios:** Fix autofocus on iOS 12.2 ([#334](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/334)) ([cb4c491](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/cb4c491)), closes [#330](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/330) 85 | * account port on resolving uri path ([#321](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/321)) ([fdfe8aa](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/fdfe8aa)) 86 | 87 | # [4.0.0](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v3.1.2...v4.0.0) (2019-02-18) 88 | 89 | 90 | ### Features 91 | 92 | * **ios:** Make iOS app Scheme configurable with a preference ([#307](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/307)) ([d52d37e](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/d52d37e)), closes [#282](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/282) 93 | * **ios:** Remove WKSuspendInBackground preference ([#309](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/309)) ([73b6659](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/73b6659)), closes [#286](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/286) 94 | 95 | 96 | ### BREAKING CHANGES 97 | 98 | * **ios:** Remove the WKSuspendInBackground preference, so app relying on that prefere will 99 | not behave as expected 100 | 101 | ## [3.1.2](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v3.1.1...v3.1.2) (2019-02-04) 102 | 103 | 104 | ### Bug Fixes 105 | 106 | * **Android:** Handle Range Requests for proper media file handling ([#298](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/298)) ([6f18248](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/6f18248)), closes [#248](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/248) [#205](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/205) [#141](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/141) 107 | 108 | ## [3.1.1](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v3.1.0...v3.1.1) (2019-01-18) 109 | 110 | 111 | ### Bug Fixes 112 | 113 | * **ios:** Remove unused code ([#281](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/281)) ([fc7ea27](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/fc7ea27)) 114 | 115 | # [3.1.0](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v3.0.0...v3.1.0) (2019-01-17) 116 | 117 | 118 | ### Bug Fixes 119 | 120 | * **ios:** Fix video playback of files with uppercase extension ([#264](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/264)) ([2c4b225](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/2c4b225)), closes [#260](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/260) 121 | * Set engines to require Cordova CLI 7.1.0 or newer ([#276](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/276)) ([40f42e1](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/40f42e1)), closes [#263](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/263) 122 | * Use a single scheme for all files ([#270](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/270)) ([3d1bcdd](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/3d1bcdd)), closes [#258](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/258) 123 | 124 | 125 | ### Features 126 | 127 | * **Android:** Make app Scheme configurable with a preference ([#274](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/274)) ([18d9f2c](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/18d9f2c)), closes [#269](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/269) [#255](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/255) 128 | 129 | # [3.0.0](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v2.3.1...v3.0.0) (2019-01-03) 130 | 131 | 132 | ### Bug Fixes 133 | 134 | * **iOS:** Remove unused code ([#247](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/247)) ([bceb17a](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/bceb17a)) 135 | 136 | 137 | ### Features 138 | 139 | * Allows configuration of Mixed Content Mode ([#240](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/240)) ([486d412](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/486d412)), closes [#231](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/231) 140 | * **Android:** Implement ionic-file and ionic-content urls ([#242](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/242)) ([8ef0c30](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/8ef0c30)), closes [#204](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/204) [#183](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/183) 141 | * **iOS:** Remove GCDWebServer ([#244](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/244)) ([0dee0cf](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/0dee0cf)) 142 | * **WebViewLocalServer.java:** return 404 error code when a local file is not found ([#217](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/217)) ([f7a551e](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/f7a551e)), closes [#216](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/216) 143 | 144 | 145 | ### BREAKING CHANGES 146 | 147 | * **iOS:** Sets deployment-target to 11, so will only work on iOS 11+ 148 | 149 | * Address changes 150 | * changes the default from 1 (never) to 0 (always) 151 | * **WebViewLocalServer.java:** Until now, the Android part of the plugin was returning a 200 http code even though 152 | the requested file didn't exist. This behavior was inconsistent with the historical behavior of the 153 | iOS webView. This change makes them both work in the same manner but introduces a breaking change 154 | for the current Android users that are expecting a 200 http code no matter what and are testing the 155 | not found error just by checking if the body is null. 156 | 157 | ## [2.3.1](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v2.3.0...v2.3.1) (2018-12-06) 158 | 159 | 160 | ### Bug Fixes 161 | 162 | * Handle convertFileSrc when using ionic:// scheme ([#236](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/236)) ([89ce899](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/89ce899)) 163 | 164 | # [2.3.0](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v2.2.5...v2.3.0) (2018-12-05) 165 | 166 | 167 | ### Features 168 | 169 | * **ios:** Add URLSchemeHandler for iOS 11+ ([#221](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/221)) ([4a973f4](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/4a973f4)) 170 | 171 | ## [2.2.5](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v2.2.4...v2.2.5) (2018-11-20) 172 | 173 | 174 | ### Bug Fixes 175 | 176 | * Add option for Dark keyboard appearance ([#44](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/44)) ([6c0fe56](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/6c0fe56)) 177 | 178 | ## [2.2.4](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v2.2.3...v2.2.4) (2018-11-20) 179 | 180 | 181 | ### Bug Fixes 182 | 183 | * fix keyboard displacement bug in iOS 12 WKWebView ([#201](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/201)) ([a670568](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/a670568)) 184 | 185 | ## [2.2.3](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v2.2.2...v2.2.3) (2018-11-09) 186 | 187 | 188 | ### Bug Fixes 189 | 190 | * Remove main and fix description ([d52db66](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/d52db66)) 191 | 192 | ## [2.2.2](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v2.2.1...v2.2.2) (2018-11-09) 193 | 194 | ### Bug Fixes 195 | 196 | * Add more server checks before loading urls or reloading ([#211](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/211)) ([60eff2f](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/60eff2f)) 197 | 198 | ## [2.2.1](https://github.com/ionic-team/cordova-plugin-ionic-webview/compare/v2.2.0...v2.2.1) (2018-11-07) 199 | 200 | 201 | ### Bug Fixes 202 | 203 | * Show error page if server is not running ([#207](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/207)) ([6a2e07e](https://github.com/ionic-team/cordova-plugin-ionic-webview/commit/6a2e07e)) 204 | 205 | 206 | ### 2.2.0 (2018-10-04) 207 | 208 | * Fix issue where two apps running on the same port could conflict with each other ([#169](https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/165) & [#186](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/186)) 209 | * Add kitkat support (API 19) ([#144](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/144)) [@leo6104](https://github.com/leo6104) 210 | * Fix issue where local server was being used if launch URL is external ([#169](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/169)) 211 | 212 | 213 | ### 2.1.4 (2018-09-13) 214 | 215 | * Allow Ionic Deploy `DisableDeploy` preference to disable loading of deploy updates ([#172](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/172)) 216 | 217 | 218 | ### 2.1.3 (2018-09-06) 219 | 220 | * Make server path relative ([#164](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/164)) 221 | 222 | 223 | ### 2.1.2 (2018-09-05) 224 | 225 | * Return 404 response when file doesn't exist ([#162](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/162)) 226 | * Load local assets if the app is a freshly installed binary ([#155](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/155)) 227 | * Reset stored server path on new binary ([#161](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/161)) 228 | 229 | 230 | ### 2.1.1 (2018-09-04) 231 | 232 | * Allow range requests for local files ([#154](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/154)) 233 | 234 | 235 | ### 2.1.0 (2018-08-23) 236 | 237 | * Add support for `cordova-android` 6 ([#150](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/150)) 238 | 239 | 240 | ### 2.0.3 (2018-08-14) 241 | 242 | * Fix nil reference by setting up the server URL before routes are set up. ([#135](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/135)) [@matejkramny](https://github.com/matejkramny) 243 | * Resolve issue when app is launched in background. ([#124](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/124)) [@ghenry22](https://github.com/ghenry22) 244 | 245 | 246 | ### 2.0.2 (2018-07-30) 247 | 248 | * Immediately load new server base path upon setting it. ([#132](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/132)) 249 | 250 | 251 | ### 2.0.1 (2018-07-25) 252 | 253 | * Avoid "not modified" response on iOS by always overriding last modified date. ([#127](https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/127)) 254 | 255 | 256 | ### 2.0.0 (2018-07-23) 257 | 258 | * **BREAKING**: HTTP server now runs for iOS **and** Android, instead of just iOS. The server is configured the same for both platforms. 259 | * **BREAKING**: HTTP server now loads the app from a base href of `/`. The app URL behaves like `http://localhost:8080/index.html` instead of `http://localhost:8080/Users/.../index.html`. 260 | * **BREAKING**: HTTP server is configured to run in HTML5 routing mode (push state) by default. 261 | * **BREAKING**: File access through the Web View must be served by the HTTP server to avoid security errors in the Web View. Loading files via `file://` is not allowed by the Web View. The HTTP server will serve files via the `_file_` prefix, e.g. `http://localhost:8080/_file_/Users/.../file.png`. 262 | * `window.Ionic.normalizeURL()` has been deprecated. Use `window.Ionic.WebView.convertFileSrc()`. 263 | * iOS update HTTP server to latest upstream version (GCDwebserve 3.4.2) 264 | * iOS update HTTP server to restart sockets with error state when resuming from background 265 | * iOS enable HTTP server to continue running in background if the webview is running. 266 | * iOS enable Webview to continue running in background. Requires background mode capability enabled in xcode + valid use case as per app store requirements. If your app is not performing valid background tasks it will still be suspended by the OS as usual. As long as valid background tasks are running the webview will continue to function as expected. 267 | * iOS add config.xml options: 268 | * WKSuspendInBackground - defaults to true, if set to false then the webview and HTTP server will continue to run when the app is in the background or screen is locked 269 | * WKPort - defaults to 8080, define the port that the HTTP server will listen on 270 | * WKBind - defaults to localhost, if set to 127.0.0.1 then this IP will be used instead of the localhost hostname for the HTTP server 271 | 272 | See [Github releases](https://github.com/ionic-team/cordova-plugin-ionic-webview/releases) for earlier changes. 273 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | :mega: **Support/Questions?**: Please see our [Support Page](https://ionicframework.com/support) for general support questions. The issues on GitHub should be reserved for bug reports and feature requests. 4 | 5 | ### Bug Reports 6 | 7 | Please create an issue describing the bug in detail. 8 | 9 | ### Feature Requests 10 | 11 | Please create an issue! 12 | 13 | ## Developing 14 | 15 | Please familiarize yourself with [Cordova plugin development](https://cordova.apache.org/docs/en/latest/guide/hybrid/plugins/). 16 | 17 | You can use `cordova plugin add` with a local directory to copy and compile plugin changes into a test project. 18 | 19 | ### Workflow 20 | 21 | This repo uses [semantic-release](https://github.com/semantic-release/semantic-release), so it's important to follow a strict workflow to ensure properly automated releases. 22 | 23 | * Work off of `master` branch (create new branch or fork) 24 | * Make changes 25 | * Use `npm run cz` (or `git cz` if [commitizen](https://github.com/commitizen/cz-cli) is installed globally) to make commits 26 | * Create a pull request 27 | * Pull requests will be approved and squashed into the `master` branch 28 | * Try to make pull requests with a single objective (don't have multiple features in one PR, don't mix fixes and features in one PR, etc.) 29 | 30 | ### Publishing 31 | 32 | Releases are automated in CI using [semantic-release](https://github.com/semantic-release/semantic-release) when the `stable` branch is pushed to Github. Rebase `master` with `stable`. Commits in `master` should be appropriately formatted from the PR workflow (see [Workflow](#workflow)). 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 19 | 20 | 21 | 22 | [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) 23 | [![Dependabot Status](https://api.dependabot.com/badges/status?host=github&identifier=104773211)](https://dependabot.com) 24 | [![npm](https://img.shields.io/npm/v/cordova-plugin-ionic-webview.svg)](https://www.npmjs.com/package/cordova-plugin-ionic-webview) 25 | 26 | # Ionic Web View for Cordova 27 | 28 | A Web View plugin for Cordova, focused on providing the highest performance experience for Ionic apps (but can be used with any Cordova app). 29 | 30 | This plugin uses WKWebView on iOS and the latest evergreen webview on Android. Additionally, this plugin makes it easy to use HTML5 style routing that web developers expect for building single-page apps. 31 | 32 | Note: This repo and its documentation are for `cordova-plugin-ionic-webview` @ `5.x`, which uses the new features that may not work with all apps. See [Requirements](#plugin-requirements) and [Migrating to 5.x](#migrating-to-5x). 33 | 34 | 2.x documentation can be found [here](https://github.com/ionic-team/cordova-plugin-ionic-webview/blob/2.x/README.md). 35 | 36 | :book: **Documentation**: [https://beta.ionicframework.com/docs/building/webview][ionic-webview-docs] 37 | 38 | :mega: **Support/Questions?** Please see our [Support Page][ionic-support] for general support questions. The issues on GitHub should be reserved for bug reports and feature requests. 39 | 40 | :sparkling_heart: **Want to contribute?** Please see [CONTRIBUTING.md](https://github.com/ionic-team/cordova-plugin-ionic-webview/blob/master/CONTRIBUTING.md). 41 | 42 | ## Configuration 43 | 44 | This plugin has several configuration options that can be set in `config.xml`. 45 | 46 | ### Android and iOS Preferences 47 | 48 | Preferences available for both iOS and Android 49 | 50 | #### Hostname 51 | 52 | `` 53 | 54 | Default value is `localhost`. 55 | 56 | Example `ionic://app` on iOS, `http://app` on Android. 57 | 58 | If you change it, you'll need to add a new `allow-navigation` entry in the `config.xml` for the configured url (i.e `` if `Hostname` is set to `app`). 59 | This is only needed for the Android url when using `http://`, `https://` or a custom scheme. All `ionic://` urls are whitelisted by the plugin. 60 | 61 | ### Android Preferences 62 | 63 | Preferences only available Android platform 64 | 65 | #### Scheme 66 | 67 | ```xml 68 | 69 | ``` 70 | 71 | Default value is `http` 72 | 73 | Configures the Scheme the app uses to load the content. 74 | 75 | #### ResolveServiceWorkerRequests 76 | 77 | ```xml 78 | 79 | ``` 80 | 81 | Default value is `false` 82 | 83 | Enable to resolve requests made by Service Workers through the local server. 84 | 85 | #### MixedContentMode 86 | 87 | ```xml 88 | 89 | ``` 90 | 91 | Configures the WebView's behavior when an origin attempts to load a resource from a different origin. 92 | 93 | Default value is `0` (`MIXED_CONTENT_ALWAYS_ALLOW`), which allows loading resources from other origins. 94 | 95 | Other possible values are `1` (`MIXED_CONTENT_NEVER_ALLOW`) and `2` (`MIXED_CONTENT_COMPATIBILITY_MODE`) 96 | 97 | 98 | [Android documentation](https://developer.android.com/reference/android/webkit/WebSettings.html#setMixedContentMode(int)) 99 | 100 | 101 | ### iOS Preferences 102 | 103 | Preferences only available for iOS platform 104 | 105 | #### iosScheme 106 | 107 | ```xml 108 | 109 | ``` 110 | 111 | Default value is `ionic` 112 | 113 | Configures the Scheme the app uses to load the content. 114 | 115 | Values like `http`, `https` or `file` are not valid and will use default value instead. 116 | 117 | If you change it, you'll need to add a new `allow-navigation` entry in the `config.xml` for the configured scheme (i.e `` if `iosScheme` is set to `httpsionic`). 118 | 119 | #### WKSuspendInBackground 120 | 121 | ```xml 122 | 123 | ``` 124 | 125 | Default value is `true` (suspend). 126 | 127 | Set to false to stop WKWebView suspending in background too eagerly. 128 | 129 | #### KeyboardAppearanceDark 130 | 131 | ```xml 132 | 133 | ``` 134 | 135 | Whether to use a dark styled keyboard on iOS 136 | 137 | #### ScrollEnabled 138 | 139 | ```xml 140 | 141 | ``` 142 | 143 | Ionic apps work better if the WKWebView is not scrollable, so the scroll is disabled by default, but can be enabled with this preference. This only affects the main ScrollView of the WKWebView, so only affects the body, not other scrollable components. 144 | 145 | ## Plugin Requirements 146 | 147 | * **Cordova CLI**: 7.1.0+ 148 | * **iOS**: iOS 11+ and `cordova-ios` 4+ 149 | * **Android**: Android 5+ and `cordova-android` 6.4+ 150 | 151 | ## Migrating to 5.x 152 | 153 | 1. Remove and re-add the Web View plugin: 154 | 155 | ``` 156 | cordova plugin rm cordova-plugin-ionic-webview 157 | cordova plugin add cordova-plugin-ionic-webview@latest 158 | ``` 159 | 160 | 1. If using `cordova-android` < 9, make sure you have `` in the config.xml as this version of the plugin only supports Android 5+ (SDK 21+). If using `cordova-android` >= 9 is not neccessary as `cordova-android` 9 only supports Android 5.1+ (SDK 22+) 161 | 162 | 1. Since version 2, apps are served from HTTP on Android by default. 163 | 164 | * The default origin for requests from the Android WebView is `http://localhost`. If `Hostname` and `Scheme` preferences are set, then origin will be `schemeValue://HostnameValue`. 165 | 166 | 1. Since version 3, apps are served from `ionic://` scheme on iOS by default. 167 | 168 | * The default origin for requests from the iOS WebView is `ionic://localhost`. If `Hostname` and `iosScheme` preferences are set, then origin will be `iosSchemeValue://HostnameValue`. 169 | 170 | 1. The WebView is not able to display images, videos or other files from file or content protocols or if it doesn't have protocol at all. For those cases use `window.Ionic.WebView.convertFileSrc()` to get the proper url. 171 | 172 | 1. Replace any usages of `window.Ionic.normalizeURL()` and `window.wkRewriteURL()` with `window.Ionic.WebView.convertFileSrc()`. 173 | 174 | * For Ionic Angular projects, there is an [Ionic Native wrapper](https://beta.ionicframework.com/docs/native/ionic-webview): 175 | 176 | ``` 177 | npm install @ionic-native/ionic-webview@latest 178 | ``` 179 | 180 | [ionic-homepage]: https://ionicframework.com 181 | [ionic-docs]: https://ionicframework.com/docs 182 | [ionic-webview-docs]: https://beta.ionicframework.com/docs/building/webview 183 | [ionic-support]: https://ionicframework.com/support 184 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-plugin-ionic-webview", 3 | "version": "5.0.1", 4 | "description": "Ionic Web View Engine Plugin", 5 | "scripts": { 6 | "sync_plugin_xml": "sync-cordova-xml package.json plugin.xml --output=plugin.xml", 7 | "version": "npm run sync_plugin_xml && git add plugin.xml", 8 | "cz": "git-cz" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/ionic-team/cordova-plugin-ionic-webview" 13 | }, 14 | "bugs": { 15 | "url": "https://github.com/ionic-team/cordova-plugin-ionic-webview/issues" 16 | }, 17 | "keywords": [ 18 | "cordova", 19 | "wkwebview" 20 | ], 21 | "author": "Ionic Team", 22 | "license": "Apache-2.0", 23 | "engines": { 24 | "cordovaDependencies": { 25 | "2.0.0": { 26 | "cordova-android": ">=6.4.0", 27 | "cordova-ios": ">=4.0.0-dev" 28 | }, 29 | "3.1.0": { 30 | "cordova-android": ">=6.4.0", 31 | "cordova-ios": ">=4.0.0-dev", 32 | "cordova": ">=7.1.0" 33 | } 34 | } 35 | }, 36 | "devDependencies": { 37 | "@semantic-release/changelog": "^3.0.0", 38 | "@semantic-release/exec": "^3.3.0", 39 | "@semantic-release/git": "^7.0.4", 40 | "@semantic-release/github": "^5.0.6", 41 | "@semantic-release/npm": "^5.0.4", 42 | "commitizen": "^4.0.3", 43 | "cz-conventional-changelog": "^3.0.2", 44 | "semantic-release": "^15.9.17", 45 | "sync-cordova-xml": "^0.4.0" 46 | }, 47 | "release": { 48 | "branch": "stable", 49 | "verifyConditions": [ 50 | "@semantic-release/changelog", 51 | "@semantic-release/npm", 52 | "@semantic-release/git", 53 | "@semantic-release/github" 54 | ], 55 | "prepare": [ 56 | "@semantic-release/changelog", 57 | "@semantic-release/npm", 58 | "@semantic-release/exec", 59 | "@semantic-release/git" 60 | ], 61 | "publish": [ 62 | "@semantic-release/github", 63 | "@semantic-release/npm" 64 | ], 65 | "success": [ 66 | "@semantic-release/github" 67 | ], 68 | "failure": [ 69 | "@semantic-release/github" 70 | ], 71 | "prepareCmd": "npm run version" 72 | }, 73 | "config": { 74 | "commitizen": { 75 | "path": "./node_modules/cz-conventional-changelog" 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 21 | 22 | 23 | cordova-plugin-ionic-webview 24 | Ionic Web View Engine Plugin 25 | Apache-2.0 26 | cordova,wkwebview 27 | https://github.com/ionic-team/cordova-plugin-ionic-webview 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | https://github.com/ionic-team/cordova-plugin-ionic-webview/issues 85 | Ionic Team 86 | 87 | -------------------------------------------------------------------------------- /src/android/com/ionicframework/cordova/webview/AndroidProtocolHandler.java: -------------------------------------------------------------------------------- 1 | package com.ionicframework.cordova.webview; 2 | 3 | // Copyright 2012 The Chromium Authors. All rights reserved. 4 | // Use of this source code is governed by a BSD-style license that can be 5 | // found in the LICENSE file. 6 | 7 | import android.content.Context; 8 | import android.content.res.AssetManager; 9 | import android.net.Uri; 10 | import android.util.Log; 11 | import android.util.TypedValue; 12 | 13 | import java.io.File; 14 | import java.io.FileInputStream; 15 | import java.io.IOException; 16 | import java.io.InputStream; 17 | import java.util.List; 18 | 19 | 20 | public class AndroidProtocolHandler { 21 | private static final String TAG = "AndroidProtocolHandler"; 22 | 23 | private Context context; 24 | 25 | public AndroidProtocolHandler(Context context) { 26 | this.context = context; 27 | } 28 | 29 | public InputStream openAsset(String path) throws IOException { 30 | return context.getAssets().open(path, AssetManager.ACCESS_STREAMING); 31 | } 32 | 33 | public InputStream openResource(Uri uri) { 34 | assert uri.getPath() != null; 35 | // The path must be of the form ".../asset_type/asset_name.ext". 36 | List pathSegments = uri.getPathSegments(); 37 | String assetType = pathSegments.get(pathSegments.size() - 2); 38 | String assetName = pathSegments.get(pathSegments.size() - 1); 39 | 40 | // Drop the file extension. 41 | assetName = assetName.split("\\.")[0]; 42 | try { 43 | // Use the application context for resolving the resource package name so that we do 44 | // not use the browser's own resources. Note that if 'context' here belongs to the 45 | // test suite, it does not have a separate application context. In that case we use 46 | // the original context object directly. 47 | if (context.getApplicationContext() != null) { 48 | context = context.getApplicationContext(); 49 | } 50 | int fieldId = getFieldId(context, assetType, assetName); 51 | int valueType = getValueType(context, fieldId); 52 | if (valueType == TypedValue.TYPE_STRING) { 53 | return context.getResources().openRawResource(fieldId); 54 | } else { 55 | Log.e(TAG, "Asset not of type string: " + uri); 56 | return null; 57 | } 58 | } catch (ClassNotFoundException e) { 59 | Log.e(TAG, "Unable to open resource URL: " + uri, e); 60 | return null; 61 | } catch (NoSuchFieldException e) { 62 | Log.e(TAG, "Unable to open resource URL: " + uri, e); 63 | return null; 64 | } catch (IllegalAccessException e) { 65 | Log.e(TAG, "Unable to open resource URL: " + uri, e); 66 | return null; 67 | } 68 | } 69 | 70 | public InputStream openFile(String filePath) throws IOException { 71 | String realPath = filePath.replace(WebViewLocalServer.fileStart, ""); 72 | File localFile = new File(realPath); 73 | return new FileInputStream(localFile); 74 | } 75 | 76 | public InputStream openContentUrl(Uri uri) throws IOException { 77 | Integer port = uri.getPort(); 78 | String realPath; 79 | if (port == -1) { 80 | realPath = uri.toString().replace(uri.getScheme() + "://" + uri.getHost() + WebViewLocalServer.contentStart, "content:/"); 81 | } else { 82 | realPath = uri.toString().replace(uri.getScheme() + "://" + uri.getHost() + ":" + port + WebViewLocalServer.contentStart, "content:/"); 83 | } 84 | InputStream stream = null; 85 | try { 86 | stream = context.getContentResolver().openInputStream(Uri.parse(realPath)); 87 | } catch (SecurityException e) { 88 | Log.e(TAG, "Unable to open content URL: " + uri, e); 89 | } 90 | return stream; 91 | } 92 | 93 | private static int getFieldId(Context context, String assetType, String assetName) 94 | throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { 95 | Class d = context.getClassLoader() 96 | .loadClass(context.getPackageName() + ".R$" + assetType); 97 | java.lang.reflect.Field field = d.getField(assetName); 98 | int id = field.getInt(null); 99 | return id; 100 | } 101 | 102 | private static int getValueType(Context context, int fieldId) { 103 | TypedValue value = new TypedValue(); 104 | context.getResources().getValue(fieldId, value, true); 105 | return value.type; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/android/com/ionicframework/cordova/webview/IonicWebView.java: -------------------------------------------------------------------------------- 1 | package com.ionicframework.cordova.webview; 2 | 3 | import android.app.Activity; 4 | import android.content.SharedPreferences; 5 | 6 | import org.apache.cordova.CallbackContext; 7 | import org.apache.cordova.CordovaPlugin; 8 | import org.json.JSONArray; 9 | import org.json.JSONException; 10 | 11 | public class IonicWebView extends CordovaPlugin { 12 | 13 | public static final String WEBVIEW_PREFS_NAME = "WebViewSettings"; 14 | public static final String CDV_SERVER_PATH = "serverBasePath"; 15 | 16 | public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 17 | 18 | if (action.equals("setServerBasePath")) { 19 | final String path = args.getString(0); 20 | cordova.getActivity().runOnUiThread(new Runnable() { 21 | public void run() { 22 | ((IonicWebViewEngine)webView.getEngine()).setServerBasePath(path); 23 | } 24 | }); 25 | return true; 26 | } else if (action.equals("getServerBasePath")) { 27 | callbackContext.success(((IonicWebViewEngine)webView.getEngine()).getServerBasePath()); 28 | return true; 29 | } else if (action.equals("persistServerBasePath")) { 30 | String path = ((IonicWebViewEngine)webView.getEngine()).getServerBasePath(); 31 | SharedPreferences prefs = cordova.getActivity().getApplicationContext().getSharedPreferences(WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE); 32 | SharedPreferences.Editor editor = prefs.edit(); 33 | editor.putString(CDV_SERVER_PATH, path); 34 | editor.apply(); 35 | return true; 36 | } 37 | return false; 38 | } 39 | 40 | } 41 | 42 | -------------------------------------------------------------------------------- /src/android/com/ionicframework/cordova/webview/IonicWebViewEngine.java: -------------------------------------------------------------------------------- 1 | package com.ionicframework.cordova.webview; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.content.SharedPreferences; 6 | import android.content.pm.PackageInfo; 7 | import android.graphics.Bitmap; 8 | import android.net.Uri; 9 | import android.os.Build; 10 | import android.util.Log; 11 | import android.webkit.ServiceWorkerController; 12 | import android.webkit.ServiceWorkerClient; 13 | import android.webkit.WebResourceRequest; 14 | import android.webkit.WebResourceResponse; 15 | import android.webkit.WebSettings; 16 | import android.webkit.WebView; 17 | import org.apache.cordova.ConfigXmlParser; 18 | import org.apache.cordova.CordovaInterface; 19 | import org.apache.cordova.CordovaPreferences; 20 | import org.apache.cordova.CordovaResourceApi; 21 | import org.apache.cordova.CordovaWebView; 22 | import org.apache.cordova.CordovaWebViewEngine; 23 | import org.apache.cordova.NativeToJsMessageQueue; 24 | import org.apache.cordova.PluginManager; 25 | import org.apache.cordova.engine.SystemWebViewClient; 26 | import org.apache.cordova.engine.SystemWebViewEngine; 27 | import org.apache.cordova.engine.SystemWebView; 28 | 29 | public class IonicWebViewEngine extends SystemWebViewEngine { 30 | public static final String TAG = "IonicWebViewEngine"; 31 | 32 | private WebViewLocalServer localServer; 33 | private String CDV_LOCAL_SERVER; 34 | private String scheme; 35 | private static final String LAST_BINARY_VERSION_CODE = "lastBinaryVersionCode"; 36 | private static final String LAST_BINARY_VERSION_NAME = "lastBinaryVersionName"; 37 | 38 | /** 39 | * Used when created via reflection. 40 | */ 41 | public IonicWebViewEngine(Context context, CordovaPreferences preferences) { 42 | super(new SystemWebView(context), preferences); 43 | Log.d(TAG, "Ionic Web View Engine Starting Right Up 1..."); 44 | } 45 | 46 | public IonicWebViewEngine(SystemWebView webView) { 47 | super(webView, null); 48 | Log.d(TAG, "Ionic Web View Engine Starting Right Up 2..."); 49 | } 50 | 51 | public IonicWebViewEngine(SystemWebView webView, CordovaPreferences preferences) { 52 | super(webView, preferences); 53 | Log.d(TAG, "Ionic Web View Engine Starting Right Up 3..."); 54 | } 55 | 56 | @Override 57 | public void init(CordovaWebView parentWebView, CordovaInterface cordova, final CordovaWebViewEngine.Client client, 58 | CordovaResourceApi resourceApi, PluginManager pluginManager, 59 | NativeToJsMessageQueue nativeToJsMessageQueue) { 60 | ConfigXmlParser parser = new ConfigXmlParser(); 61 | parser.parse(cordova.getActivity()); 62 | 63 | String hostname = preferences.getString("Hostname", "localhost"); 64 | scheme = preferences.getString("Scheme", "http"); 65 | CDV_LOCAL_SERVER = scheme + "://" + hostname; 66 | 67 | localServer = new WebViewLocalServer(cordova.getActivity(), hostname, true, parser, scheme); 68 | localServer.hostAssets("www"); 69 | 70 | webView.setWebViewClient(new ServerClient(this, parser)); 71 | 72 | super.init(parentWebView, cordova, client, resourceApi, pluginManager, nativeToJsMessageQueue); 73 | if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 74 | final WebSettings settings = webView.getSettings(); 75 | int mode = preferences.getInteger("MixedContentMode", 0); 76 | settings.setMixedContentMode(mode); 77 | } 78 | SharedPreferences prefs = cordova.getActivity().getApplicationContext().getSharedPreferences(IonicWebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE); 79 | String path = prefs.getString(IonicWebView.CDV_SERVER_PATH, null); 80 | if (!isDeployDisabled() && !isNewBinary() && path != null && !path.isEmpty()) { 81 | setServerBasePath(path); 82 | } 83 | 84 | boolean setAsServiceWorkerClient = preferences.getBoolean("ResolveServiceWorkerRequests", false); 85 | ServiceWorkerController controller = null; 86 | 87 | if (setAsServiceWorkerClient && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { 88 | controller = ServiceWorkerController.getInstance(); 89 | controller.setServiceWorkerClient(new ServiceWorkerClient(){ 90 | @Override 91 | public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) { 92 | return localServer.shouldInterceptRequest(request.getUrl(), request); 93 | } 94 | }); 95 | } 96 | } 97 | 98 | private boolean isNewBinary() { 99 | String versionCode = ""; 100 | String versionName = ""; 101 | SharedPreferences prefs = cordova.getActivity().getApplicationContext().getSharedPreferences(IonicWebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE); 102 | String lastVersionCode = prefs.getString(LAST_BINARY_VERSION_CODE, null); 103 | String lastVersionName = prefs.getString(LAST_BINARY_VERSION_NAME, null); 104 | 105 | try { 106 | PackageInfo pInfo = this.cordova.getActivity().getPackageManager().getPackageInfo(this.cordova.getActivity().getPackageName(), 0); 107 | versionCode = Integer.toString(pInfo.versionCode); 108 | versionName = pInfo.versionName; 109 | } catch(Exception ex) { 110 | Log.e(TAG, "Unable to get package info", ex); 111 | } 112 | 113 | if (!versionCode.equals(lastVersionCode) || !versionName.equals(lastVersionName)) { 114 | SharedPreferences.Editor editor = prefs.edit(); 115 | editor.putString(LAST_BINARY_VERSION_CODE, versionCode); 116 | editor.putString(LAST_BINARY_VERSION_NAME, versionName); 117 | editor.putString(IonicWebView.CDV_SERVER_PATH, ""); 118 | editor.apply(); 119 | return true; 120 | } 121 | return false; 122 | } 123 | 124 | private boolean isDeployDisabled() { 125 | return preferences.getBoolean("DisableDeploy", false); 126 | } 127 | private class ServerClient extends SystemWebViewClient { 128 | private ConfigXmlParser parser; 129 | 130 | public ServerClient(SystemWebViewEngine parentEngine, ConfigXmlParser parser) { 131 | super(parentEngine); 132 | this.parser = parser; 133 | } 134 | 135 | @Override 136 | public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { 137 | return localServer.shouldInterceptRequest(request.getUrl(), request); 138 | } 139 | 140 | @Override 141 | public void onPageStarted(WebView view, String url, Bitmap favicon) { 142 | super.onPageStarted(view, url, favicon); 143 | String launchUrl = parser.getLaunchUrl(); 144 | if (!launchUrl.contains(WebViewLocalServer.httpsScheme) && !launchUrl.contains(WebViewLocalServer.httpScheme) && url.equals(launchUrl)) { 145 | view.stopLoading(); 146 | // When using a custom scheme the app won't load if server start url doesn't end in / 147 | String startUrl = CDV_LOCAL_SERVER; 148 | if (!scheme.equalsIgnoreCase(WebViewLocalServer.httpsScheme) && !scheme.equalsIgnoreCase(WebViewLocalServer.httpScheme)) { 149 | startUrl += "/"; 150 | } 151 | view.loadUrl(startUrl); 152 | } 153 | } 154 | 155 | @Override 156 | public void onPageFinished(WebView view, String url) { 157 | super.onPageFinished(view, url); 158 | view.loadUrl("javascript:(function() { " + 159 | "window.WEBVIEW_SERVER_URL = '" + CDV_LOCAL_SERVER + "';" + 160 | "})()"); 161 | } 162 | } 163 | 164 | public void setServerBasePath(String path) { 165 | localServer.hostFiles(path); 166 | webView.loadUrl(CDV_LOCAL_SERVER); 167 | } 168 | 169 | public String getServerBasePath() { 170 | return this.localServer.getBasePath(); 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /src/android/com/ionicframework/cordova/webview/UriMatcher.java: -------------------------------------------------------------------------------- 1 | package com.ionicframework.cordova.webview; 2 | 3 | /* 4 | * Copyright (C) 2006 The Android Open Source Project 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | //package com.google.webviewlocalserver.third_party.android; 19 | 20 | import android.net.Uri; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | import java.util.regex.Pattern; 25 | 26 | public class UriMatcher { 27 | /** 28 | * Creates the root node of the URI tree. 29 | * 30 | * @param code the code to match for the root URI 31 | */ 32 | public UriMatcher(Object code) { 33 | mCode = code; 34 | mWhich = -1; 35 | mChildren = new ArrayList(); 36 | mText = null; 37 | } 38 | 39 | private UriMatcher() { 40 | mCode = null; 41 | mWhich = -1; 42 | mChildren = new ArrayList(); 43 | mText = null; 44 | } 45 | 46 | /** 47 | * Add a URI to match, and the code to return when this URI is 48 | * matched. URI nodes may be exact match string, the token "*" 49 | * that matches any text, or the token "#" that matches only 50 | * numbers. 51 | *

52 | * Starting from API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}, 53 | * this method will accept a leading slash in the path. 54 | * 55 | * @param authority the authority to match 56 | * @param path the path to match. * may be used as a wild card for 57 | * any text, and # may be used as a wild card for numbers. 58 | * @param code the code that is returned when a URI is matched 59 | * against the given components. Must be positive. 60 | */ 61 | public void addURI(String scheme, String authority, String path, Object code) { 62 | if (code == null) { 63 | throw new IllegalArgumentException("Code can't be null"); 64 | } 65 | 66 | String[] tokens = null; 67 | if (path != null) { 68 | String newPath = path; 69 | // Strip leading slash if present. 70 | if (path.length() > 0 && path.charAt(0) == '/') { 71 | newPath = path.substring(1); 72 | } 73 | tokens = PATH_SPLIT_PATTERN.split(newPath); 74 | } 75 | 76 | int numTokens = tokens != null ? tokens.length : 0; 77 | UriMatcher node = this; 78 | for (int i = -2; i < numTokens; i++) { 79 | String token; 80 | if (i == -2) 81 | token = scheme; 82 | else if (i == -1) 83 | token = authority; 84 | else 85 | token = tokens[i]; 86 | ArrayList children = node.mChildren; 87 | int numChildren = children.size(); 88 | UriMatcher child; 89 | int j; 90 | for (j = 0; j < numChildren; j++) { 91 | child = children.get(j); 92 | if (token.equals(child.mText)) { 93 | node = child; 94 | break; 95 | } 96 | } 97 | if (j == numChildren) { 98 | // Child not found, create it 99 | child = new UriMatcher(); 100 | if (token.equals("**")) { 101 | child.mWhich = REST; 102 | } else if (token.equals("*")) { 103 | child.mWhich = TEXT; 104 | } else { 105 | child.mWhich = EXACT; 106 | } 107 | child.mText = token; 108 | node.mChildren.add(child); 109 | node = child; 110 | } 111 | } 112 | node.mCode = code; 113 | } 114 | 115 | static final Pattern PATH_SPLIT_PATTERN = Pattern.compile("/"); 116 | 117 | /** 118 | * Try to match against the path in a url. 119 | * 120 | * @param uri The url whose path we will match against. 121 | * @return The code for the matched node (added using addURI), 122 | * or null if there is no matched node. 123 | */ 124 | public Object match(Uri uri) { 125 | final List pathSegments = uri.getPathSegments(); 126 | final int li = pathSegments.size(); 127 | 128 | UriMatcher node = this; 129 | 130 | if (li == 0 && uri.getAuthority() == null) { 131 | return this.mCode; 132 | } 133 | 134 | for (int i = -2; i < li; i++) { 135 | String u; 136 | if (i == -2) 137 | u = uri.getScheme(); 138 | else if (i == -1) 139 | u = uri.getAuthority(); 140 | else 141 | u = pathSegments.get(i); 142 | ArrayList list = node.mChildren; 143 | if (list == null) { 144 | break; 145 | } 146 | node = null; 147 | int lj = list.size(); 148 | for (int j = 0; j < lj; j++) { 149 | UriMatcher n = list.get(j); 150 | which_switch: 151 | switch (n.mWhich) { 152 | case EXACT: 153 | if (n.mText.equals(u)) { 154 | node = n; 155 | } 156 | break; 157 | case TEXT: 158 | node = n; 159 | break; 160 | case REST: 161 | return n.mCode; 162 | } 163 | if (node != null) { 164 | break; 165 | } 166 | } 167 | if (node == null) { 168 | return null; 169 | } 170 | } 171 | 172 | return node.mCode; 173 | } 174 | 175 | private static final int EXACT = 0; 176 | private static final int TEXT = 1; 177 | private static final int REST = 2; 178 | 179 | private Object mCode; 180 | private int mWhich; 181 | private String mText; 182 | private ArrayList mChildren; 183 | } 184 | -------------------------------------------------------------------------------- /src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2015 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | package com.ionicframework.cordova.webview; 17 | 18 | import android.content.Context; 19 | import android.net.Uri; 20 | import android.os.Build; 21 | import android.util.Log; 22 | import android.webkit.WebResourceRequest; 23 | import android.webkit.WebResourceResponse; 24 | 25 | import org.apache.cordova.ConfigXmlParser; 26 | 27 | import java.io.IOException; 28 | import java.io.InputStream; 29 | import java.net.HttpURLConnection; 30 | import java.net.SocketTimeoutException; 31 | import java.net.URL; 32 | import java.net.URLConnection; 33 | import java.util.HashMap; 34 | import java.util.Map; 35 | import java.util.UUID; 36 | 37 | /** 38 | * Helper class meant to be used with the android.webkit.WebView class to enable hosting assets, 39 | * resources and other data on 'virtual' http(s):// URL. 40 | * Hosting assets and resources on http(s):// URLs is desirable as it is compatible with the 41 | * Same-Origin policy. 42 | *

43 | * This class is intended to be used from within the 44 | * {@link android.webkit.WebViewClient#shouldInterceptRequest(android.webkit.WebView, String)} and 45 | * {@link android.webkit.WebViewClient#shouldInterceptRequest(android.webkit.WebView, 46 | * android.webkit.WebResourceRequest)} 47 | * methods. 48 | */ 49 | public class WebViewLocalServer { 50 | private static String TAG = "WebViewAssetServer"; 51 | private String basePath; 52 | public final static String httpScheme = "http"; 53 | public final static String httpsScheme = "https"; 54 | public final static String fileStart = "/_app_file_"; 55 | public final static String contentStart = "/_app_content_"; 56 | 57 | private final UriMatcher uriMatcher; 58 | private final AndroidProtocolHandler protocolHandler; 59 | private final String authority; 60 | private final String customScheme; 61 | // Whether we're serving local files or proxying (for example, when doing livereload on a 62 | // non-local endpoint (will be false in that case) 63 | private boolean isAsset; 64 | // Whether to route all requests to paths without extensions back to `index.html` 65 | private final boolean html5mode; 66 | private ConfigXmlParser parser; 67 | 68 | public String getAuthority() { return authority; } 69 | 70 | /** 71 | * A handler that produces responses for paths on the virtual asset server. 72 | *

73 | * Methods of this handler will be invoked on a background thread and care must be taken to 74 | * correctly synchronize access to any shared state. 75 | *

76 | * On Android KitKat and above these methods may be called on more than one thread. This thread 77 | * may be different than the thread on which the shouldInterceptRequest method was invoke. 78 | * This means that on Android KitKat and above it is possible to block in this method without 79 | * blocking other resources from loading. The number of threads used to parallelize loading 80 | * is an internal implementation detail of the WebView and may change between updates which 81 | * means that the amount of time spend blocking in this method should be kept to an absolute 82 | * minimum. 83 | */ 84 | public abstract static class PathHandler { 85 | protected String mimeType; 86 | private String encoding; 87 | private String charset; 88 | private int statusCode; 89 | private String reasonPhrase; 90 | private Map responseHeaders; 91 | 92 | public PathHandler() { 93 | this(null, null, 200, "OK", null); 94 | } 95 | 96 | public PathHandler(String encoding, String charset, int statusCode, 97 | String reasonPhrase, Map responseHeaders) { 98 | this.encoding = encoding; 99 | this.charset = charset; 100 | this.statusCode = statusCode; 101 | this.reasonPhrase = reasonPhrase; 102 | Map tempResponseHeaders; 103 | if (responseHeaders == null) { 104 | tempResponseHeaders = new HashMap(); 105 | } else { 106 | tempResponseHeaders = responseHeaders; 107 | } 108 | tempResponseHeaders.put("Cache-Control", "no-cache"); 109 | this.responseHeaders = tempResponseHeaders; 110 | } 111 | 112 | abstract public InputStream handle(Uri url); 113 | 114 | public String getEncoding() { 115 | return encoding; 116 | } 117 | 118 | public String getCharset() { 119 | return charset; 120 | } 121 | 122 | public int getStatusCode() { 123 | return statusCode; 124 | } 125 | 126 | public String getReasonPhrase() { 127 | return reasonPhrase; 128 | } 129 | 130 | public Map getResponseHeaders() { 131 | return responseHeaders; 132 | } 133 | } 134 | 135 | /** 136 | * Information about the URLs used to host the assets in the WebView. 137 | */ 138 | public static class AssetHostingDetails { 139 | private Uri httpPrefix; 140 | private Uri httpsPrefix; 141 | 142 | /*package*/ AssetHostingDetails(Uri httpPrefix, Uri httpsPrefix) { 143 | this.httpPrefix = httpPrefix; 144 | this.httpsPrefix = httpsPrefix; 145 | } 146 | 147 | /** 148 | * Gets the http: scheme prefix at which assets are hosted. 149 | * 150 | * @return the http: scheme prefix at which assets are hosted. Can return null. 151 | */ 152 | public Uri getHttpPrefix() { 153 | return httpPrefix; 154 | } 155 | 156 | /** 157 | * Gets the https: scheme prefix at which assets are hosted. 158 | * 159 | * @return the https: scheme prefix at which assets are hosted. Can return null. 160 | */ 161 | public Uri getHttpsPrefix() { 162 | return httpsPrefix; 163 | } 164 | } 165 | 166 | WebViewLocalServer(Context context, String authority, boolean html5mode, ConfigXmlParser parser, String customScheme) { 167 | uriMatcher = new UriMatcher(null); 168 | this.html5mode = html5mode; 169 | this.parser = parser; 170 | this.protocolHandler = new AndroidProtocolHandler(context.getApplicationContext()); 171 | this.authority = authority; 172 | this.customScheme = customScheme; 173 | } 174 | 175 | private static Uri parseAndVerifyUrl(String url) { 176 | if (url == null) { 177 | return null; 178 | } 179 | Uri uri = Uri.parse(url); 180 | if (uri == null) { 181 | Log.e(TAG, "Malformed URL: " + url); 182 | return null; 183 | } 184 | String path = uri.getPath(); 185 | if (path == null || path.length() == 0) { 186 | Log.e(TAG, "URL does not have a path: " + url); 187 | return null; 188 | } 189 | return uri; 190 | } 191 | 192 | private static WebResourceResponse createWebResourceResponse(String mimeType, String encoding, int statusCode, String reasonPhrase, Map responseHeaders, InputStream data) { 193 | if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 194 | int finalStatusCode = statusCode; 195 | try { 196 | if (data.available() == 0) { 197 | finalStatusCode = 404; 198 | } 199 | } catch (IOException e) { 200 | finalStatusCode = 500; 201 | } 202 | return new WebResourceResponse(mimeType, encoding, finalStatusCode, reasonPhrase, responseHeaders, data); 203 | } else { 204 | return new WebResourceResponse(mimeType, encoding, data); 205 | } 206 | } 207 | 208 | /** 209 | * Attempt to retrieve the WebResourceResponse associated with the given request. 210 | * This method should be invoked from within 211 | * {@link android.webkit.WebViewClient#shouldInterceptRequest(android.webkit.WebView, 212 | * android.webkit.WebResourceRequest)}. 213 | * 214 | * @param uri the request Uri to process. 215 | * @return a response if the request URL had a matching handler, null if no handler was found. 216 | */ 217 | public WebResourceResponse shouldInterceptRequest(Uri uri, WebResourceRequest request) { 218 | PathHandler handler; 219 | synchronized (uriMatcher) { 220 | handler = (PathHandler) uriMatcher.match(uri); 221 | } 222 | if (handler == null) { 223 | return null; 224 | } 225 | 226 | if (isLocalFile(uri) || uri.getAuthority().equals(this.authority)) { 227 | Log.d("SERVER", "Handling local request: " + uri.toString()); 228 | return handleLocalRequest(uri, handler, request); 229 | } else { 230 | return handleProxyRequest(uri, handler); 231 | } 232 | } 233 | 234 | private boolean isLocalFile(Uri uri) { 235 | String path = uri.getPath(); 236 | if (path.startsWith(contentStart) || path.startsWith(fileStart)) { 237 | return true; 238 | } 239 | return false; 240 | } 241 | 242 | 243 | private WebResourceResponse handleLocalRequest(Uri uri, PathHandler handler, WebResourceRequest request) { 244 | String path = uri.getPath(); 245 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && request != null && request.getRequestHeaders().get("Range") != null) { 246 | InputStream responseStream = new LollipopLazyInputStream(handler, uri); 247 | String mimeType = getMimeType(path, responseStream); 248 | Map tempResponseHeaders = handler.getResponseHeaders(); 249 | int statusCode = 206; 250 | try { 251 | int totalRange = responseStream.available(); 252 | String rangeString = request.getRequestHeaders().get("Range"); 253 | String[] parts = rangeString.split("="); 254 | String[] streamParts = parts[1].split("-"); 255 | String fromRange = streamParts[0]; 256 | int range = totalRange-1; 257 | if (streamParts.length > 1) { 258 | range = Integer.parseInt(streamParts[1]); 259 | } 260 | tempResponseHeaders.put("Accept-Ranges", "bytes"); 261 | tempResponseHeaders.put("Content-Range", "bytes " + fromRange + "-" + range + "/" + totalRange); 262 | } catch (IOException e) { 263 | statusCode = 404; 264 | } 265 | return createWebResourceResponse(mimeType, handler.getEncoding(), 266 | statusCode, handler.getReasonPhrase(), tempResponseHeaders, responseStream); 267 | } 268 | if (isLocalFile(uri)) { 269 | InputStream responseStream = new LollipopLazyInputStream(handler, uri); 270 | String mimeType = getMimeType(path, responseStream); 271 | return createWebResourceResponse(mimeType, handler.getEncoding(), 272 | handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), responseStream); 273 | } 274 | 275 | if (path.equals("") || path.equals("/") || (!uri.getLastPathSegment().contains(".") && html5mode)) { 276 | InputStream stream; 277 | String launchURL = parser.getLaunchUrl(); 278 | String launchFile = launchURL.substring(launchURL.lastIndexOf("/") + 1, launchURL.length()); 279 | try { 280 | String startPath = this.basePath + "/" + launchFile; 281 | if (isAsset) { 282 | stream = protocolHandler.openAsset(startPath); 283 | } else { 284 | stream = protocolHandler.openFile(startPath); 285 | } 286 | 287 | } catch (IOException e) { 288 | Log.e(TAG, "Unable to open " + launchFile, e); 289 | return null; 290 | } 291 | 292 | return createWebResourceResponse("text/html", handler.getEncoding(), 293 | handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), stream); 294 | } 295 | 296 | int periodIndex = path.lastIndexOf("."); 297 | if (periodIndex >= 0) { 298 | InputStream responseStream = new LollipopLazyInputStream(handler, uri); 299 | String mimeType = getMimeType(path, responseStream); 300 | return createWebResourceResponse(mimeType, handler.getEncoding(), 301 | handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), responseStream); 302 | } 303 | 304 | return null; 305 | } 306 | 307 | /** 308 | * Instead of reading files from the filesystem/assets, proxy through to the URL 309 | * and let an external server handle it. 310 | * @param uri 311 | * @param handler 312 | * @return 313 | */ 314 | private WebResourceResponse handleProxyRequest(Uri uri, PathHandler handler) { 315 | try { 316 | String path = uri.getPath(); 317 | URL url = new URL(uri.toString()); 318 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 319 | conn.setRequestMethod("GET"); 320 | conn.setReadTimeout(30 * 1000); 321 | conn.setConnectTimeout(30 * 1000); 322 | 323 | InputStream stream = conn.getInputStream(); 324 | 325 | if (path.equals("/") || (!uri.getLastPathSegment().contains(".") && html5mode)) { 326 | return createWebResourceResponse("text/html", handler.getEncoding(), 327 | handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), stream); 328 | } 329 | 330 | int periodIndex = path.lastIndexOf("."); 331 | if (periodIndex >= 0) { 332 | String ext = path.substring(path.lastIndexOf("."), path.length()); 333 | 334 | // TODO: Conjure up a bit more subtlety than this 335 | if (ext.equals(".html")) { 336 | } 337 | 338 | String mimeType = getMimeType(path, stream); 339 | 340 | return createWebResourceResponse(mimeType, handler.getEncoding(), 341 | handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), stream); 342 | } 343 | 344 | return createWebResourceResponse("", handler.getEncoding(), 345 | handler.getStatusCode(), handler.getReasonPhrase(), handler.getResponseHeaders(), stream); 346 | 347 | } catch (SocketTimeoutException ex) { 348 | // bridge.handleAppUrlLoadError(ex); 349 | } catch (Exception ex) { 350 | // bridge.handleAppUrlLoadError(ex); 351 | } 352 | return null; 353 | } 354 | 355 | private String getMimeType(String path, InputStream stream) { 356 | String mimeType = null; 357 | try { 358 | mimeType = URLConnection.guessContentTypeFromName(path); // Does not recognize *.js 359 | if (mimeType != null && path.endsWith(".js") && mimeType.equals("image/x-icon")) { 360 | Log.d(IonicWebViewEngine.TAG, "We shouldn't be here"); 361 | } 362 | if (mimeType == null) { 363 | if (path.endsWith(".js") || path.endsWith(".mjs")) { 364 | // Make sure JS files get the proper mimetype to support ES modules 365 | mimeType = "application/javascript"; 366 | } else if (path.endsWith(".wasm")) { 367 | mimeType = "application/wasm"; 368 | } else { 369 | mimeType = URLConnection.guessContentTypeFromStream(stream); 370 | } 371 | } 372 | } catch (Exception ex) { 373 | Log.e(TAG, "Unable to get mime type" + path, ex); 374 | } 375 | return mimeType; 376 | } 377 | 378 | /** 379 | * Registers a handler for the given uri. The handler will be invoked 380 | * every time the shouldInterceptRequest method of the instance is called with 381 | * a matching uri. 382 | * 383 | * @param uri the uri to use the handler for. The scheme and authority (domain) will be matched 384 | * exactly. The path may contain a '*' element which will match a single element of 385 | * a path (so a handler registered for /a/* will be invoked for /a/b and /a/c.html 386 | * but not for /a/b/b) or the '**' element which will match any number of path 387 | * elements. 388 | * @param handler the handler to use for the uri. 389 | */ 390 | void register(Uri uri, PathHandler handler) { 391 | synchronized (uriMatcher) { 392 | uriMatcher.addURI(uri.getScheme(), uri.getAuthority(), uri.getPath(), handler); 393 | } 394 | } 395 | 396 | /** 397 | * Hosts the application's assets on an http(s):// URL. Assets from the local path 398 | * assetPath/... will be available under 399 | * http(s)://{uuid}.androidplatform.net/assets/.... 400 | * 401 | * @param assetPath the local path in the application's asset folder which will be made 402 | * available by the server (for example "/www"). 403 | */ 404 | public void hostAssets(String assetPath) { 405 | hostAssets(authority, assetPath); 406 | } 407 | 408 | 409 | /** 410 | * Hosts the application's assets on an http(s):// URL. Assets from the local path 411 | * assetPath/... will be available under 412 | * http(s)://{domain}/{virtualAssetPath}/.... 413 | * 414 | * @param domain custom domain on which the assets should be hosted (for example "example.com"). 415 | * @param assetPath the local path in the application's asset folder which will be made 416 | * available by the server (for example "/www"). 417 | * @return prefixes under which the assets are hosted. 418 | */ 419 | public void hostAssets(final String domain, 420 | final String assetPath) { 421 | this.isAsset = true; 422 | this.basePath = assetPath; 423 | 424 | createHostingDetails(); 425 | } 426 | 427 | private void createHostingDetails() { 428 | final String assetPath = this.basePath; 429 | 430 | if (assetPath.indexOf('*') != -1) { 431 | throw new IllegalArgumentException("assetPath cannot contain the '*' character."); 432 | } 433 | 434 | PathHandler handler = new PathHandler() { 435 | @Override 436 | public InputStream handle(Uri url) { 437 | InputStream stream = null; 438 | String path = url.getPath(); 439 | try { 440 | if (path.startsWith(contentStart)) { 441 | stream = protocolHandler.openContentUrl(url); 442 | } else if (path.startsWith(fileStart) || !isAsset) { 443 | if (!path.startsWith(fileStart)) { 444 | path = basePath + url.getPath(); 445 | } 446 | stream = protocolHandler.openFile(path); 447 | } else { 448 | stream = protocolHandler.openAsset(assetPath + path); 449 | } 450 | } catch (IOException e) { 451 | Log.e(TAG, "Unable to open asset URL: " + url); 452 | return null; 453 | } 454 | 455 | return stream; 456 | } 457 | }; 458 | 459 | registerUriForScheme(httpScheme, handler, authority); 460 | registerUriForScheme(httpsScheme, handler, authority); 461 | if (!customScheme.equals(httpScheme) && !customScheme.equals(httpsScheme)) { 462 | registerUriForScheme(customScheme, handler, authority); 463 | } 464 | 465 | } 466 | 467 | private void registerUriForScheme(String scheme, PathHandler handler, String authority) { 468 | Uri.Builder uriBuilder = new Uri.Builder(); 469 | uriBuilder.scheme(scheme); 470 | uriBuilder.authority(authority); 471 | uriBuilder.path(""); 472 | Uri uriPrefix = uriBuilder.build(); 473 | 474 | register(Uri.withAppendedPath(uriPrefix, "/"), handler); 475 | register(Uri.withAppendedPath(uriPrefix, "**"), handler); 476 | } 477 | 478 | /** 479 | * Hosts the application's resources on an http(s):// URL. Resources 480 | * http(s)://{uuid}.androidplatform.net/res/{resource_type}/{resource_name}. 481 | * 482 | * @return prefixes under which the resources are hosted. 483 | */ 484 | public AssetHostingDetails hostResources() { 485 | return hostResources(authority, "/res", true, true); 486 | } 487 | 488 | /** 489 | * Hosts the application's resources on an http(s):// URL. Resources 490 | * http(s)://{uuid}.androidplatform.net/{virtualResourcesPath}/{resource_type}/{resource_name}. 491 | * 492 | * @param virtualResourcesPath the path on the local server under which the resources 493 | * should be hosted. 494 | * @param enableHttp whether to enable hosting using the http scheme. 495 | * @param enableHttps whether to enable hosting using the https scheme. 496 | * @return prefixes under which the resources are hosted. 497 | */ 498 | public AssetHostingDetails hostResources(final String virtualResourcesPath, boolean enableHttp, 499 | boolean enableHttps) { 500 | return hostResources(authority, virtualResourcesPath, enableHttp, enableHttps); 501 | } 502 | 503 | /** 504 | * Hosts the application's resources on an http(s):// URL. Resources 505 | * http(s)://{domain}/{virtualResourcesPath}/{resource_type}/{resource_name}. 506 | * 507 | * @param domain custom domain on which the assets should be hosted (for example "example.com"). 508 | * If untrusted content is to be loaded into the WebView it is advised to make 509 | * this random. 510 | * @param virtualResourcesPath the path on the local server under which the resources 511 | * should be hosted. 512 | * @param enableHttp whether to enable hosting using the http scheme. 513 | * @param enableHttps whether to enable hosting using the https scheme. 514 | * @return prefixes under which the resources are hosted. 515 | */ 516 | public AssetHostingDetails hostResources(final String domain, 517 | final String virtualResourcesPath, boolean enableHttp, 518 | boolean enableHttps) { 519 | if (virtualResourcesPath.indexOf('*') != -1) { 520 | throw new IllegalArgumentException( 521 | "virtualResourcesPath cannot contain the '*' character."); 522 | } 523 | 524 | Uri.Builder uriBuilder = new Uri.Builder(); 525 | uriBuilder.scheme(httpScheme); 526 | uriBuilder.authority(domain); 527 | uriBuilder.path(virtualResourcesPath); 528 | 529 | Uri httpPrefix = null; 530 | Uri httpsPrefix = null; 531 | 532 | PathHandler handler = new PathHandler() { 533 | @Override 534 | public InputStream handle(Uri url) { 535 | InputStream stream = protocolHandler.openResource(url); 536 | String mimeType = null; 537 | try { 538 | mimeType = URLConnection.guessContentTypeFromStream(stream); 539 | } catch (Exception ex) { 540 | Log.e(TAG, "Unable to get mime type" + url); 541 | } 542 | 543 | return stream; 544 | } 545 | }; 546 | 547 | if (enableHttp) { 548 | httpPrefix = uriBuilder.build(); 549 | register(Uri.withAppendedPath(httpPrefix, "**"), handler); 550 | } 551 | if (enableHttps) { 552 | uriBuilder.scheme(httpsScheme); 553 | httpsPrefix = uriBuilder.build(); 554 | register(Uri.withAppendedPath(httpsPrefix, "**"), handler); 555 | } 556 | return new AssetHostingDetails(httpPrefix, httpsPrefix); 557 | } 558 | 559 | 560 | /** 561 | * Hosts the application's files on an http(s):// URL. Files from the basePath 562 | * basePath/... will be available under 563 | * http(s)://{uuid}.androidplatform.net/.... 564 | * 565 | * @param basePath the local path in the application's data folder which will be made 566 | * available by the server (for example "/www"). 567 | */ 568 | public void hostFiles(final String basePath) { 569 | this.isAsset = false; 570 | this.basePath = basePath; 571 | createHostingDetails(); 572 | } 573 | 574 | /** 575 | * The KitKat WebView reads the InputStream on a separate threadpool. We can use that to 576 | * parallelize loading. 577 | */ 578 | private static abstract class LazyInputStream extends InputStream { 579 | protected final PathHandler handler; 580 | private InputStream is = null; 581 | 582 | public LazyInputStream(PathHandler handler) { 583 | this.handler = handler; 584 | } 585 | 586 | private InputStream getInputStream() { 587 | if (is == null) { 588 | is = handle(); 589 | } 590 | return is; 591 | } 592 | 593 | protected abstract InputStream handle(); 594 | 595 | @Override 596 | public int available() throws IOException { 597 | InputStream is = getInputStream(); 598 | return (is != null) ? is.available() : 0; 599 | } 600 | 601 | @Override 602 | public int read() throws IOException { 603 | InputStream is = getInputStream(); 604 | return (is != null) ? is.read() : -1; 605 | } 606 | 607 | @Override 608 | public int read(byte b[]) throws IOException { 609 | InputStream is = getInputStream(); 610 | return (is != null) ? is.read(b) : -1; 611 | } 612 | 613 | @Override 614 | public int read(byte b[], int off, int len) throws IOException { 615 | InputStream is = getInputStream(); 616 | return (is != null) ? is.read(b, off, len) : -1; 617 | } 618 | 619 | @Override 620 | public long skip(long n) throws IOException { 621 | InputStream is = getInputStream(); 622 | return (is != null) ? is.skip(n) : 0; 623 | } 624 | } 625 | 626 | // For L and above. 627 | private static class LollipopLazyInputStream extends LazyInputStream { 628 | private Uri uri; 629 | private InputStream is; 630 | 631 | public LollipopLazyInputStream(PathHandler handler, Uri uri) { 632 | super(handler); 633 | this.uri = uri; 634 | } 635 | 636 | @Override 637 | protected InputStream handle() { 638 | return handler.handle(uri); 639 | } 640 | } 641 | 642 | public String getBasePath(){ 643 | return this.basePath; 644 | } 645 | } 646 | -------------------------------------------------------------------------------- /src/ios/CDVWKProcessPoolFactory.h: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | #import 21 | 22 | @interface CDVWKProcessPoolFactory : NSObject 23 | @property (nonatomic, retain) WKProcessPool* sharedPool; 24 | 25 | +(instancetype) sharedFactory; 26 | -(WKProcessPool*) sharedProcessPool; 27 | @end 28 | -------------------------------------------------------------------------------- /src/ios/CDVWKProcessPoolFactory.m: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | #import 21 | #import 22 | #import "CDVWKProcessPoolFactory.h" 23 | 24 | static CDVWKProcessPoolFactory *factory = nil; 25 | 26 | @implementation CDVWKProcessPoolFactory 27 | 28 | + (instancetype)sharedFactory 29 | { 30 | static dispatch_once_t onceToken; 31 | dispatch_once(&onceToken, ^{ 32 | factory = [[CDVWKProcessPoolFactory alloc] init]; 33 | }); 34 | 35 | return factory; 36 | } 37 | 38 | - (instancetype)init 39 | { 40 | if (self = [super init]) { 41 | _sharedPool = [[WKProcessPool alloc] init]; 42 | } 43 | return self; 44 | } 45 | 46 | - (WKProcessPool*) sharedProcessPool { 47 | return _sharedPool; 48 | } 49 | @end 50 | -------------------------------------------------------------------------------- /src/ios/CDVWKWebViewEngine.h: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | #import 21 | #import 22 | 23 | @interface CDVWKWebViewEngine : CDVPlugin 24 | 25 | @property (nonatomic, strong, readonly) id uiDelegate; 26 | @property (nonatomic, strong) NSString * basePath; 27 | 28 | -(void)setServerBasePath:(CDVInvokedUrlCommand*)command; 29 | -(void)getServerBasePath:(CDVInvokedUrlCommand*)command; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /src/ios/CDVWKWebViewEngine.m: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | #import 21 | #import 22 | #import 23 | #import 24 | #import 25 | 26 | #import "CDVWKWebViewEngine.h" 27 | #import "CDVWKWebViewUIDelegate.h" 28 | #import "CDVWKProcessPoolFactory.h" 29 | #import "IONAssetHandler.h" 30 | 31 | #define CDV_BRIDGE_NAME @"cordova" 32 | #define CDV_IONIC_STOP_SCROLL @"stopScroll" 33 | #define CDV_SERVER_PATH @"serverBasePath" 34 | #define LAST_BINARY_VERSION_CODE @"lastBinaryVersionCode" 35 | #define LAST_BINARY_VERSION_NAME @"lastBinaryVersionName" 36 | 37 | @implementation UIScrollView (BugIOS11) 38 | 39 | + (void)load { 40 | static dispatch_once_t onceToken; 41 | dispatch_once(&onceToken, ^{ 42 | Class class = [self class]; 43 | SEL originalSelector = @selector(init); 44 | SEL swizzledSelector = @selector(xxx_init); 45 | 46 | Method originalMethod = class_getInstanceMethod(class, originalSelector); 47 | Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); 48 | 49 | BOOL didAddMethod = 50 | class_addMethod(class, 51 | originalSelector, 52 | method_getImplementation(swizzledMethod), 53 | method_getTypeEncoding(swizzledMethod)); 54 | 55 | if (didAddMethod) { 56 | class_replaceMethod(class, 57 | swizzledSelector, 58 | method_getImplementation(originalMethod), 59 | method_getTypeEncoding(originalMethod)); 60 | } else { 61 | method_exchangeImplementations(originalMethod, swizzledMethod); 62 | } 63 | }); 64 | } 65 | 66 | #pragma mark - Method Swizzling 67 | 68 | - (id)xxx_init { 69 | id a = [self xxx_init]; 70 | NSArray *stack = [NSThread callStackSymbols]; 71 | for(NSString *trace in stack) { 72 | if([trace containsString:@"WebKit"]) { 73 | [a setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever]; 74 | break; 75 | } 76 | } 77 | return a; 78 | } 79 | 80 | @end 81 | 82 | 83 | @interface CDVWKWeakScriptMessageHandler : NSObject 84 | 85 | @property (nonatomic, weak, readonly) idscriptMessageHandler; 86 | 87 | - (instancetype)initWithScriptMessageHandler:(id)scriptMessageHandler; 88 | 89 | @end 90 | 91 | 92 | @interface CDVWKWebViewEngine () 93 | 94 | @property (nonatomic, strong, readwrite) UIView* engineWebView; 95 | @property (nonatomic, strong, readwrite) id uiDelegate; 96 | @property (nonatomic, weak) id weakScriptMessageHandler; 97 | @property (nonatomic, readwrite) CGRect frame; 98 | @property (nonatomic, strong) NSString *userAgentCreds; 99 | @property (nonatomic, strong) IONAssetHandler * handler; 100 | 101 | @property (nonatomic, readwrite) NSString *CDV_LOCAL_SERVER; 102 | @end 103 | 104 | // expose private configuration value required for background operation 105 | @interface WKWebViewConfiguration () 106 | 107 | @end 108 | 109 | 110 | // see forwardingTargetForSelector: selector comment for the reason for this pragma 111 | #pragma clang diagnostic ignored "-Wprotocol" 112 | 113 | @implementation CDVWKWebViewEngine 114 | 115 | @synthesize engineWebView = _engineWebView; 116 | 117 | - (instancetype)initWithFrame:(CGRect)frame 118 | { 119 | self = [super init]; 120 | if (self) { 121 | if (NSClassFromString(@"WKWebView") == nil) { 122 | return nil; 123 | } 124 | // add to keyWindow to ensure it is 'active' 125 | [UIApplication.sharedApplication.keyWindow addSubview:self.engineWebView]; 126 | 127 | self.frame = frame; 128 | } 129 | return self; 130 | } 131 | 132 | -(NSString *) getStartPath { 133 | NSString * wwwPath = [[NSBundle mainBundle] pathForResource:@"www" ofType: nil]; 134 | 135 | NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; 136 | NSString * persistedPath = [userDefaults objectForKey:CDV_SERVER_PATH]; 137 | if (![self isDeployDisabled] && ![self isNewBinary] && persistedPath && ![persistedPath isEqualToString:@""]) { 138 | NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 139 | NSString * cordovaDataDirectory = [libPath stringByAppendingPathComponent:@"NoCloud"]; 140 | NSString * snapshots = [cordovaDataDirectory stringByAppendingPathComponent:@"ionic_built_snapshots"]; 141 | wwwPath = [snapshots stringByAppendingPathComponent:[persistedPath lastPathComponent]]; 142 | } 143 | self.basePath = wwwPath; 144 | return wwwPath; 145 | } 146 | 147 | -(BOOL) isNewBinary 148 | { 149 | NSString * versionCode = [[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"]; 150 | NSString * versionName = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"]; 151 | NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults]; 152 | NSString * lastVersionCode = [prefs stringForKey:LAST_BINARY_VERSION_CODE]; 153 | NSString * lastVersionName = [prefs stringForKey:LAST_BINARY_VERSION_NAME]; 154 | if (![versionCode isEqualToString:lastVersionCode] || ![versionName isEqualToString:lastVersionName]) { 155 | [prefs setObject:versionCode forKey:LAST_BINARY_VERSION_CODE]; 156 | [prefs setObject:versionName forKey:LAST_BINARY_VERSION_NAME]; 157 | [prefs setObject:@"" forKey:CDV_SERVER_PATH]; 158 | [prefs synchronize]; 159 | return YES; 160 | } 161 | return NO; 162 | } 163 | 164 | -(BOOL) isDeployDisabled { 165 | return [[self.commandDelegate.settings objectForKey:[@"DisableDeploy" lowercaseString]] boolValue]; 166 | } 167 | 168 | - (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)settings 169 | { 170 | WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init]; 171 | configuration.processPool = [[CDVWKProcessPoolFactory sharedFactory] sharedProcessPool]; 172 | configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone; 173 | 174 | if (settings == nil) { 175 | return configuration; 176 | } 177 | 178 | if(![settings cordovaBoolSettingForKey:@"WKSuspendInBackground" defaultValue:YES]){ 179 | NSString* _BGStatus; 180 | if (@available(iOS 12.2, *)) { 181 | // do stuff for iOS 12.2 and newer 182 | NSLog(@"iOS 12.2+ detected"); 183 | NSString* str = @"YWx3YXlzUnVuc0F0Rm9yZWdyb3VuZFByaW9yaXR5"; 184 | NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0]; 185 | _BGStatus = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 186 | } else { 187 | // do stuff for iOS 12.1 and older 188 | NSLog(@"iOS Below 12.2 detected"); 189 | NSString* str = @"X2Fsd2F5c1J1bnNBdEZvcmVncm91bmRQcmlvcml0eQ=="; 190 | NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0]; 191 | _BGStatus = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 192 | } 193 | [configuration setValue:[NSNumber numberWithBool:YES] 194 | forKey:_BGStatus]; 195 | } 196 | NSString *userAgent = configuration.applicationNameForUserAgent; 197 | if ( 198 | [settings cordovaSettingForKey:@"OverrideUserAgent"] == nil && 199 | [settings cordovaSettingForKey:@"AppendUserAgent"] != nil 200 | ) { 201 | userAgent = [NSString stringWithFormat:@"%@ %@", userAgent, [settings cordovaSettingForKey:@"AppendUserAgent"]]; 202 | } 203 | configuration.applicationNameForUserAgent = userAgent; 204 | configuration.allowsInlineMediaPlayback = [settings cordovaBoolSettingForKey:@"AllowInlineMediaPlayback" defaultValue:YES]; 205 | configuration.suppressesIncrementalRendering = [settings cordovaBoolSettingForKey:@"SuppressesIncrementalRendering" defaultValue:NO]; 206 | configuration.allowsAirPlayForMediaPlayback = [settings cordovaBoolSettingForKey:@"MediaPlaybackAllowsAirPlay" defaultValue:YES]; 207 | return configuration; 208 | } 209 | 210 | - (void)pluginInitialize 211 | { 212 | // viewController would be available now. we attempt to set all possible delegates to it, by default 213 | NSDictionary* settings = self.commandDelegate.settings; 214 | NSString *bind = [settings cordovaSettingForKey:@"Hostname"]; 215 | if(bind == nil){ 216 | bind = @"localhost"; 217 | } 218 | NSString *scheme = [settings cordovaSettingForKey:@"iosScheme"]; 219 | if(scheme == nil || [scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"] || [scheme isEqualToString:@"file"]){ 220 | scheme = @"ionic"; 221 | } 222 | self.CDV_LOCAL_SERVER = [NSString stringWithFormat:@"%@://%@", scheme, bind]; 223 | 224 | self.uiDelegate = [[CDVWKWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]]; 225 | 226 | CDVWKWeakScriptMessageHandler *weakScriptMessageHandler = [[CDVWKWeakScriptMessageHandler alloc] initWithScriptMessageHandler:self]; 227 | 228 | WKUserContentController* userContentController = [[WKUserContentController alloc] init]; 229 | [userContentController addScriptMessageHandler:weakScriptMessageHandler name:CDV_BRIDGE_NAME]; 230 | [userContentController addScriptMessageHandler:weakScriptMessageHandler name:CDV_IONIC_STOP_SCROLL]; 231 | 232 | // Inject XHR Polyfill 233 | NSLog(@"CDVWKWebViewEngine: trying to inject XHR polyfill"); 234 | WKUserScript *wkScript = [self wkPluginScript]; 235 | if (wkScript) { 236 | [userContentController addUserScript:wkScript]; 237 | } 238 | 239 | WKUserScript *configScript = [self configScript]; 240 | if (configScript) { 241 | [userContentController addUserScript:configScript]; 242 | } 243 | 244 | BOOL autoCordova = [settings cordovaBoolSettingForKey:@"AutoInjectCordova" defaultValue:NO]; 245 | if (autoCordova){ 246 | NSLog(@"CDVWKWebViewEngine: trying to inject XHR polyfill"); 247 | WKUserScript *cordova = [self autoCordovify]; 248 | if (cordova) { 249 | [userContentController addUserScript:cordova]; 250 | } 251 | } 252 | 253 | BOOL audioCanMix = [settings cordovaBoolSettingForKey:@"AudioCanMix" defaultValue:NO]; 254 | if (audioCanMix) { 255 | [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord 256 | withOptions:AVAudioSessionCategoryOptionMixWithOthers 257 | error:nil]; 258 | } 259 | 260 | WKWebViewConfiguration* configuration = [self createConfigurationFromSettings:settings]; 261 | configuration.userContentController = userContentController; 262 | 263 | self.handler = [[IONAssetHandler alloc] initWithBasePath:[self getStartPath] andScheme:scheme]; 264 | [configuration setURLSchemeHandler:self.handler forURLScheme:scheme]; 265 | 266 | // re-create WKWebView, since we need to update configuration 267 | // remove from keyWindow before recreating 268 | [self.engineWebView removeFromSuperview]; 269 | WKWebView* wkWebView = [[WKWebView alloc] initWithFrame:self.frame configuration:configuration]; 270 | 271 | [wkWebView.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever]; 272 | 273 | wkWebView.UIDelegate = self.uiDelegate; 274 | self.engineWebView = wkWebView; 275 | // add to keyWindow to ensure it is 'active' 276 | [UIApplication.sharedApplication.keyWindow addSubview:self.engineWebView]; 277 | 278 | NSString * overrideUserAgent = [settings cordovaSettingForKey:@"OverrideUserAgent"]; 279 | if (overrideUserAgent != nil) { 280 | wkWebView.customUserAgent = overrideUserAgent; 281 | } 282 | 283 | if ([self.viewController conformsToProtocol:@protocol(WKUIDelegate)]) { 284 | wkWebView.UIDelegate = (id )self.viewController; 285 | } 286 | 287 | if ([self.viewController conformsToProtocol:@protocol(WKNavigationDelegate)]) { 288 | wkWebView.navigationDelegate = (id )self.viewController; 289 | } else { 290 | wkWebView.navigationDelegate = (id )self; 291 | } 292 | 293 | if ([self.viewController conformsToProtocol:@protocol(WKScriptMessageHandler)]) { 294 | [wkWebView.configuration.userContentController addScriptMessageHandler:(id < WKScriptMessageHandler >)self.viewController name:CDV_BRIDGE_NAME]; 295 | } 296 | 297 | [self keyboardDisplayDoesNotRequireUserAction]; 298 | 299 | if ([settings cordovaBoolSettingForKey:@"KeyboardAppearanceDark" defaultValue:NO]) { 300 | [self setKeyboardAppearanceDark]; 301 | } 302 | 303 | #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 160400 304 | // With the introduction of iOS 16.4 the webview is no longer inspectable by default. 305 | // We'll honor that change for release builds, but will still allow inspection on debug builds by default. 306 | // We also introduce an override option, so consumers can influence this decision in their own build. 307 | if (@available(iOS 16.4, *)) { 308 | #ifdef DEBUG 309 | BOOL allowWebviewInspectionDefault = YES; 310 | #else 311 | BOOL allowWebviewInspectionDefault = NO; 312 | #endif 313 | wkWebView.inspectable = [settings cordovaBoolSettingForKey:@"InspectableWebview" defaultValue:allowWebviewInspectionDefault]; 314 | } 315 | #endif 316 | [self updateSettings:settings]; 317 | 318 | // check if content thread has died on resume 319 | NSLog(@"%@", @"CDVWKWebViewEngine will reload WKWebView if required on resume"); 320 | [[NSNotificationCenter defaultCenter] 321 | addObserver:self 322 | selector:@selector(onAppWillEnterForeground:) 323 | name:UIApplicationWillEnterForegroundNotification object:nil]; 324 | 325 | // If less than ios 13.4 326 | if (@available(iOS 13.4, *)) {} else { 327 | // For keyboard dismissal leaving viewport shifted (can potentially be removed when apple releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885) 328 | // Apple has released a fix in 13.4, but not in 12.x (as of 12.4.6) 329 | [[NSNotificationCenter defaultCenter] 330 | addObserver:self 331 | selector:@selector(keyboardWillHide) 332 | name:UIKeyboardWillHideNotification object:nil]; 333 | } 334 | 335 | NSLog(@"Using Ionic WKWebView"); 336 | 337 | } 338 | 339 | // https://github.com/Telerik-Verified-Plugins/WKWebView/commit/04e8296adeb61f289f9c698045c19b62d080c7e3#L609-L620 340 | - (void) keyboardDisplayDoesNotRequireUserAction { 341 | Class class = NSClassFromString(@"WKContentView"); 342 | NSOperatingSystemVersion iOS_11_3_0 = (NSOperatingSystemVersion){11, 3, 0}; 343 | NSOperatingSystemVersion iOS_12_2_0 = (NSOperatingSystemVersion){12, 2, 0}; 344 | NSOperatingSystemVersion iOS_13_0_0 = (NSOperatingSystemVersion){13, 0, 0}; 345 | char * methodSignature = "_startAssistingNode:userIsInteracting:blurPreviousNode:changingActivityState:userObject:"; 346 | 347 | if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_13_0_0]) { 348 | methodSignature = "_elementDidFocus:userIsInteracting:blurPreviousNode:activityStateChanges:userObject:"; 349 | } else if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_12_2_0]) { 350 | methodSignature = "_elementDidFocus:userIsInteracting:blurPreviousNode:changingActivityState:userObject:"; 351 | } 352 | 353 | if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_11_3_0]) { 354 | SEL selector = sel_getUid(methodSignature); 355 | Method method = class_getInstanceMethod(class, selector); 356 | IMP original = method_getImplementation(method); 357 | IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, BOOL arg3, id arg4) { 358 | ((void (*)(id, SEL, void*, BOOL, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3, arg4); 359 | }); 360 | method_setImplementation(method, override); 361 | } else { 362 | SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:"); 363 | Method method = class_getInstanceMethod(class, selector); 364 | IMP original = method_getImplementation(method); 365 | IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, id arg3) { 366 | ((void (*)(id, SEL, void*, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3); 367 | }); 368 | method_setImplementation(method, override); 369 | } 370 | } 371 | 372 | - (void)setKeyboardAppearanceDark 373 | { 374 | IMP darkImp = imp_implementationWithBlock(^(id _s) { 375 | return UIKeyboardAppearanceDark; 376 | }); 377 | for (NSString* classString in @[@"WKContentView", @"UITextInputTraits"]) { 378 | Class c = NSClassFromString(classString); 379 | Method m = class_getInstanceMethod(c, @selector(keyboardAppearance)); 380 | if (m != NULL) { 381 | method_setImplementation(m, darkImp); 382 | } else { 383 | class_addMethod(c, @selector(keyboardAppearance), darkImp, "l@:"); 384 | } 385 | } 386 | } 387 | 388 | 389 | 390 | - (void)onAppWillEnterForeground:(NSNotification *)notification { 391 | if ([self shouldReloadWebView]) { 392 | NSLog(@"%@", @"CDVWKWebViewEngine reloading!"); 393 | [(WKWebView*)_engineWebView reload]; 394 | } 395 | } 396 | 397 | 398 | -(void)keyboardWillHide 399 | { 400 | // For keyboard dismissal leaving viewport shifted (can potentially be removed when apple releases the fix for the issue discussed here: https://github.com/apache/cordova-ios/issues/417#issuecomment-423340885) 401 | UIScrollView * scrollView = self.webView.scrollView; 402 | // Calculate some vars for convenience 403 | CGFloat contentLengthWithInsets = scrollView.contentSize.height + scrollView.adjustedContentInset.top + scrollView.adjustedContentInset.bottom; 404 | CGFloat contentOffsetY = scrollView.contentOffset.y; 405 | CGFloat screenHeight = scrollView.frame.size.height; 406 | CGFloat maxAllowedOffsetY = fmax(contentLengthWithInsets - screenHeight, 0); // 0 is for the case where content is shorter than screen 407 | 408 | // If the keyboard allowed the user to get to an offset beyond the max 409 | if (contentOffsetY > maxAllowedOffsetY) { 410 | // Reset the scroll to the max allowed so that there is no additional empty white space at the bottom where the keyboard occupied! 411 | CGPoint bottomOfPage = CGPointMake(scrollView.contentOffset.x, maxAllowedOffsetY); 412 | [scrollView setContentOffset:bottomOfPage]; 413 | } 414 | } 415 | 416 | - (BOOL)shouldReloadWebView 417 | { 418 | WKWebView* wkWebView = (WKWebView*)_engineWebView; 419 | return [self shouldReloadWebView:wkWebView.URL title:wkWebView.title]; 420 | } 421 | 422 | - (BOOL)shouldReloadWebView:(NSURL *)location title:(NSString*)title 423 | { 424 | BOOL title_is_nil = (title == nil); 425 | BOOL location_is_blank = [[location absoluteString] isEqualToString:@"about:blank"]; 426 | 427 | BOOL reload = (title_is_nil || location_is_blank); 428 | 429 | #ifdef DEBUG 430 | NSLog(@"%@", @"CDVWKWebViewEngine shouldReloadWebView::"); 431 | NSLog(@"CDVWKWebViewEngine shouldReloadWebView title: %@", title); 432 | NSLog(@"CDVWKWebViewEngine shouldReloadWebView location: %@", [location absoluteString]); 433 | NSLog(@"CDVWKWebViewEngine shouldReloadWebView reload: %u", reload); 434 | #endif 435 | 436 | return reload; 437 | } 438 | 439 | 440 | - (id)loadRequest:(NSURLRequest *)request 441 | { 442 | if (request.URL.fileURL) { 443 | NSURL* startURL = [NSURL URLWithString:((CDVViewController *)self.viewController).startPage]; 444 | NSString* startFilePath = [self.commandDelegate pathForResource:[startURL path]]; 445 | NSURL *url = [[NSURL URLWithString:self.CDV_LOCAL_SERVER] URLByAppendingPathComponent:request.URL.path]; 446 | if ([request.URL.path isEqualToString:startFilePath]) { 447 | url = [NSURL URLWithString:self.CDV_LOCAL_SERVER]; 448 | } 449 | if(request.URL.query) { 450 | url = [NSURL URLWithString:[@"?" stringByAppendingString:request.URL.query] relativeToURL:url]; 451 | } 452 | if(request.URL.fragment) { 453 | url = [NSURL URLWithString:[@"#" stringByAppendingString:request.URL.fragment] relativeToURL:url]; 454 | } 455 | request = [NSURLRequest requestWithURL:url]; 456 | } 457 | return [(WKWebView*)_engineWebView loadRequest:request]; 458 | } 459 | 460 | - (id)loadHTMLString:(NSString *)string baseURL:(NSURL*)baseURL 461 | { 462 | return [(WKWebView*)_engineWebView loadHTMLString:string baseURL:baseURL]; 463 | } 464 | 465 | - (NSURL*) URL 466 | { 467 | return [(WKWebView*)_engineWebView URL]; 468 | } 469 | 470 | - (BOOL)canLoadRequest:(NSURLRequest *)request 471 | { 472 | return TRUE; 473 | } 474 | 475 | - (void)updateSettings:(NSDictionary *)settings 476 | { 477 | WKWebView* wkWebView = (WKWebView *)_engineWebView; 478 | 479 | // By default, DisallowOverscroll is false (thus bounce is allowed) 480 | BOOL bounceAllowed = !([settings cordovaBoolSettingForKey:@"DisallowOverscroll" defaultValue:NO]); 481 | 482 | // prevent webView from bouncing 483 | if (!bounceAllowed) { 484 | if ([wkWebView respondsToSelector:@selector(scrollView)]) { 485 | ((UIScrollView*)[wkWebView scrollView]).bounces = NO; 486 | } else { 487 | for (id subview in wkWebView.subviews) { 488 | if ([[subview class] isSubclassOfClass:[UIScrollView class]]) { 489 | ((UIScrollView*)subview).bounces = NO; 490 | } 491 | } 492 | } 493 | } 494 | 495 | wkWebView.configuration.preferences.minimumFontSize = [settings cordovaFloatSettingForKey:@"MinimumFontSize" defaultValue:0.0]; 496 | wkWebView.allowsLinkPreview = [settings cordovaBoolSettingForKey:@"AllowLinkPreview" defaultValue:NO]; 497 | wkWebView.scrollView.scrollEnabled = [settings cordovaBoolSettingForKey:@"ScrollEnabled" defaultValue:NO]; 498 | wkWebView.allowsBackForwardNavigationGestures = [settings cordovaBoolSettingForKey:@"AllowBackForwardNavigationGestures" defaultValue:NO]; 499 | } 500 | 501 | - (void)updateWithInfo:(NSDictionary *)info 502 | { 503 | NSDictionary* scriptMessageHandlers = [info objectForKey:kCDVWebViewEngineScriptMessageHandlers]; 504 | NSDictionary* settings = [info objectForKey:kCDVWebViewEngineWebViewPreferences]; 505 | id navigationDelegate = [info objectForKey:kCDVWebViewEngineWKNavigationDelegate]; 506 | id uiDelegate = [info objectForKey:kCDVWebViewEngineWKUIDelegate]; 507 | 508 | WKWebView* wkWebView = (WKWebView*)_engineWebView; 509 | 510 | if (scriptMessageHandlers && [scriptMessageHandlers isKindOfClass:[NSDictionary class]]) { 511 | NSArray* allKeys = [scriptMessageHandlers allKeys]; 512 | 513 | for (NSString* key in allKeys) { 514 | id object = [scriptMessageHandlers objectForKey:key]; 515 | if ([object conformsToProtocol:@protocol(WKScriptMessageHandler)]) { 516 | [wkWebView.configuration.userContentController addScriptMessageHandler:object name:key]; 517 | } 518 | } 519 | } 520 | 521 | if (navigationDelegate && [navigationDelegate conformsToProtocol:@protocol(WKNavigationDelegate)]) { 522 | wkWebView.navigationDelegate = navigationDelegate; 523 | } 524 | 525 | if (uiDelegate && [uiDelegate conformsToProtocol:@protocol(WKUIDelegate)]) { 526 | wkWebView.UIDelegate = uiDelegate; 527 | } 528 | 529 | if (settings && [settings isKindOfClass:[NSDictionary class]]) { 530 | [self updateSettings:settings]; 531 | } 532 | } 533 | 534 | // This forwards the methods that are in the header that are not implemented here. 535 | // loadHTMLString:baseURL: 536 | // loadRequest: 537 | - (id)forwardingTargetForSelector:(SEL)aSelector 538 | { 539 | return _engineWebView; 540 | } 541 | 542 | - (UIView *)webView 543 | { 544 | return self.engineWebView; 545 | } 546 | 547 | - (WKUserScript *)wkPluginScript 548 | { 549 | NSString *scriptFile = [[NSBundle mainBundle] pathForResource:@"www/wk-plugin" ofType:@"js"]; 550 | if (scriptFile == nil) { 551 | NSLog(@"CDVWKWebViewEngine: WK plugin was not found"); 552 | return nil; 553 | } 554 | NSError *error = nil; 555 | NSString *source = [NSString stringWithContentsOfFile:scriptFile encoding:NSUTF8StringEncoding error:&error]; 556 | if (source == nil || error != nil) { 557 | NSLog(@"CDVWKWebViewEngine: WK plugin can not be loaded: %@", error); 558 | return nil; 559 | } 560 | source = [source stringByAppendingString:[NSString stringWithFormat:@"window.WEBVIEW_SERVER_URL = '%@';", self.CDV_LOCAL_SERVER]]; 561 | 562 | return [[WKUserScript alloc] initWithSource:source 563 | injectionTime:WKUserScriptInjectionTimeAtDocumentStart 564 | forMainFrameOnly:YES]; 565 | } 566 | 567 | - (WKUserScript *)configScript 568 | { 569 | Class keyboard = NSClassFromString(@"CDVIonicKeyboard"); 570 | BOOL keyboardPlugin = keyboard != nil; 571 | if(!keyboardPlugin) { 572 | return nil; 573 | } 574 | 575 | BOOL keyboardResizes = [self.commandDelegate.settings cordovaBoolSettingForKey:@"KeyboardResize" defaultValue:YES]; 576 | NSString *source = [NSString stringWithFormat: 577 | @"window.Ionic = window.Ionic || {};" 578 | @"window.Ionic.keyboardPlugin=true;" 579 | @"window.Ionic.keyboardResizes=%@", 580 | keyboardResizes ? @"true" : @"false"]; 581 | 582 | return [[WKUserScript alloc] initWithSource:source 583 | injectionTime:WKUserScriptInjectionTimeAtDocumentStart 584 | forMainFrameOnly:YES]; 585 | } 586 | 587 | - (WKUserScript *)autoCordovify 588 | { 589 | NSURL *cordovaURL = [[NSBundle mainBundle] URLForResource:@"www/cordova" withExtension:@"js"]; 590 | if (cordovaURL == nil) { 591 | NSLog(@"CDVWKWebViewEngine: cordova.js WAS NOT FOUND"); 592 | return nil; 593 | } 594 | NSError *error = nil; 595 | NSString *source = [NSString stringWithContentsOfURL:cordovaURL encoding:NSUTF8StringEncoding error:&error]; 596 | if (source == nil || error != nil) { 597 | NSLog(@"CDVWKWebViewEngine: cordova.js can not be loaded: %@", error); 598 | return nil; 599 | } 600 | NSLog(@"CDVWKWebViewEngine: auto injecting cordova"); 601 | NSString *cordovaPath = [self.CDV_LOCAL_SERVER stringByAppendingString:cordovaURL.URLByDeletingLastPathComponent.path]; 602 | NSString *replacement = [NSString stringWithFormat:@"var pathPrefix = '%@/';", cordovaPath]; 603 | source = [source stringByReplacingOccurrencesOfString:@"var pathPrefix = findCordovaPath();" withString:replacement]; 604 | 605 | return [[WKUserScript alloc] initWithSource:source 606 | injectionTime:WKUserScriptInjectionTimeAtDocumentStart 607 | forMainFrameOnly:YES]; 608 | } 609 | 610 | #pragma mark WKScriptMessageHandler implementation 611 | 612 | - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message 613 | { 614 | if ([message.name isEqualToString:CDV_BRIDGE_NAME]) { 615 | [self handleCordovaMessage: message]; 616 | } else if ([message.name isEqualToString:CDV_IONIC_STOP_SCROLL]) { 617 | [self handleStopScroll]; 618 | } 619 | } 620 | 621 | - (void)handleCordovaMessage:(WKScriptMessage*)message 622 | { 623 | CDVViewController *vc = (CDVViewController*)self.viewController; 624 | 625 | NSArray *jsonEntry = message.body; // NSString:callbackId, NSString:service, NSString:action, NSArray:args 626 | CDVInvokedUrlCommand* command = [CDVInvokedUrlCommand commandFromJson:jsonEntry]; 627 | CDV_EXEC_LOG(@"Exec(%@): Calling %@.%@", command.callbackId, command.className, command.methodName); 628 | 629 | if (![vc.commandQueue execute:command]) { 630 | #ifdef DEBUG 631 | NSError* error = nil; 632 | NSString* commandJson = nil; 633 | NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonEntry 634 | options:0 635 | error:&error]; 636 | 637 | if (error == nil) { 638 | commandJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 639 | } 640 | 641 | static NSUInteger maxLogLength = 1024; 642 | NSString* commandString = ([commandJson length] > maxLogLength) ? 643 | [NSString stringWithFormat : @"%@[...]", [commandJson substringToIndex:maxLogLength]] : 644 | commandJson; 645 | 646 | NSLog(@"FAILED pluginJSON = %@", commandString); 647 | #endif 648 | } 649 | } 650 | 651 | - (void)handleStopScroll 652 | { 653 | WKWebView* wkWebView = (WKWebView*)_engineWebView; 654 | NSLog(@"CDVWKWebViewEngine: handleStopScroll"); 655 | [self recursiveStopScroll:[wkWebView scrollView]]; 656 | [wkWebView evaluateJavaScript:@"window.IonicStopScroll.fire()" completionHandler:nil]; 657 | } 658 | 659 | - (void)recursiveStopScroll:(UIView *)node 660 | { 661 | if([node isKindOfClass: [UIScrollView class]]) { 662 | UIScrollView *nodeAsScroll = (UIScrollView *)node; 663 | 664 | if([nodeAsScroll isScrollEnabled] && ![nodeAsScroll isHidden]) { 665 | [nodeAsScroll setScrollEnabled: NO]; 666 | [nodeAsScroll setScrollEnabled: YES]; 667 | } 668 | } 669 | 670 | // iterate tree recursivelly 671 | for (UIView *child in [node subviews]) { 672 | [self recursiveStopScroll:child]; 673 | } 674 | } 675 | 676 | 677 | #pragma mark WKNavigationDelegate implementation 678 | 679 | - (void)webView:(WKWebView*)webView didStartProvisionalNavigation:(WKNavigation*)navigation 680 | { 681 | [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginResetNotification object:webView]]; 682 | } 683 | 684 | - (void)webView:(WKWebView*)webView didFinishNavigation:(WKNavigation*)navigation 685 | { 686 | #ifndef __CORDOVA_6_0_0 687 | CDVViewController* vc = (CDVViewController*)self.viewController; 688 | [CDVUserAgentUtil releaseLock:vc.userAgentLockToken]; 689 | #endif 690 | [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPageDidLoadNotification object:webView]]; 691 | } 692 | 693 | - (void)webView:(WKWebView*)theWebView didFailProvisionalNavigation:(WKNavigation*)navigation withError:(NSError*)error 694 | { 695 | [self webView:theWebView didFailNavigation:navigation withError:error]; 696 | } 697 | 698 | - (void)webView:(WKWebView*)theWebView didFailNavigation:(WKNavigation*)navigation withError:(NSError*)error 699 | { 700 | CDVViewController* vc = (CDVViewController*)self.viewController; 701 | #ifndef __CORDOVA_6_0_0 702 | [CDVUserAgentUtil releaseLock:vc.userAgentLockToken]; 703 | #endif 704 | 705 | NSString* message = [NSString stringWithFormat:@"Failed to load webpage with error: %@", [error localizedDescription]]; 706 | NSLog(@"%@", message); 707 | 708 | NSURL* errorUrl = vc.errorURL; 709 | if (errorUrl) { 710 | NSCharacterSet *charSet = [NSCharacterSet URLFragmentAllowedCharacterSet]; 711 | errorUrl = [NSURL URLWithString:[NSString stringWithFormat:@"?error=%@", [message stringByAddingPercentEncodingWithAllowedCharacters:charSet]] relativeToURL:errorUrl]; 712 | NSLog(@"%@", [errorUrl absoluteString]); 713 | [theWebView loadRequest:[NSURLRequest requestWithURL:errorUrl]]; 714 | } 715 | #ifdef DEBUG 716 | UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"] message:message preferredStyle:UIAlertControllerStyleAlert]; 717 | [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:nil]]; 718 | [vc presentViewController:alertController animated:YES completion:nil]; 719 | #endif 720 | } 721 | 722 | - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView 723 | { 724 | [webView reload]; 725 | } 726 | 727 | - (BOOL)defaultResourcePolicyForURL:(NSURL*)url 728 | { 729 | // all file:// urls are allowed 730 | if ([url isFileURL]) { 731 | return YES; 732 | } 733 | 734 | return NO; 735 | } 736 | 737 | - (void) webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler 738 | { 739 | NSURL* url = [navigationAction.request URL]; 740 | CDVViewController* vc = (CDVViewController*)self.viewController; 741 | 742 | /* 743 | * Give plugins the chance to handle the url 744 | */ 745 | BOOL anyPluginsResponded = NO; 746 | BOOL shouldAllowRequest = NO; 747 | 748 | for (NSString* pluginName in vc.pluginObjects) { 749 | CDVPlugin* plugin = [vc.pluginObjects objectForKey:pluginName]; 750 | SEL selector = NSSelectorFromString(@"shouldOverrideLoadWithRequest:navigationType:"); 751 | if ([plugin respondsToSelector:selector]) { 752 | anyPluginsResponded = YES; 753 | // https://issues.apache.org/jira/browse/CB-12497 754 | int navType = (int)navigationAction.navigationType; 755 | if (WKNavigationTypeOther == navigationAction.navigationType) { 756 | #ifdef __CORDOVA_6_0_0 757 | navType = -1; 758 | #else 759 | navType = 5; 760 | #endif 761 | } 762 | shouldAllowRequest = (((BOOL (*)(id, SEL, id, int))objc_msgSend)(plugin, selector, navigationAction.request, navType)); 763 | if (!shouldAllowRequest) { 764 | break; 765 | } 766 | } 767 | } 768 | 769 | if (!anyPluginsResponded) { 770 | /* 771 | * Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview. 772 | */ 773 | shouldAllowRequest = [self defaultResourcePolicyForURL:url]; 774 | if (!shouldAllowRequest) { 775 | [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]]; 776 | } 777 | } 778 | 779 | if (shouldAllowRequest) { 780 | NSString *scheme = url.scheme; 781 | if ([scheme isEqualToString:@"tel"] || 782 | [scheme isEqualToString:@"mailto"] || 783 | [scheme isEqualToString:@"facetime"] || 784 | [scheme isEqualToString:@"sms"] || 785 | [scheme isEqualToString:@"maps"]) { 786 | [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; 787 | decisionHandler(WKNavigationActionPolicyCancel); 788 | } else { 789 | decisionHandler(WKNavigationActionPolicyAllow); 790 | } 791 | } else { 792 | decisionHandler(WKNavigationActionPolicyCancel); 793 | } 794 | } 795 | 796 | -(void)getServerBasePath:(CDVInvokedUrlCommand*)command 797 | { 798 | [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:self.basePath] callbackId:command.callbackId]; 799 | } 800 | 801 | -(void)setServerBasePath:(CDVInvokedUrlCommand*)command 802 | { 803 | NSString * path = [command argumentAtIndex:0]; 804 | self.basePath = path; 805 | [self.handler setAssetPath:path]; 806 | 807 | NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.CDV_LOCAL_SERVER]]; 808 | [(WKWebView*)_engineWebView loadRequest:request]; 809 | } 810 | 811 | -(void)persistServerBasePath:(CDVInvokedUrlCommand*)command 812 | { 813 | NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; 814 | [userDefaults setObject:[self.basePath lastPathComponent] forKey:CDV_SERVER_PATH]; 815 | [userDefaults synchronize]; 816 | } 817 | 818 | @end 819 | 820 | #pragma mark - CDVWKWeakScriptMessageHandler 821 | 822 | @implementation CDVWKWeakScriptMessageHandler 823 | 824 | - (instancetype)initWithScriptMessageHandler:(id)scriptMessageHandler 825 | { 826 | self = [super init]; 827 | if (self) { 828 | _scriptMessageHandler = scriptMessageHandler; 829 | } 830 | return self; 831 | } 832 | 833 | - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message 834 | { 835 | [self.scriptMessageHandler userContentController:userContentController didReceiveScriptMessage:message]; 836 | } 837 | 838 | @end 839 | -------------------------------------------------------------------------------- /src/ios/CDVWKWebViewUIDelegate.h: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | #import 21 | 22 | @interface CDVWKWebViewUIDelegate : NSObject 23 | 24 | @property (nonatomic, copy) NSString* title; 25 | 26 | - (instancetype)initWithTitle:(NSString*)title; 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /src/ios/CDVWKWebViewUIDelegate.m: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | #import "CDVWKWebViewUIDelegate.h" 21 | 22 | @implementation CDVWKWebViewUIDelegate 23 | 24 | - (instancetype)initWithTitle:(NSString*)title 25 | { 26 | self = [super init]; 27 | if (self) { 28 | self.title = title; 29 | } 30 | 31 | return self; 32 | } 33 | 34 | - (void) webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSString*)message 35 | initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void (^)(void))completionHandler 36 | { 37 | UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title 38 | message:message 39 | preferredStyle:UIAlertControllerStyleAlert]; 40 | 41 | UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK") 42 | style:UIAlertActionStyleDefault 43 | handler:^(UIAlertAction* action) 44 | { 45 | completionHandler(); 46 | [alert dismissViewControllerAnimated:YES completion:nil]; 47 | }]; 48 | 49 | [alert addAction:ok]; 50 | 51 | UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController; 52 | 53 | [rootController presentViewController:alert animated:YES completion:nil]; 54 | } 55 | 56 | - (void) webView:(WKWebView*)webView runJavaScriptConfirmPanelWithMessage:(NSString*)message 57 | initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void (^)(BOOL result))completionHandler 58 | { 59 | UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title 60 | message:message 61 | preferredStyle:UIAlertControllerStyleAlert]; 62 | 63 | UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK") 64 | style:UIAlertActionStyleDefault 65 | handler:^(UIAlertAction* action) 66 | { 67 | completionHandler(YES); 68 | [alert dismissViewControllerAnimated:YES completion:nil]; 69 | }]; 70 | 71 | [alert addAction:ok]; 72 | 73 | UIAlertAction* cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel") 74 | style:UIAlertActionStyleDefault 75 | handler:^(UIAlertAction* action) 76 | { 77 | completionHandler(NO); 78 | [alert dismissViewControllerAnimated:YES completion:nil]; 79 | }]; 80 | [alert addAction:cancel]; 81 | 82 | UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController; 83 | 84 | [rootController presentViewController:alert animated:YES completion:nil]; 85 | } 86 | 87 | - (void) webView:(WKWebView*)webView runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt 88 | defaultText:(NSString*)defaultText initiatedByFrame:(WKFrameInfo*)frame 89 | completionHandler:(void (^)(NSString* result))completionHandler 90 | { 91 | UIAlertController* alert = [UIAlertController alertControllerWithTitle:self.title 92 | message:prompt 93 | preferredStyle:UIAlertControllerStyleAlert]; 94 | 95 | UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK") 96 | style:UIAlertActionStyleDefault 97 | handler:^(UIAlertAction* action) 98 | { 99 | completionHandler(((UITextField*)alert.textFields[0]).text); 100 | [alert dismissViewControllerAnimated:YES completion:nil]; 101 | }]; 102 | 103 | [alert addAction:ok]; 104 | 105 | UIAlertAction* cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel") 106 | style:UIAlertActionStyleDefault 107 | handler:^(UIAlertAction* action) 108 | { 109 | completionHandler(nil); 110 | [alert dismissViewControllerAnimated:YES completion:nil]; 111 | }]; 112 | [alert addAction:cancel]; 113 | 114 | [alert addTextFieldWithConfigurationHandler:^(UITextField* textField) { 115 | textField.text = defaultText; 116 | }]; 117 | 118 | UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController; 119 | 120 | [rootController presentViewController:alert animated:YES completion:nil]; 121 | } 122 | 123 | @end 124 | -------------------------------------------------------------------------------- /src/ios/IONAssetHandler.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import 3 | 4 | @interface IONAssetHandler : NSObject 5 | 6 | @property (nonatomic, strong) NSString * basePath; 7 | @property (nonatomic, strong) NSString * scheme; 8 | 9 | -(void)setAssetPath:(NSString *)assetPath; 10 | - (instancetype)initWithBasePath:(NSString *)basePath andScheme:(NSString *)scheme; 11 | 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /src/ios/IONAssetHandler.m: -------------------------------------------------------------------------------- 1 | #import "IONAssetHandler.h" 2 | #import 3 | #import "CDVWKWebViewEngine.h" 4 | 5 | @implementation IONAssetHandler 6 | 7 | -(void)setAssetPath:(NSString *)assetPath { 8 | self.basePath = assetPath; 9 | } 10 | 11 | - (instancetype)initWithBasePath:(NSString *)basePath andScheme:(NSString *)scheme { 12 | self = [super init]; 13 | if (self) { 14 | _basePath = basePath; 15 | _scheme = scheme; 16 | } 17 | return self; 18 | } 19 | 20 | - (void)webView:(WKWebView *)webView startURLSchemeTask:(id )urlSchemeTask 21 | { 22 | NSString * startPath = @""; 23 | NSURL * url = urlSchemeTask.request.URL; 24 | NSString * stringToLoad = url.path; 25 | NSString * scheme = url.scheme; 26 | 27 | if ([scheme isEqualToString:self.scheme]) { 28 | if ([stringToLoad hasPrefix:@"/_app_file_"]) { 29 | startPath = [stringToLoad stringByReplacingOccurrencesOfString:@"/_app_file_" withString:@""]; 30 | } else { 31 | startPath = self.basePath ? self.basePath : @""; 32 | if ([stringToLoad isEqualToString:@""] || [url.pathExtension isEqualToString:@""]) { 33 | startPath = [startPath stringByAppendingString:@"/index.html"]; 34 | } else { 35 | startPath = [startPath stringByAppendingString:stringToLoad]; 36 | } 37 | } 38 | } 39 | NSError * fileError = nil; 40 | NSData * data = nil; 41 | if ([self isMediaExtension:url.pathExtension]) { 42 | data = [NSData dataWithContentsOfFile:startPath options:NSDataReadingMappedIfSafe error:&fileError]; 43 | } 44 | if (!data || fileError) { 45 | data = [[NSData alloc] initWithContentsOfFile:startPath]; 46 | } 47 | NSInteger statusCode = 200; 48 | if (!data) { 49 | statusCode = 404; 50 | } 51 | NSURL * localUrl = [NSURL URLWithString:url.absoluteString]; 52 | NSString * mimeType = [self getMimeType:url.pathExtension]; 53 | id response = nil; 54 | if (data && [self isMediaExtension:url.pathExtension]) { 55 | response = [[NSURLResponse alloc] initWithURL:localUrl MIMEType:mimeType expectedContentLength:data.length textEncodingName:nil]; 56 | } else { 57 | NSDictionary * headers = @{ @"Content-Type" : mimeType, @"Cache-Control": @"no-cache"}; 58 | response = [[NSHTTPURLResponse alloc] initWithURL:localUrl statusCode:statusCode HTTPVersion:nil headerFields:headers]; 59 | } 60 | 61 | [urlSchemeTask didReceiveResponse:response]; 62 | [urlSchemeTask didReceiveData:data]; 63 | [urlSchemeTask didFinish]; 64 | 65 | } 66 | 67 | - (void)webView:(nonnull WKWebView *)webView stopURLSchemeTask:(nonnull id)urlSchemeTask 68 | { 69 | NSLog(@"stop"); 70 | } 71 | 72 | -(NSString *) getMimeType:(NSString *)fileExtension { 73 | if (fileExtension && ![fileExtension isEqualToString:@""]) { 74 | NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExtension, NULL); 75 | NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType); 76 | return contentType ? contentType : @"application/octet-stream"; 77 | } else { 78 | return @"text/html"; 79 | } 80 | } 81 | 82 | -(BOOL) isMediaExtension:(NSString *) pathExtension { 83 | NSArray * mediaExtensions = @[@"m4v", @"mov", @"mp4", 84 | @"aac", @"ac3", @"aiff", @"au", @"flac", @"m4a", @"mp3", @"wav"]; 85 | if ([mediaExtensions containsObject:pathExtension.lowercaseString]) { 86 | return YES; 87 | } 88 | return NO; 89 | } 90 | 91 | 92 | @end 93 | -------------------------------------------------------------------------------- /src/ios/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Niklas von Hertzen 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /src/ios/wk-plugin.js: -------------------------------------------------------------------------------- 1 | 2 | (function _wk_plugin() { 3 | // Check if we are running in WKWebView 4 | if (!window.webkit || !window.webkit.messageHandlers) { 5 | return; 6 | } 7 | 8 | // Initialize Ionic 9 | window.Ionic = window.Ionic || {}; 10 | 11 | var stopScrollHandler = window.webkit.messageHandlers.stopScroll; 12 | if (!stopScrollHandler) { 13 | console.error('Can not find stopScroll handler'); 14 | return; 15 | } 16 | 17 | var stopScrollFunc = null; 18 | var stopScroll = { 19 | stop: function stop(callback) { 20 | if (!stopScrollFunc) { 21 | stopScrollFunc = callback; 22 | stopScrollHandler.postMessage(''); 23 | } 24 | }, 25 | fire: function fire() { 26 | stopScrollFunc && stopScrollFunc(); 27 | stopScrollFunc = null; 28 | }, 29 | cancel: function cancel() { 30 | stopScrollFunc = null; 31 | } 32 | }; 33 | 34 | window.Ionic.StopScroll = stopScroll; 35 | // deprecated 36 | window.IonicStopScroll = stopScroll; 37 | 38 | console.debug("Ionic Stop Scroll injected!"); 39 | })(); 40 | -------------------------------------------------------------------------------- /src/www/ios/ios-wkwebview-exec.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one 4 | * or more contributor license agreements. See the NOTICE file 5 | * distributed with this work for additional information 6 | * regarding copyright ownership. The ASF licenses this file 7 | * to you under the Apache License, Version 2.0 (the 8 | * "License"); you may not use this file except in compliance 9 | * with the License. You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, 14 | * software distributed under the License is distributed on an 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | * KIND, either express or implied. See the License for the 17 | * specific language governing permissions and limitations 18 | * under the License. 19 | * 20 | */ 21 | 22 | /** 23 | * Creates the exec bridge used to notify the native code of 24 | * commands. 25 | */ 26 | var cordova = require('cordova'); 27 | var utils = require('cordova/utils'); 28 | var base64 = require('cordova/base64'); 29 | 30 | function massageArgsJsToNative (args) { 31 | if (!args || utils.typeName(args) !== 'Array') { 32 | return args; 33 | } 34 | var ret = []; 35 | args.forEach(function (arg, i) { 36 | if (utils.typeName(arg) === 'ArrayBuffer') { 37 | ret.push({ 38 | 'CDVType': 'ArrayBuffer', 39 | 'data': base64.fromArrayBuffer(arg) 40 | }); 41 | } else { 42 | ret.push(arg); 43 | } 44 | }); 45 | return ret; 46 | } 47 | 48 | function massageMessageNativeToJs (message) { 49 | if (message.CDVType === 'ArrayBuffer') { 50 | var stringToArrayBuffer = function (str) { 51 | var ret = new Uint8Array(str.length); 52 | for (var i = 0; i < str.length; i++) { 53 | ret[i] = str.charCodeAt(i); 54 | } 55 | return ret.buffer; 56 | }; 57 | var base64ToArrayBuffer = function (b64) { 58 | return stringToArrayBuffer(atob(b64)); // eslint-disable-line no-undef 59 | }; 60 | message = base64ToArrayBuffer(message.data); 61 | } 62 | return message; 63 | } 64 | 65 | function convertMessageToArgsNativeToJs (message) { 66 | var args = []; 67 | if (!message || !message.hasOwnProperty('CDVType')) { 68 | args.push(message); 69 | } else if (message.CDVType === 'MultiPart') { 70 | message.messages.forEach(function (e) { 71 | args.push(massageMessageNativeToJs(e)); 72 | }); 73 | } else { 74 | args.push(massageMessageNativeToJs(message)); 75 | } 76 | return args; 77 | } 78 | 79 | var iOSExec = function () { 80 | // detect change in bridge, if there is a change, we forward to new bridge 81 | 82 | // if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.cordova && window.webkit.messageHandlers.cordova.postMessage) { 83 | // bridgeMode = jsToNativeModes.WK_WEBVIEW_BINDING; 84 | // } 85 | 86 | var successCallback, failCallback, service, action, actionArgs; 87 | var callbackId = null; 88 | if (typeof arguments[0] !== 'string') { 89 | // FORMAT ONE 90 | successCallback = arguments[0]; 91 | failCallback = arguments[1]; 92 | service = arguments[2]; 93 | action = arguments[3]; 94 | actionArgs = arguments[4]; 95 | 96 | // Since we need to maintain backwards compatibility, we have to pass 97 | // an invalid callbackId even if no callback was provided since plugins 98 | // will be expecting it. The Cordova.exec() implementation allocates 99 | // an invalid callbackId and passes it even if no callbacks were given. 100 | callbackId = 'INVALID'; 101 | } else { 102 | throw new Error('The old format of this exec call has been removed (deprecated since 2.1). Change to: ' + // eslint-disable-line 103 | 'cordova.exec(null, null, \'Service\', \'action\', [ arg1, arg2 ]);'); 104 | } 105 | 106 | // If actionArgs is not provided, default to an empty array 107 | actionArgs = actionArgs || []; 108 | 109 | // Register the callbacks and add the callbackId to the positional 110 | // arguments if given. 111 | if (successCallback || failCallback) { 112 | callbackId = service + cordova.callbackId++; 113 | cordova.callbacks[callbackId] = 114 | {success: successCallback, fail: failCallback}; 115 | } 116 | 117 | actionArgs = massageArgsJsToNative(actionArgs); 118 | 119 | // CB-10133 DataClone DOM Exception 25 guard (fast function remover) 120 | var command = [callbackId, service, action, JSON.parse(JSON.stringify(actionArgs))]; 121 | window.webkit.messageHandlers.cordova.postMessage(command); 122 | }; 123 | 124 | iOSExec.nativeCallback = function (callbackId, status, message, keepCallback, debug) { 125 | var success = status === 0 || status === 1; 126 | var args = convertMessageToArgsNativeToJs(message); 127 | Promise.resolve().then(function () { 128 | cordova.callbackFromNative(callbackId, success, status, args, keepCallback); // eslint-disable-line 129 | }); 130 | }; 131 | 132 | // for backwards compatibility 133 | iOSExec.nativeEvalAndFetch = function (func) { 134 | try { 135 | func(); 136 | } catch (e) { 137 | console.log(e); 138 | } 139 | }; 140 | 141 | // Proxy the exec for bridge changes. See CB-10106 142 | 143 | function cordovaExec () { 144 | var cexec = require('cordova/exec'); 145 | var cexec_valid = (typeof cexec.nativeFetchMessages === 'function') && (typeof cexec.nativeEvalAndFetch === 'function') && (typeof cexec.nativeCallback === 'function'); 146 | return (cexec_valid && execProxy !== cexec) ? cexec : iOSExec; 147 | } 148 | 149 | function execProxy () { 150 | cordovaExec().apply(null, arguments); 151 | } 152 | 153 | execProxy.nativeFetchMessages = function () { 154 | return cordovaExec().nativeFetchMessages.apply(null, arguments); 155 | }; 156 | 157 | execProxy.nativeEvalAndFetch = function () { 158 | return cordovaExec().nativeEvalAndFetch.apply(null, arguments); 159 | }; 160 | 161 | execProxy.nativeCallback = function () { 162 | return cordovaExec().nativeCallback.apply(null, arguments); 163 | }; 164 | 165 | module.exports = execProxy; 166 | 167 | if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.cordova && window.webkit.messageHandlers.cordova.postMessage) { 168 | // unregister the old bridge 169 | cordova.define.remove('cordova/exec'); 170 | // redefine bridge to our new bridge 171 | cordova.define('cordova/exec', function (require, exports, module) { 172 | module.exports = execProxy; 173 | }); 174 | } 175 | -------------------------------------------------------------------------------- /src/www/util.js: -------------------------------------------------------------------------------- 1 | var exec = require('cordova/exec'); 2 | 3 | var WebView = { 4 | convertFileSrc: function(url) { 5 | if (!url) { 6 | return url; 7 | } 8 | if (url.indexOf('/')===0) { 9 | return window.WEBVIEW_SERVER_URL + '/_app_file_' + url; 10 | } 11 | if (url.indexOf('file://')===0) { 12 | return window.WEBVIEW_SERVER_URL + url.replace('file://', '/_app_file_'); 13 | } 14 | if (url.indexOf('content://')===0) { 15 | return window.WEBVIEW_SERVER_URL + url.replace('content:/', '/_app_content_'); 16 | } 17 | return url; 18 | }, 19 | setServerBasePath: function(path) { 20 | exec(null, null, 'IonicWebView', 'setServerBasePath', [path]); 21 | }, 22 | getServerBasePath: function(callback) { 23 | exec(callback, null, 'IonicWebView', 'getServerBasePath', []); 24 | }, 25 | persistServerBasePath: function() { 26 | exec(null, null, 'IonicWebView', 'persistServerBasePath', []); 27 | } 28 | } 29 | 30 | module.exports = WebView; 31 | --------------------------------------------------------------------------------