├── .gitignore ├── ClickMeEventRegister.js ├── LICENSE ├── README.md ├── WKWebViewBridgeExample.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── WKWebViewBridgeExample ├── AppDelegate.swift ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── TestFile.html └── ViewController.swift └── WKWebViewBridgeExampleTests ├── Info.plist └── WKWebViewBridgeExampleTests.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | -------------------------------------------------------------------------------- /ClickMeEventRegister.js: -------------------------------------------------------------------------------- 1 | var button = document.getElementById("clickMeButton"); 2 | button.addEventListener("click", function() { 3 | var messgeToPost = {'ButtonId':'clickMeButton'}; 4 | window.webkit.messageHandlers.buttonClicked.postMessage(messgeToPost); 5 | },false); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 rajagp 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | OS-WKWebViewBridgeExample-Swift 2 | =============================== 3 | 4 | A sample app in Swift that demonstrates how to bridge between JS-Native code using WKWebViews in iOS8. 5 | In this example, we load a simple HTML page that has a button using a WKWebView. When the page loads, the native app injects a JS ("User Script") into the loaded document that listens for button click event and calls back into the native app ("Script Message") . The native app implements a listener to handle the callback message from the web page and updates the color of the button from within the callback handler. 6 | 7 | A correponding presentation is available for download at http://www.priyaontech.com/download/25/ 8 | -------------------------------------------------------------------------------- /WKWebViewBridgeExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 02549F141A364DB700FF51B4 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02549F131A364DB700FF51B4 /* AppDelegate.swift */; }; 11 | 02549F161A364DB700FF51B4 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02549F151A364DB700FF51B4 /* ViewController.swift */; }; 12 | 02549F191A364DB700FF51B4 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 02549F171A364DB700FF51B4 /* Main.storyboard */; }; 13 | 02549F1B1A364DB700FF51B4 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 02549F1A1A364DB700FF51B4 /* Images.xcassets */; }; 14 | 02549F1E1A364DB700FF51B4 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 02549F1C1A364DB700FF51B4 /* LaunchScreen.xib */; }; 15 | 02549F2A1A364DB700FF51B4 /* WKWebViewBridgeExampleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02549F291A364DB700FF51B4 /* WKWebViewBridgeExampleTests.swift */; }; 16 | 02549F391A3734F600FF51B4 /* ClickMeEventRegister.js in Resources */ = {isa = PBXBuildFile; fileRef = 02549F381A3734F600FF51B4 /* ClickMeEventRegister.js */; }; 17 | 02549F3A1A3734F600FF51B4 /* ClickMeEventRegister.js in Resources */ = {isa = PBXBuildFile; fileRef = 02549F381A3734F600FF51B4 /* ClickMeEventRegister.js */; }; 18 | 02549F3C1A373B1D00FF51B4 /* TestFile.html in Resources */ = {isa = PBXBuildFile; fileRef = 02549F3B1A373B1D00FF51B4 /* TestFile.html */; }; 19 | 02549F3D1A373B1D00FF51B4 /* TestFile.html in Resources */ = {isa = PBXBuildFile; fileRef = 02549F3B1A373B1D00FF51B4 /* TestFile.html */; }; 20 | /* End PBXBuildFile section */ 21 | 22 | /* Begin PBXContainerItemProxy section */ 23 | 02549F241A364DB700FF51B4 /* PBXContainerItemProxy */ = { 24 | isa = PBXContainerItemProxy; 25 | containerPortal = 02549F061A364DB700FF51B4 /* Project object */; 26 | proxyType = 1; 27 | remoteGlobalIDString = 02549F0D1A364DB700FF51B4; 28 | remoteInfo = WKWebViewBridgeExample; 29 | }; 30 | /* End PBXContainerItemProxy section */ 31 | 32 | /* Begin PBXFileReference section */ 33 | 02549F0E1A364DB700FF51B4 /* WKWebViewBridgeExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WKWebViewBridgeExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 34 | 02549F121A364DB700FF51B4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 35 | 02549F131A364DB700FF51B4 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 36 | 02549F151A364DB700FF51B4 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 37 | 02549F181A364DB700FF51B4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 38 | 02549F1A1A364DB700FF51B4 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 39 | 02549F1D1A364DB700FF51B4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 40 | 02549F231A364DB700FF51B4 /* WKWebViewBridgeExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = WKWebViewBridgeExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 41 | 02549F281A364DB700FF51B4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 42 | 02549F291A364DB700FF51B4 /* WKWebViewBridgeExampleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKWebViewBridgeExampleTests.swift; sourceTree = ""; }; 43 | 02549F381A3734F600FF51B4 /* ClickMeEventRegister.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = ClickMeEventRegister.js; path = ../ClickMeEventRegister.js; sourceTree = ""; }; 44 | 02549F3B1A373B1D00FF51B4 /* TestFile.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = TestFile.html; sourceTree = ""; }; 45 | /* End PBXFileReference section */ 46 | 47 | /* Begin PBXFrameworksBuildPhase section */ 48 | 02549F0B1A364DB700FF51B4 /* Frameworks */ = { 49 | isa = PBXFrameworksBuildPhase; 50 | buildActionMask = 2147483647; 51 | files = ( 52 | ); 53 | runOnlyForDeploymentPostprocessing = 0; 54 | }; 55 | 02549F201A364DB700FF51B4 /* Frameworks */ = { 56 | isa = PBXFrameworksBuildPhase; 57 | buildActionMask = 2147483647; 58 | files = ( 59 | ); 60 | runOnlyForDeploymentPostprocessing = 0; 61 | }; 62 | /* End PBXFrameworksBuildPhase section */ 63 | 64 | /* Begin PBXGroup section */ 65 | 02549F051A364DB700FF51B4 = { 66 | isa = PBXGroup; 67 | children = ( 68 | 02549F101A364DB700FF51B4 /* WKWebViewBridgeExample */, 69 | 02549F261A364DB700FF51B4 /* WKWebViewBridgeExampleTests */, 70 | 02549F0F1A364DB700FF51B4 /* Products */, 71 | ); 72 | sourceTree = ""; 73 | }; 74 | 02549F0F1A364DB700FF51B4 /* Products */ = { 75 | isa = PBXGroup; 76 | children = ( 77 | 02549F0E1A364DB700FF51B4 /* WKWebViewBridgeExample.app */, 78 | 02549F231A364DB700FF51B4 /* WKWebViewBridgeExampleTests.xctest */, 79 | ); 80 | name = Products; 81 | sourceTree = ""; 82 | }; 83 | 02549F101A364DB700FF51B4 /* WKWebViewBridgeExample */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | 02549F131A364DB700FF51B4 /* AppDelegate.swift */, 87 | 02549F151A364DB700FF51B4 /* ViewController.swift */, 88 | 02549F171A364DB700FF51B4 /* Main.storyboard */, 89 | 02549F1A1A364DB700FF51B4 /* Images.xcassets */, 90 | 02549F1C1A364DB700FF51B4 /* LaunchScreen.xib */, 91 | 02549F111A364DB700FF51B4 /* Supporting Files */, 92 | ); 93 | path = WKWebViewBridgeExample; 94 | sourceTree = ""; 95 | }; 96 | 02549F111A364DB700FF51B4 /* Supporting Files */ = { 97 | isa = PBXGroup; 98 | children = ( 99 | 02549F3B1A373B1D00FF51B4 /* TestFile.html */, 100 | 02549F381A3734F600FF51B4 /* ClickMeEventRegister.js */, 101 | 02549F121A364DB700FF51B4 /* Info.plist */, 102 | ); 103 | name = "Supporting Files"; 104 | sourceTree = ""; 105 | }; 106 | 02549F261A364DB700FF51B4 /* WKWebViewBridgeExampleTests */ = { 107 | isa = PBXGroup; 108 | children = ( 109 | 02549F291A364DB700FF51B4 /* WKWebViewBridgeExampleTests.swift */, 110 | 02549F271A364DB700FF51B4 /* Supporting Files */, 111 | ); 112 | path = WKWebViewBridgeExampleTests; 113 | sourceTree = ""; 114 | }; 115 | 02549F271A364DB700FF51B4 /* Supporting Files */ = { 116 | isa = PBXGroup; 117 | children = ( 118 | 02549F281A364DB700FF51B4 /* Info.plist */, 119 | ); 120 | name = "Supporting Files"; 121 | sourceTree = ""; 122 | }; 123 | /* End PBXGroup section */ 124 | 125 | /* Begin PBXNativeTarget section */ 126 | 02549F0D1A364DB700FF51B4 /* WKWebViewBridgeExample */ = { 127 | isa = PBXNativeTarget; 128 | buildConfigurationList = 02549F2D1A364DB700FF51B4 /* Build configuration list for PBXNativeTarget "WKWebViewBridgeExample" */; 129 | buildPhases = ( 130 | 02549F0A1A364DB700FF51B4 /* Sources */, 131 | 02549F0B1A364DB700FF51B4 /* Frameworks */, 132 | 02549F0C1A364DB700FF51B4 /* Resources */, 133 | ); 134 | buildRules = ( 135 | ); 136 | dependencies = ( 137 | ); 138 | name = WKWebViewBridgeExample; 139 | productName = WKWebViewBridgeExample; 140 | productReference = 02549F0E1A364DB700FF51B4 /* WKWebViewBridgeExample.app */; 141 | productType = "com.apple.product-type.application"; 142 | }; 143 | 02549F221A364DB700FF51B4 /* WKWebViewBridgeExampleTests */ = { 144 | isa = PBXNativeTarget; 145 | buildConfigurationList = 02549F301A364DB700FF51B4 /* Build configuration list for PBXNativeTarget "WKWebViewBridgeExampleTests" */; 146 | buildPhases = ( 147 | 02549F1F1A364DB700FF51B4 /* Sources */, 148 | 02549F201A364DB700FF51B4 /* Frameworks */, 149 | 02549F211A364DB700FF51B4 /* Resources */, 150 | ); 151 | buildRules = ( 152 | ); 153 | dependencies = ( 154 | 02549F251A364DB700FF51B4 /* PBXTargetDependency */, 155 | ); 156 | name = WKWebViewBridgeExampleTests; 157 | productName = WKWebViewBridgeExampleTests; 158 | productReference = 02549F231A364DB700FF51B4 /* WKWebViewBridgeExampleTests.xctest */; 159 | productType = "com.apple.product-type.bundle.unit-test"; 160 | }; 161 | /* End PBXNativeTarget section */ 162 | 163 | /* Begin PBXProject section */ 164 | 02549F061A364DB700FF51B4 /* Project object */ = { 165 | isa = PBXProject; 166 | attributes = { 167 | LastUpgradeCheck = 0610; 168 | ORGANIZATIONNAME = "Lunaria Software LLC"; 169 | TargetAttributes = { 170 | 02549F0D1A364DB700FF51B4 = { 171 | CreatedOnToolsVersion = 6.1; 172 | }; 173 | 02549F221A364DB700FF51B4 = { 174 | CreatedOnToolsVersion = 6.1; 175 | TestTargetID = 02549F0D1A364DB700FF51B4; 176 | }; 177 | }; 178 | }; 179 | buildConfigurationList = 02549F091A364DB700FF51B4 /* Build configuration list for PBXProject "WKWebViewBridgeExample" */; 180 | compatibilityVersion = "Xcode 3.2"; 181 | developmentRegion = English; 182 | hasScannedForEncodings = 0; 183 | knownRegions = ( 184 | en, 185 | Base, 186 | ); 187 | mainGroup = 02549F051A364DB700FF51B4; 188 | productRefGroup = 02549F0F1A364DB700FF51B4 /* Products */; 189 | projectDirPath = ""; 190 | projectRoot = ""; 191 | targets = ( 192 | 02549F0D1A364DB700FF51B4 /* WKWebViewBridgeExample */, 193 | 02549F221A364DB700FF51B4 /* WKWebViewBridgeExampleTests */, 194 | ); 195 | }; 196 | /* End PBXProject section */ 197 | 198 | /* Begin PBXResourcesBuildPhase section */ 199 | 02549F0C1A364DB700FF51B4 /* Resources */ = { 200 | isa = PBXResourcesBuildPhase; 201 | buildActionMask = 2147483647; 202 | files = ( 203 | 02549F3C1A373B1D00FF51B4 /* TestFile.html in Resources */, 204 | 02549F191A364DB700FF51B4 /* Main.storyboard in Resources */, 205 | 02549F391A3734F600FF51B4 /* ClickMeEventRegister.js in Resources */, 206 | 02549F1E1A364DB700FF51B4 /* LaunchScreen.xib in Resources */, 207 | 02549F1B1A364DB700FF51B4 /* Images.xcassets in Resources */, 208 | ); 209 | runOnlyForDeploymentPostprocessing = 0; 210 | }; 211 | 02549F211A364DB700FF51B4 /* Resources */ = { 212 | isa = PBXResourcesBuildPhase; 213 | buildActionMask = 2147483647; 214 | files = ( 215 | 02549F3D1A373B1D00FF51B4 /* TestFile.html in Resources */, 216 | 02549F3A1A3734F600FF51B4 /* ClickMeEventRegister.js in Resources */, 217 | ); 218 | runOnlyForDeploymentPostprocessing = 0; 219 | }; 220 | /* End PBXResourcesBuildPhase section */ 221 | 222 | /* Begin PBXSourcesBuildPhase section */ 223 | 02549F0A1A364DB700FF51B4 /* Sources */ = { 224 | isa = PBXSourcesBuildPhase; 225 | buildActionMask = 2147483647; 226 | files = ( 227 | 02549F161A364DB700FF51B4 /* ViewController.swift in Sources */, 228 | 02549F141A364DB700FF51B4 /* AppDelegate.swift in Sources */, 229 | ); 230 | runOnlyForDeploymentPostprocessing = 0; 231 | }; 232 | 02549F1F1A364DB700FF51B4 /* Sources */ = { 233 | isa = PBXSourcesBuildPhase; 234 | buildActionMask = 2147483647; 235 | files = ( 236 | 02549F2A1A364DB700FF51B4 /* WKWebViewBridgeExampleTests.swift in Sources */, 237 | ); 238 | runOnlyForDeploymentPostprocessing = 0; 239 | }; 240 | /* End PBXSourcesBuildPhase section */ 241 | 242 | /* Begin PBXTargetDependency section */ 243 | 02549F251A364DB700FF51B4 /* PBXTargetDependency */ = { 244 | isa = PBXTargetDependency; 245 | target = 02549F0D1A364DB700FF51B4 /* WKWebViewBridgeExample */; 246 | targetProxy = 02549F241A364DB700FF51B4 /* PBXContainerItemProxy */; 247 | }; 248 | /* End PBXTargetDependency section */ 249 | 250 | /* Begin PBXVariantGroup section */ 251 | 02549F171A364DB700FF51B4 /* Main.storyboard */ = { 252 | isa = PBXVariantGroup; 253 | children = ( 254 | 02549F181A364DB700FF51B4 /* Base */, 255 | ); 256 | name = Main.storyboard; 257 | sourceTree = ""; 258 | }; 259 | 02549F1C1A364DB700FF51B4 /* LaunchScreen.xib */ = { 260 | isa = PBXVariantGroup; 261 | children = ( 262 | 02549F1D1A364DB700FF51B4 /* Base */, 263 | ); 264 | name = LaunchScreen.xib; 265 | sourceTree = ""; 266 | }; 267 | /* End PBXVariantGroup section */ 268 | 269 | /* Begin XCBuildConfiguration section */ 270 | 02549F2B1A364DB700FF51B4 /* Debug */ = { 271 | isa = XCBuildConfiguration; 272 | buildSettings = { 273 | ALWAYS_SEARCH_USER_PATHS = NO; 274 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 275 | CLANG_CXX_LIBRARY = "libc++"; 276 | CLANG_ENABLE_MODULES = YES; 277 | CLANG_ENABLE_OBJC_ARC = YES; 278 | CLANG_WARN_BOOL_CONVERSION = YES; 279 | CLANG_WARN_CONSTANT_CONVERSION = YES; 280 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 281 | CLANG_WARN_EMPTY_BODY = YES; 282 | CLANG_WARN_ENUM_CONVERSION = YES; 283 | CLANG_WARN_INT_CONVERSION = YES; 284 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 285 | CLANG_WARN_UNREACHABLE_CODE = YES; 286 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 287 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 288 | COPY_PHASE_STRIP = NO; 289 | ENABLE_STRICT_OBJC_MSGSEND = YES; 290 | GCC_C_LANGUAGE_STANDARD = gnu99; 291 | GCC_DYNAMIC_NO_PIC = NO; 292 | GCC_OPTIMIZATION_LEVEL = 0; 293 | GCC_PREPROCESSOR_DEFINITIONS = ( 294 | "DEBUG=1", 295 | "$(inherited)", 296 | ); 297 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 298 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 299 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 300 | GCC_WARN_UNDECLARED_SELECTOR = YES; 301 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 302 | GCC_WARN_UNUSED_FUNCTION = YES; 303 | GCC_WARN_UNUSED_VARIABLE = YES; 304 | IPHONEOS_DEPLOYMENT_TARGET = 8.1; 305 | MTL_ENABLE_DEBUG_INFO = YES; 306 | ONLY_ACTIVE_ARCH = YES; 307 | SDKROOT = iphoneos; 308 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 309 | }; 310 | name = Debug; 311 | }; 312 | 02549F2C1A364DB700FF51B4 /* Release */ = { 313 | isa = XCBuildConfiguration; 314 | buildSettings = { 315 | ALWAYS_SEARCH_USER_PATHS = NO; 316 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 317 | CLANG_CXX_LIBRARY = "libc++"; 318 | CLANG_ENABLE_MODULES = YES; 319 | CLANG_ENABLE_OBJC_ARC = YES; 320 | CLANG_WARN_BOOL_CONVERSION = YES; 321 | CLANG_WARN_CONSTANT_CONVERSION = YES; 322 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 323 | CLANG_WARN_EMPTY_BODY = YES; 324 | CLANG_WARN_ENUM_CONVERSION = YES; 325 | CLANG_WARN_INT_CONVERSION = YES; 326 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 327 | CLANG_WARN_UNREACHABLE_CODE = YES; 328 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 329 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 330 | COPY_PHASE_STRIP = YES; 331 | ENABLE_NS_ASSERTIONS = NO; 332 | ENABLE_STRICT_OBJC_MSGSEND = YES; 333 | GCC_C_LANGUAGE_STANDARD = gnu99; 334 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 335 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 336 | GCC_WARN_UNDECLARED_SELECTOR = YES; 337 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 338 | GCC_WARN_UNUSED_FUNCTION = YES; 339 | GCC_WARN_UNUSED_VARIABLE = YES; 340 | IPHONEOS_DEPLOYMENT_TARGET = 8.1; 341 | MTL_ENABLE_DEBUG_INFO = NO; 342 | SDKROOT = iphoneos; 343 | VALIDATE_PRODUCT = YES; 344 | }; 345 | name = Release; 346 | }; 347 | 02549F2E1A364DB700FF51B4 /* Debug */ = { 348 | isa = XCBuildConfiguration; 349 | buildSettings = { 350 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 351 | INFOPLIST_FILE = WKWebViewBridgeExample/Info.plist; 352 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 353 | PRODUCT_NAME = "$(TARGET_NAME)"; 354 | }; 355 | name = Debug; 356 | }; 357 | 02549F2F1A364DB700FF51B4 /* Release */ = { 358 | isa = XCBuildConfiguration; 359 | buildSettings = { 360 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 361 | INFOPLIST_FILE = WKWebViewBridgeExample/Info.plist; 362 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 363 | PRODUCT_NAME = "$(TARGET_NAME)"; 364 | }; 365 | name = Release; 366 | }; 367 | 02549F311A364DB700FF51B4 /* Debug */ = { 368 | isa = XCBuildConfiguration; 369 | buildSettings = { 370 | BUNDLE_LOADER = "$(TEST_HOST)"; 371 | FRAMEWORK_SEARCH_PATHS = ( 372 | "$(SDKROOT)/Developer/Library/Frameworks", 373 | "$(inherited)", 374 | ); 375 | GCC_PREPROCESSOR_DEFINITIONS = ( 376 | "DEBUG=1", 377 | "$(inherited)", 378 | ); 379 | INFOPLIST_FILE = WKWebViewBridgeExampleTests/Info.plist; 380 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 381 | PRODUCT_NAME = "$(TARGET_NAME)"; 382 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/WKWebViewBridgeExample.app/WKWebViewBridgeExample"; 383 | }; 384 | name = Debug; 385 | }; 386 | 02549F321A364DB700FF51B4 /* Release */ = { 387 | isa = XCBuildConfiguration; 388 | buildSettings = { 389 | BUNDLE_LOADER = "$(TEST_HOST)"; 390 | FRAMEWORK_SEARCH_PATHS = ( 391 | "$(SDKROOT)/Developer/Library/Frameworks", 392 | "$(inherited)", 393 | ); 394 | INFOPLIST_FILE = WKWebViewBridgeExampleTests/Info.plist; 395 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 396 | PRODUCT_NAME = "$(TARGET_NAME)"; 397 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/WKWebViewBridgeExample.app/WKWebViewBridgeExample"; 398 | }; 399 | name = Release; 400 | }; 401 | /* End XCBuildConfiguration section */ 402 | 403 | /* Begin XCConfigurationList section */ 404 | 02549F091A364DB700FF51B4 /* Build configuration list for PBXProject "WKWebViewBridgeExample" */ = { 405 | isa = XCConfigurationList; 406 | buildConfigurations = ( 407 | 02549F2B1A364DB700FF51B4 /* Debug */, 408 | 02549F2C1A364DB700FF51B4 /* Release */, 409 | ); 410 | defaultConfigurationIsVisible = 0; 411 | defaultConfigurationName = Release; 412 | }; 413 | 02549F2D1A364DB700FF51B4 /* Build configuration list for PBXNativeTarget "WKWebViewBridgeExample" */ = { 414 | isa = XCConfigurationList; 415 | buildConfigurations = ( 416 | 02549F2E1A364DB700FF51B4 /* Debug */, 417 | 02549F2F1A364DB700FF51B4 /* Release */, 418 | ); 419 | defaultConfigurationIsVisible = 0; 420 | }; 421 | 02549F301A364DB700FF51B4 /* Build configuration list for PBXNativeTarget "WKWebViewBridgeExampleTests" */ = { 422 | isa = XCConfigurationList; 423 | buildConfigurations = ( 424 | 02549F311A364DB700FF51B4 /* Debug */, 425 | 02549F321A364DB700FF51B4 /* Release */, 426 | ); 427 | defaultConfigurationIsVisible = 0; 428 | }; 429 | /* End XCConfigurationList section */ 430 | }; 431 | rootObject = 02549F061A364DB700FF51B4 /* Project object */; 432 | } 433 | -------------------------------------------------------------------------------- /WKWebViewBridgeExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /WKWebViewBridgeExample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // WKWebViewBridgeExample 4 | // 5 | // Created by Priya Rajagopal on 12/8/14. 6 | // Copyright (c) 2014 Lunaria Software LLC. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /WKWebViewBridgeExample/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /WKWebViewBridgeExample/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /WKWebViewBridgeExample/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /WKWebViewBridgeExample/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.lunariasoftware.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /WKWebViewBridgeExample/TestFile.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 10 |
11 |
12 |
13 |

Tap on the button to change its color!

14 |

                                                                                                15 | 16 |

17 | 18 | 19 | -------------------------------------------------------------------------------- /WKWebViewBridgeExample/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // WKWebViewBridgeExample 4 | // 5 | // Created by Priya Rajagopal on 12/8/14. 6 | // Copyright (c) 2014 Lunaria Software LLC. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import WebKit 11 | 12 | class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDelegate { 13 | var webView: WKWebView? 14 | var buttonClicked:Int = 0 15 | var colors:[String] = ["0xff00ff","#ff0000","#ffcc00","#ccff00","#ff0033","#ff0099","#cc0099","#0033ff","#0066ff","#ffff00","#0000ff","#0099cc"]; 16 | var webConfig:WKWebViewConfiguration { 17 | get { 18 | 19 | // Create WKWebViewConfiguration instance 20 | var webCfg:WKWebViewConfiguration = WKWebViewConfiguration() 21 | 22 | // Setup WKUserContentController instance for injecting user script 23 | var userController:WKUserContentController = WKUserContentController() 24 | 25 | // Add a script message handler for receiving "buttonClicked" event notifications posted from the JS document using window.webkit.messageHandlers.buttonClicked.postMessage script message 26 | userController.addScriptMessageHandler(self, name: "buttonClicked") 27 | 28 | // Get script that's to be injected into the document 29 | let js:String = buttonClickEventTriggeredScriptToAddToDocument() 30 | 31 | // Specify when and where and what user script needs to be injected into the web document 32 | var userScript:WKUserScript = WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, forMainFrameOnly: false) 33 | 34 | // Add the user script to the WKUserContentController instance 35 | userController.addUserScript(userScript) 36 | 37 | // Configure the WKWebViewConfiguration instance with the WKUserContentController 38 | webCfg.userContentController = userController; 39 | 40 | return webCfg; 41 | } 42 | } 43 | 44 | override func viewDidLoad() { 45 | super.viewDidLoad() 46 | 47 | // Create a WKWebView instance 48 | webView = WKWebView (frame: self.view.frame, configuration: webConfig) 49 | 50 | // Delegate to handle navigation of web content 51 | webView!.navigationDelegate = self 52 | 53 | view.addSubview(webView!) 54 | 55 | } 56 | 57 | override func viewDidAppear(animated: Bool) { 58 | super.viewDidAppear(animated) 59 | 60 | // Load the HTML document 61 | loadHtml() 62 | } 63 | 64 | override func viewDidDisappear(animated: Bool) { 65 | super.viewDidDisappear(animated) 66 | 67 | var fileName:String = String("\( NSProcessInfo.processInfo().globallyUniqueString)_TestFile.html") 68 | 69 | var error:NSError? 70 | var tempHtmlPath:String = NSTemporaryDirectory().stringByAppendingPathComponent(fileName) 71 | NSFileManager.defaultManager().removeItemAtPath(tempHtmlPath, error: &error) 72 | 73 | webView = nil 74 | 75 | } 76 | 77 | override func didReceiveMemoryWarning() { 78 | super.didReceiveMemoryWarning() 79 | // Dispose of any resources that can be recreated. 80 | } 81 | 82 | // WKNavigationDelegate 83 | func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) { 84 | NSLog("%s", __FUNCTION__) 85 | } 86 | 87 | func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) { 88 | NSLog("%s. With Error %@", __FUNCTION__,error) 89 | showAlertWithMessage("Failed to load file with error \(error.localizedDescription)!") 90 | } 91 | 92 | 93 | // File Loading 94 | func loadHtml() { 95 | // NOTE: Due to a bug in webKit as of iOS 8.1.1 we CANNOT load a local resource when running on device. Once that is fixed, we can get rid of the temp copy 96 | let mainBundle:NSBundle = NSBundle(forClass: ViewController.self) 97 | var error:NSError? 98 | 99 | var fileName:String = String("\( NSProcessInfo.processInfo().globallyUniqueString)_TestFile.html") 100 | 101 | var tempHtmlPath:String? = NSTemporaryDirectory().stringByAppendingPathComponent(fileName) 102 | 103 | if let htmlPath = mainBundle.pathForResource("TestFile", ofType: "html") { 104 | NSFileManager.defaultManager().copyItemAtPath(htmlPath, toPath: tempHtmlPath!, error: &error) 105 | if tempHtmlPath != nil { 106 | let requestUrl = NSURLRequest(URL: NSURL(fileURLWithPath: tempHtmlPath!)!) 107 | webView?.loadRequest(requestUrl) 108 | } 109 | } 110 | else { 111 | showAlertWithMessage("Could not load HTML File!") 112 | } 113 | 114 | } 115 | 116 | // Button Click Script to Add to Document 117 | func buttonClickEventTriggeredScriptToAddToDocument() ->String{ 118 | // Script: When window is loaded, execute an anonymous function that adds a "click" event handler function to the "ClickMeButton" button element. The "click" event handler calls back into our native code via the window.webkit.messageHandlers.buttonClicked.postMessage call 119 | var script:String? 120 | 121 | if let filePath:String = NSBundle(forClass: ViewController.self).pathForResource("ClickMeEventRegister", ofType:"js") { 122 | 123 | script = String (contentsOfFile: filePath, encoding: NSUTF8StringEncoding, error: nil) 124 | } 125 | return script!; 126 | 127 | } 128 | 129 | // WKScriptMessageHandler Delegate 130 | func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) { 131 | 132 | if let messageBody:NSDictionary = message.body as? NSDictionary { 133 | let idOfTappedButton:String = messageBody["ButtonId"] as String 134 | updateColorOfButtonWithId(idOfTappedButton) 135 | } 136 | 137 | } 138 | 139 | 140 | // Update color of Button with specified Id 141 | func updateColorOfButtonWithId(buttonId:String) { 142 | let count:UInt32 = UInt32(colors.count) 143 | let index:Int = Int(arc4random_uniform(count)) 144 | let color:String = colors [index] 145 | 146 | // Script that changes the color of tapped button 147 | var js2:String = String(format: "var button = document.getElementById('%@'); button.style.backgroundColor='%@';", buttonId,color) 148 | 149 | webView?.evaluateJavaScript(js2, completionHandler: { (AnyObject, NSError) -> Void in 150 | NSLog("%s", __FUNCTION__) 151 | 152 | }) 153 | } 154 | 155 | // Helper 156 | func showAlertWithMessage(message:String) { 157 | let alertAction:UIAlertAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (UIAlertAction) -> Void in 158 | self.dismissViewControllerAnimated(true, completion: { () -> Void in 159 | 160 | }) 161 | } 162 | 163 | let alertView:UIAlertController = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert) 164 | alertView.addAction(alertAction) 165 | 166 | self.presentViewController(alertView, animated: true, completion: { () -> Void in 167 | 168 | }) 169 | } 170 | 171 | 172 | } 173 | 174 | -------------------------------------------------------------------------------- /WKWebViewBridgeExampleTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.lunariasoftware.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /WKWebViewBridgeExampleTests/WKWebViewBridgeExampleTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // WKWebViewBridgeExampleTests.swift 3 | // WKWebViewBridgeExampleTests 4 | // 5 | // Created by Priya Rajagopal on 12/8/14. 6 | // Copyright (c) 2014 Lunaria Software LLC. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import XCTest 11 | 12 | class WKWebViewBridgeExampleTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | // This is an example of a functional test case. 26 | XCTAssert(true, "Pass") 27 | } 28 | 29 | func testPerformanceExample() { 30 | // This is an example of a performance test case. 31 | self.measureBlock() { 32 | // Put the code you want to measure the time of here. 33 | } 34 | } 35 | 36 | } 37 | --------------------------------------------------------------------------------