├── .gitignore ├── LICENSE ├── README.md └── WKWebViewDemo ├── .DS_Store ├── WKWebViewDemo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── hujunhua.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── hujunhua.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── WKWebViewDemo.xcscheme │ └── xcschememanagement.plist └── WKWebViewDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── OCJSHelper.h ├── OCJSHelper.m ├── PushVC.h ├── PushVC.m ├── ViewController.h ├── ViewController.m ├── main.m └── test.html /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xcuserstate 23 | 24 | ## Obj-C/Swift specific 25 | *.hmap 26 | *.ipa 27 | *.dSYM.zip 28 | *.dSYM 29 | 30 | # CocoaPods 31 | # 32 | # We recommend against adding the Pods directory to your .gitignore. However 33 | # you should judge for yourself, the pros and cons are mentioned at: 34 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 35 | # 36 | # Pods/ 37 | 38 | # Carthage 39 | # 40 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 41 | # Carthage/Checkouts 42 | 43 | Carthage/Build 44 | 45 | # fastlane 46 | # 47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 48 | # screenshots whenever they are needed. 49 | # For more information about the recommended setup visit: 50 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 51 | 52 | fastlane/report.xml 53 | fastlane/screenshots 54 | 55 | #Code Injection 56 | # 57 | # After new code Injection tools there's a generated folder /iOSInjectionProject 58 | # https://github.com/johnno1962/injectionforxcode 59 | 60 | iOSInjectionProject/ 61 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 SSiming 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WKWebViewDemo 2 | 3 | WKWebView实际使用中遇到的注意点, 以及WKWebView和JavaScript交互 4 | 具体介绍请戳[WKWebView使用及注意点(keng)](http://www.jianshu.com/p/9513d101e582) -------------------------------------------------------------------------------- /WKWebViewDemo/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SSiming/WKWebViewDemo/aede6afc21a9e7eade06c8bab7488bbbbe6b43a2/WKWebViewDemo/.DS_Store -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 705DE6261DDD8539005752FC /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 705DE6251DDD8539005752FC /* main.m */; }; 11 | 705DE6291DDD8539005752FC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 705DE6281DDD8539005752FC /* AppDelegate.m */; }; 12 | 705DE62C1DDD8539005752FC /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 705DE62B1DDD8539005752FC /* ViewController.m */; }; 13 | 705DE62F1DDD8539005752FC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 705DE62D1DDD8539005752FC /* Main.storyboard */; }; 14 | 705DE6311DDD8539005752FC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 705DE6301DDD8539005752FC /* Assets.xcassets */; }; 15 | 705DE6341DDD8539005752FC /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 705DE6321DDD8539005752FC /* LaunchScreen.storyboard */; }; 16 | 705DE63D1DDD86AA005752FC /* OCJSHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 705DE63C1DDD86AA005752FC /* OCJSHelper.m */; }; 17 | 705DE63F1DDDB302005752FC /* test.html in Resources */ = {isa = PBXBuildFile; fileRef = 705DE63E1DDDB302005752FC /* test.html */; }; 18 | 70DE24F11E053458000D68CA /* PushVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 70DE24F01E053458000D68CA /* PushVC.m */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXFileReference section */ 22 | 705DE6211DDD8539005752FC /* WKWebViewDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WKWebViewDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 23 | 705DE6251DDD8539005752FC /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 24 | 705DE6271DDD8539005752FC /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 25 | 705DE6281DDD8539005752FC /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 26 | 705DE62A1DDD8539005752FC /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 27 | 705DE62B1DDD8539005752FC /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 28 | 705DE62E1DDD8539005752FC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 29 | 705DE6301DDD8539005752FC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 30 | 705DE6331DDD8539005752FC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 31 | 705DE6351DDD8539005752FC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 32 | 705DE63B1DDD86AA005752FC /* OCJSHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCJSHelper.h; sourceTree = ""; }; 33 | 705DE63C1DDD86AA005752FC /* OCJSHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCJSHelper.m; sourceTree = ""; }; 34 | 705DE63E1DDDB302005752FC /* test.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = test.html; sourceTree = ""; }; 35 | 70DE24EF1E053458000D68CA /* PushVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PushVC.h; sourceTree = ""; }; 36 | 70DE24F01E053458000D68CA /* PushVC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PushVC.m; sourceTree = ""; }; 37 | /* End PBXFileReference section */ 38 | 39 | /* Begin PBXFrameworksBuildPhase section */ 40 | 705DE61E1DDD8539005752FC /* Frameworks */ = { 41 | isa = PBXFrameworksBuildPhase; 42 | buildActionMask = 2147483647; 43 | files = ( 44 | ); 45 | runOnlyForDeploymentPostprocessing = 0; 46 | }; 47 | /* End PBXFrameworksBuildPhase section */ 48 | 49 | /* Begin PBXGroup section */ 50 | 705DE6181DDD8539005752FC = { 51 | isa = PBXGroup; 52 | children = ( 53 | 705DE6231DDD8539005752FC /* WKWebViewDemo */, 54 | 705DE6221DDD8539005752FC /* Products */, 55 | ); 56 | sourceTree = ""; 57 | }; 58 | 705DE6221DDD8539005752FC /* Products */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 705DE6211DDD8539005752FC /* WKWebViewDemo.app */, 62 | ); 63 | name = Products; 64 | sourceTree = ""; 65 | }; 66 | 705DE6231DDD8539005752FC /* WKWebViewDemo */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | 705DE6271DDD8539005752FC /* AppDelegate.h */, 70 | 705DE6281DDD8539005752FC /* AppDelegate.m */, 71 | 70DE24EF1E053458000D68CA /* PushVC.h */, 72 | 70DE24F01E053458000D68CA /* PushVC.m */, 73 | 705DE62A1DDD8539005752FC /* ViewController.h */, 74 | 705DE62B1DDD8539005752FC /* ViewController.m */, 75 | 705DE63B1DDD86AA005752FC /* OCJSHelper.h */, 76 | 705DE63C1DDD86AA005752FC /* OCJSHelper.m */, 77 | 705DE63E1DDDB302005752FC /* test.html */, 78 | 705DE62D1DDD8539005752FC /* Main.storyboard */, 79 | 705DE6301DDD8539005752FC /* Assets.xcassets */, 80 | 705DE6321DDD8539005752FC /* LaunchScreen.storyboard */, 81 | 705DE6351DDD8539005752FC /* Info.plist */, 82 | 705DE6241DDD8539005752FC /* Supporting Files */, 83 | ); 84 | path = WKWebViewDemo; 85 | sourceTree = ""; 86 | }; 87 | 705DE6241DDD8539005752FC /* Supporting Files */ = { 88 | isa = PBXGroup; 89 | children = ( 90 | 705DE6251DDD8539005752FC /* main.m */, 91 | ); 92 | name = "Supporting Files"; 93 | sourceTree = ""; 94 | }; 95 | /* End PBXGroup section */ 96 | 97 | /* Begin PBXNativeTarget section */ 98 | 705DE6201DDD8539005752FC /* WKWebViewDemo */ = { 99 | isa = PBXNativeTarget; 100 | buildConfigurationList = 705DE6381DDD8539005752FC /* Build configuration list for PBXNativeTarget "WKWebViewDemo" */; 101 | buildPhases = ( 102 | 705DE61D1DDD8539005752FC /* Sources */, 103 | 705DE61E1DDD8539005752FC /* Frameworks */, 104 | 705DE61F1DDD8539005752FC /* Resources */, 105 | ); 106 | buildRules = ( 107 | ); 108 | dependencies = ( 109 | ); 110 | name = WKWebViewDemo; 111 | productName = WKWebViewDemo; 112 | productReference = 705DE6211DDD8539005752FC /* WKWebViewDemo.app */; 113 | productType = "com.apple.product-type.application"; 114 | }; 115 | /* End PBXNativeTarget section */ 116 | 117 | /* Begin PBXProject section */ 118 | 705DE6191DDD8539005752FC /* Project object */ = { 119 | isa = PBXProject; 120 | attributes = { 121 | LastUpgradeCheck = 0810; 122 | ORGANIZATIONNAME = hujunhua; 123 | TargetAttributes = { 124 | 705DE6201DDD8539005752FC = { 125 | CreatedOnToolsVersion = 8.1; 126 | ProvisioningStyle = Automatic; 127 | }; 128 | }; 129 | }; 130 | buildConfigurationList = 705DE61C1DDD8539005752FC /* Build configuration list for PBXProject "WKWebViewDemo" */; 131 | compatibilityVersion = "Xcode 3.2"; 132 | developmentRegion = English; 133 | hasScannedForEncodings = 0; 134 | knownRegions = ( 135 | en, 136 | Base, 137 | ); 138 | mainGroup = 705DE6181DDD8539005752FC; 139 | productRefGroup = 705DE6221DDD8539005752FC /* Products */; 140 | projectDirPath = ""; 141 | projectRoot = ""; 142 | targets = ( 143 | 705DE6201DDD8539005752FC /* WKWebViewDemo */, 144 | ); 145 | }; 146 | /* End PBXProject section */ 147 | 148 | /* Begin PBXResourcesBuildPhase section */ 149 | 705DE61F1DDD8539005752FC /* Resources */ = { 150 | isa = PBXResourcesBuildPhase; 151 | buildActionMask = 2147483647; 152 | files = ( 153 | 705DE6341DDD8539005752FC /* LaunchScreen.storyboard in Resources */, 154 | 705DE63F1DDDB302005752FC /* test.html in Resources */, 155 | 705DE6311DDD8539005752FC /* Assets.xcassets in Resources */, 156 | 705DE62F1DDD8539005752FC /* Main.storyboard in Resources */, 157 | ); 158 | runOnlyForDeploymentPostprocessing = 0; 159 | }; 160 | /* End PBXResourcesBuildPhase section */ 161 | 162 | /* Begin PBXSourcesBuildPhase section */ 163 | 705DE61D1DDD8539005752FC /* Sources */ = { 164 | isa = PBXSourcesBuildPhase; 165 | buildActionMask = 2147483647; 166 | files = ( 167 | 705DE62C1DDD8539005752FC /* ViewController.m in Sources */, 168 | 705DE6291DDD8539005752FC /* AppDelegate.m in Sources */, 169 | 705DE63D1DDD86AA005752FC /* OCJSHelper.m in Sources */, 170 | 70DE24F11E053458000D68CA /* PushVC.m in Sources */, 171 | 705DE6261DDD8539005752FC /* main.m in Sources */, 172 | ); 173 | runOnlyForDeploymentPostprocessing = 0; 174 | }; 175 | /* End PBXSourcesBuildPhase section */ 176 | 177 | /* Begin PBXVariantGroup section */ 178 | 705DE62D1DDD8539005752FC /* Main.storyboard */ = { 179 | isa = PBXVariantGroup; 180 | children = ( 181 | 705DE62E1DDD8539005752FC /* Base */, 182 | ); 183 | name = Main.storyboard; 184 | sourceTree = ""; 185 | }; 186 | 705DE6321DDD8539005752FC /* LaunchScreen.storyboard */ = { 187 | isa = PBXVariantGroup; 188 | children = ( 189 | 705DE6331DDD8539005752FC /* Base */, 190 | ); 191 | name = LaunchScreen.storyboard; 192 | sourceTree = ""; 193 | }; 194 | /* End PBXVariantGroup section */ 195 | 196 | /* Begin XCBuildConfiguration section */ 197 | 705DE6361DDD8539005752FC /* Debug */ = { 198 | isa = XCBuildConfiguration; 199 | buildSettings = { 200 | ALWAYS_SEARCH_USER_PATHS = NO; 201 | CLANG_ANALYZER_NONNULL = YES; 202 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 203 | CLANG_CXX_LIBRARY = "libc++"; 204 | CLANG_ENABLE_MODULES = YES; 205 | CLANG_ENABLE_OBJC_ARC = YES; 206 | CLANG_WARN_BOOL_CONVERSION = YES; 207 | CLANG_WARN_CONSTANT_CONVERSION = YES; 208 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 209 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 210 | CLANG_WARN_EMPTY_BODY = YES; 211 | CLANG_WARN_ENUM_CONVERSION = YES; 212 | CLANG_WARN_INFINITE_RECURSION = YES; 213 | CLANG_WARN_INT_CONVERSION = YES; 214 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 215 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 216 | CLANG_WARN_UNREACHABLE_CODE = YES; 217 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 218 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 219 | COPY_PHASE_STRIP = NO; 220 | DEBUG_INFORMATION_FORMAT = dwarf; 221 | ENABLE_STRICT_OBJC_MSGSEND = YES; 222 | ENABLE_TESTABILITY = YES; 223 | GCC_C_LANGUAGE_STANDARD = gnu99; 224 | GCC_DYNAMIC_NO_PIC = NO; 225 | GCC_NO_COMMON_BLOCKS = YES; 226 | GCC_OPTIMIZATION_LEVEL = 0; 227 | GCC_PREPROCESSOR_DEFINITIONS = ( 228 | "DEBUG=1", 229 | "$(inherited)", 230 | ); 231 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 232 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 233 | GCC_WARN_UNDECLARED_SELECTOR = YES; 234 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 235 | GCC_WARN_UNUSED_FUNCTION = YES; 236 | GCC_WARN_UNUSED_VARIABLE = YES; 237 | IPHONEOS_DEPLOYMENT_TARGET = 10.1; 238 | MTL_ENABLE_DEBUG_INFO = YES; 239 | ONLY_ACTIVE_ARCH = YES; 240 | SDKROOT = iphoneos; 241 | }; 242 | name = Debug; 243 | }; 244 | 705DE6371DDD8539005752FC /* Release */ = { 245 | isa = XCBuildConfiguration; 246 | buildSettings = { 247 | ALWAYS_SEARCH_USER_PATHS = NO; 248 | CLANG_ANALYZER_NONNULL = YES; 249 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 250 | CLANG_CXX_LIBRARY = "libc++"; 251 | CLANG_ENABLE_MODULES = YES; 252 | CLANG_ENABLE_OBJC_ARC = YES; 253 | CLANG_WARN_BOOL_CONVERSION = YES; 254 | CLANG_WARN_CONSTANT_CONVERSION = YES; 255 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 256 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 257 | CLANG_WARN_EMPTY_BODY = YES; 258 | CLANG_WARN_ENUM_CONVERSION = YES; 259 | CLANG_WARN_INFINITE_RECURSION = YES; 260 | CLANG_WARN_INT_CONVERSION = YES; 261 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 262 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 263 | CLANG_WARN_UNREACHABLE_CODE = YES; 264 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 265 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 266 | COPY_PHASE_STRIP = NO; 267 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 268 | ENABLE_NS_ASSERTIONS = NO; 269 | ENABLE_STRICT_OBJC_MSGSEND = YES; 270 | GCC_C_LANGUAGE_STANDARD = gnu99; 271 | GCC_NO_COMMON_BLOCKS = YES; 272 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 273 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 274 | GCC_WARN_UNDECLARED_SELECTOR = YES; 275 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 276 | GCC_WARN_UNUSED_FUNCTION = YES; 277 | GCC_WARN_UNUSED_VARIABLE = YES; 278 | IPHONEOS_DEPLOYMENT_TARGET = 10.1; 279 | MTL_ENABLE_DEBUG_INFO = NO; 280 | SDKROOT = iphoneos; 281 | VALIDATE_PRODUCT = YES; 282 | }; 283 | name = Release; 284 | }; 285 | 705DE6391DDD8539005752FC /* Debug */ = { 286 | isa = XCBuildConfiguration; 287 | buildSettings = { 288 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 289 | INFOPLIST_FILE = WKWebViewDemo/Info.plist; 290 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 291 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 292 | PRODUCT_BUNDLE_IDENTIFIER = com.threegene.WKWebViewDemo; 293 | PRODUCT_NAME = "$(TARGET_NAME)"; 294 | }; 295 | name = Debug; 296 | }; 297 | 705DE63A1DDD8539005752FC /* Release */ = { 298 | isa = XCBuildConfiguration; 299 | buildSettings = { 300 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 301 | INFOPLIST_FILE = WKWebViewDemo/Info.plist; 302 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 303 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 304 | PRODUCT_BUNDLE_IDENTIFIER = com.threegene.WKWebViewDemo; 305 | PRODUCT_NAME = "$(TARGET_NAME)"; 306 | }; 307 | name = Release; 308 | }; 309 | /* End XCBuildConfiguration section */ 310 | 311 | /* Begin XCConfigurationList section */ 312 | 705DE61C1DDD8539005752FC /* Build configuration list for PBXProject "WKWebViewDemo" */ = { 313 | isa = XCConfigurationList; 314 | buildConfigurations = ( 315 | 705DE6361DDD8539005752FC /* Debug */, 316 | 705DE6371DDD8539005752FC /* Release */, 317 | ); 318 | defaultConfigurationIsVisible = 0; 319 | defaultConfigurationName = Release; 320 | }; 321 | 705DE6381DDD8539005752FC /* Build configuration list for PBXNativeTarget "WKWebViewDemo" */ = { 322 | isa = XCConfigurationList; 323 | buildConfigurations = ( 324 | 705DE6391DDD8539005752FC /* Debug */, 325 | 705DE63A1DDD8539005752FC /* Release */, 326 | ); 327 | defaultConfigurationIsVisible = 0; 328 | defaultConfigurationName = Release; 329 | }; 330 | /* End XCConfigurationList section */ 331 | }; 332 | rootObject = 705DE6191DDD8539005752FC /* Project object */; 333 | } 334 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo.xcodeproj/project.xcworkspace/xcuserdata/hujunhua.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SSiming/WKWebViewDemo/aede6afc21a9e7eade06c8bab7488bbbbe6b43a2/WKWebViewDemo/WKWebViewDemo.xcodeproj/project.xcworkspace/xcuserdata/hujunhua.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo.xcodeproj/xcuserdata/hujunhua.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo.xcodeproj/xcuserdata/hujunhua.xcuserdatad/xcschemes/WKWebViewDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo.xcodeproj/xcuserdata/hujunhua.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | WKWebViewDemo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 705DE6201DDD8539005752FC 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // WKWebViewDemo 4 | // 5 | // Created by hujunhua on 2016/11/17. 6 | // Copyright © 2016年 hujunhua. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // WKWebViewDemo 4 | // 5 | // Created by hujunhua on 2016/11/17. 6 | // Copyright © 2016年 hujunhua. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // 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. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // 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. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // 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. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | } 43 | ], 44 | "info" : { 45 | "version" : 1, 46 | "author" : "xcode" 47 | } 48 | } -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/Base.lproj/LaunchScreen.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 | 27 | 28 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/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 | 27 | 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 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NSAppTransportSecurity 6 | 7 | NSAllowsArbitraryLoads 8 | 9 | 10 | CFBundleDevelopmentRegion 11 | en 12 | CFBundleExecutable 13 | $(EXECUTABLE_NAME) 14 | CFBundleIdentifier 15 | $(PRODUCT_BUNDLE_IDENTIFIER) 16 | CFBundleInfoDictionaryVersion 17 | 6.0 18 | CFBundleName 19 | $(PRODUCT_NAME) 20 | CFBundlePackageType 21 | APPL 22 | CFBundleShortVersionString 23 | 1.0 24 | CFBundleVersion 25 | 1 26 | LSRequiresIPhoneOS 27 | 28 | UILaunchStoryboardName 29 | LaunchScreen 30 | UIMainStoryboardFile 31 | Main 32 | UIRequiredDeviceCapabilities 33 | 34 | armv7 35 | 36 | UISupportedInterfaceOrientations 37 | 38 | UIInterfaceOrientationPortrait 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/OCJSHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // OCJSHelper.h 3 | // WKWebViewDemo 4 | // 5 | // Created by hujunhua on 2016/11/17. 6 | // Copyright © 2016年 hujunhua. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @protocol OCJSHelperDelegate 13 | @optional 14 | @end 15 | 16 | @interface OCJSHelper : NSObject 17 | 18 | @property (nonatomic, weak) id delegate; 19 | @property (nonatomic, weak) WKWebView *webView; 20 | 21 | /** 22 | 指定初始化方法 23 | 24 | @param delegate 代理 25 | @param vc 实现WebView的VC 26 | @return 返回自身实例 27 | */ 28 | - (instancetype)initWithDelegate:(id)delegate vc:(UIViewController *)vc; 29 | @end 30 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/OCJSHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // OCJSHelper.m 3 | // WKWebViewDemo 4 | // 5 | // Created by hujunhua on 2016/11/17. 6 | // Copyright © 2016年 hujunhua. All rights reserved. 7 | // 8 | 9 | #import "OCJSHelper.h" 10 | 11 | @interface OCJSHelper() 12 | @property (nonatomic, weak) UIViewController *vc; 13 | @end 14 | 15 | @implementation OCJSHelper 16 | 17 | - (instancetype)initWithDelegate:(id)delegate vc:(UIViewController *)vc; { 18 | if (self = [super init]) { 19 | self.delegate = delegate; 20 | self.vc = vc; 21 | } 22 | return self; 23 | } 24 | 25 | - (void)dealloc { 26 | NSLog(@"%@, %s", self.class, __func__); 27 | } 28 | 29 | - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { 30 | NSDictionary *dic = [NSDictionary dictionaryWithDictionary:message.body]; 31 | NSLog(@"JS交互参数:%@", dic); 32 | 33 | if ([message.name isEqualToString:@"timefor"] && [dic isKindOfClass:[NSDictionary class]]) { 34 | 35 | NSLog(@"currentThread ------ %@", [NSThread currentThread]); 36 | 37 | dispatch_async(dispatch_get_main_queue(), ^{ 38 | NSString *code = dic[@"code"]; 39 | if ([code isEqualToString:@"0001"]) { 40 | NSString *js = [NSString stringWithFormat:@"globalCallback(\'%@\')", @"该设备的deviceId: *****"]; 41 | [self.webView evaluateJavaScript:js completionHandler:nil]; 42 | } else { 43 | return; 44 | } 45 | }); 46 | } else { 47 | return; 48 | } 49 | } 50 | 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/PushVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // PushVC.h 3 | // WKWebViewDemo 4 | // 5 | // Created by hujunhua on 2016/12/17. 6 | // Copyright © 2016年 hujunhua. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface PushVC : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/PushVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // PushVC.m 3 | // WKWebViewDemo 4 | // 5 | // Created by hujunhua on 2016/12/17. 6 | // Copyright © 2016年 hujunhua. All rights reserved. 7 | // 8 | 9 | #import "PushVC.h" 10 | 11 | @interface PushVC () 12 | 13 | @end 14 | 15 | @implementation PushVC 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | // Do any additional setup after loading the view. 20 | } 21 | 22 | - (void)didReceiveMemoryWarning { 23 | [super didReceiveMemoryWarning]; 24 | // Dispose of any resources that can be recreated. 25 | } 26 | 27 | /* 28 | #pragma mark - Navigation 29 | 30 | // In a storyboard-based application, you will often want to do a little preparation before navigation 31 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 32 | // Get the new view controller using [segue destinationViewController]. 33 | // Pass the selected object to the new view controller. 34 | } 35 | */ 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // WKWebViewDemo 4 | // 5 | // Created by hujunhua on 2016/11/17. 6 | // Copyright © 2016年 hujunhua. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // WKWebViewDemo 4 | // 5 | // Created by hujunhua on 2016/11/17. 6 | // Copyright © 2016年 hujunhua. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import 11 | #import "OCJSHelper.h" 12 | 13 | #define ScreenWidth [[UIScreen mainScreen] bounds].size.width 14 | #define ScreenHeight [[UIScreen mainScreen] bounds].size.height 15 | 16 | static CGFloat addViewHeight = 500; // 添加自定义 View 的高度 17 | static BOOL showAddView = YES; // 是否添加自定义 View 18 | static BOOL useEdgeInset = NO; // 用哪种方法添加自定义View, NO 使用 contentInset,YES 使用 div 19 | 20 | @interface ViewController () 21 | < 22 | WKNavigationDelegate, 23 | WKUIDelegate, 24 | UIScrollViewDelegate 25 | > 26 | 27 | @property (nonatomic, strong) WKWebView *webView; 28 | @property (nonatomic, strong) UIProgressView *progressView; 29 | @property (nonatomic, strong) OCJSHelper *ocjsHelper; 30 | @property (nonatomic, assign) CGFloat delayTime; 31 | @property (nonatomic, assign) CGFloat contentHeight; 32 | @property (nonatomic, strong) UIView *addView; 33 | 34 | //@property (nonatomic, strong) NSURLConnection *httpsUrlConnection; 35 | //@property (nonatomic, assign) BOOL httpsAuth; 36 | //@property (nonatomic, strong) NSURLRequest *originRequest; 37 | 38 | @end 39 | 40 | @implementation ViewController 41 | 42 | #pragma mark - Life Cycle 43 | 44 | // 用来测试的一些url链接 45 | - (NSURL *)testurl { 46 | NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"html"]; 47 | // NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"]; 48 | // NSURL *url = [NSURL URLWithString:@"https://github.com/"]; 49 | // NSURL *url = [NSURL URLWithString:@"https://z.yeemiao.com/share/share.html"]; // 自建证书,在iOS8下面,无法通过验证 50 | return url; 51 | } 52 | 53 | - (void)viewDidLoad { 54 | [super viewDidLoad]; 55 | 56 | WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; 57 | config.userContentController = [[WKUserContentController alloc] init]; 58 | // 交互对象设置 59 | [config.userContentController addScriptMessageHandler:(id)self.ocjsHelper name:@"timefor"]; 60 | // 支持内嵌视频播放,不然网页中的视频无法播放 61 | config.allowsInlineMediaPlayback = YES; 62 | self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config]; 63 | self.ocjsHelper.webView = self.webView; 64 | [self.view addSubview:self.webView]; 65 | 66 | self.webView.scrollView.delegate = self; 67 | 68 | self.webView.navigationDelegate = self; 69 | self.webView.UIDelegate = self; 70 | // 开始右滑返回手势 71 | self.webView.allowsBackForwardNavigationGestures = YES; 72 | 73 | NSURL *url = [self testurl]; 74 | [self.webView loadRequest:[NSURLRequest requestWithURL:url]]; 75 | 76 | self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 2)]; 77 | [self.view addSubview:self.progressView]; 78 | self.progressView.progressTintColor = [UIColor greenColor]; 79 | self.progressView.trackTintColor = [UIColor clearColor]; 80 | 81 | NSKeyValueObservingOptions observingOptions = NSKeyValueObservingOptionNew; 82 | // KVO 监听属性,除了下面列举的两个,还有其他的一些属性,具体参考 WKWebView 的头文件 83 | [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:observingOptions context:nil]; 84 | [self.webView addObserver:self forKeyPath:@"title" options:observingOptions context:nil]; 85 | 86 | // 监听 self.webView.scrollView 的 contentSize 属性改变,从而对底部添加的自定义 View 进行位置调整 87 | [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:observingOptions context:nil]; 88 | } 89 | 90 | - (void)dealloc { 91 | [self.webView removeObserver:self forKeyPath:@"estimatedProgress"]; 92 | [self.webView removeObserver:self forKeyPath:@"title"]; 93 | [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"]; 94 | [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"timefor"]; 95 | } 96 | 97 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 98 | if ([keyPath isEqualToString:@"estimatedProgress"]) { 99 | [self.progressView setProgress:self.webView.estimatedProgress animated:YES]; 100 | if (self.webView.estimatedProgress < 1.0) { 101 | self.delayTime = 1 - self.webView.estimatedProgress; 102 | return; 103 | } 104 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 105 | self.progressView.progress = 0; 106 | }); 107 | } else if ([keyPath isEqualToString:@"title"]) { 108 | self.title = self.webView.title; 109 | } else if ([keyPath isEqualToString:@"contentSize"]) { 110 | if (self.contentHeight != self.webView.scrollView.contentSize.height) { 111 | self.contentHeight = self.webView.scrollView.contentSize.height; 112 | self.addView.frame = CGRectMake(0, self.contentHeight - addViewHeight, ScreenWidth, addViewHeight); 113 | NSLog(@"----------%@", NSStringFromCGSize(self.webView.scrollView.contentSize)); 114 | } 115 | } 116 | } 117 | 118 | 119 | 120 | #pragma mark - WKNavigationDelegate 121 | 122 | // 开始加载时调用 123 | - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation { 124 | NSLog(@"didStartProvisionalNavigation ==== %@", navigation); 125 | } 126 | 127 | // 页面加载完调用 128 | - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation { 129 | NSLog(@"didFinishNavigation ==== %@", navigation); 130 | 131 | if (!showAddView) return; 132 | 133 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 134 | 135 | [self.webView.scrollView addSubview:self.addView]; 136 | 137 | if (useEdgeInset) { 138 | // url 使用 test.html 对比更明显 139 | self.webView.scrollView.contentInset = UIEdgeInsetsMake(64, 0, addViewHeight, 0); 140 | } else { 141 | NSString *js = [NSString stringWithFormat:@"\ 142 | var appendDiv = document.getElementById(\"AppAppendDIV\");\ 143 | if (appendDiv) {\ 144 | appendDiv.style.height = %@+\"px\";\ 145 | } else {\ 146 | var appendDiv = document.createElement(\"div\");\ 147 | appendDiv.setAttribute(\"id\",\"AppAppendDIV\");\ 148 | appendDiv.style.width=%@+\"px\";\ 149 | appendDiv.style.height=%@+\"px\";\ 150 | document.body.appendChild(appendDiv);\ 151 | }\ 152 | ", @(addViewHeight), @(ScreenWidth), @(addViewHeight)]; 153 | [self.webView evaluateJavaScript:js completionHandler:^(id value, NSError *error) { 154 | // 执行完上面的那段 JS, webView.scrollView.contentSize.height 的高度已经是加上 div 的高度 155 | self.addView.frame = CGRectMake(0, self.webView.scrollView.contentSize.height - addViewHeight, ScreenWidth, addViewHeight); 156 | }]; 157 | } 158 | }); 159 | } 160 | 161 | // 页面加载失败时调用 162 | - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error { 163 | NSLog(@"didFailProvisionalNavigation ==== %@\nerror ==== %@", navigation, error); 164 | } 165 | 166 | // 内容开始返回时调用 167 | - (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation { 168 | NSLog(@"didCommitNavigation ==== %@", navigation); 169 | } 170 | 171 | // 在发送请求之前,决定是否跳转 172 | - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { 173 | NSLog(@"decidePolicyForNavigationAction ==== %@", navigationAction); 174 | decisionHandler(WKNavigationActionPolicyAllow); 175 | } 176 | 177 | // 在收到响应后,决定是否跳转 178 | - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler { 179 | NSLog(@"decidePolicyForNavigationResponse ==== %@", navigationResponse); 180 | decisionHandler(WKNavigationResponsePolicyAllow); 181 | } 182 | 183 | // 加载 HTTPS 的链接,需要权限认证时调用 \ 如果 HTTPS 是用的证书在信任列表中这不要此代理方法 184 | - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { 185 | if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { 186 | if ([challenge previousFailureCount] == 0) { 187 | NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; 188 | completionHandler(NSURLSessionAuthChallengeUseCredential, credential); 189 | } else { 190 | completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); 191 | } 192 | } else { 193 | completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); 194 | } 195 | } 196 | 197 | #pragma mark - WKUIDelegate 198 | 199 | // 提示框 200 | - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler { 201 | UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示框" message:message preferredStyle:UIAlertControllerStyleAlert]; 202 | [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 203 | completionHandler(); 204 | }]]; 205 | [self presentViewController:alert animated:YES completion:NULL]; 206 | } 207 | 208 | // 确认框 209 | - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler { 210 | UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认框" message:message preferredStyle:UIAlertControllerStyleAlert]; 211 | [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 212 | completionHandler(YES); 213 | }]]; 214 | [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 215 | completionHandler(NO); 216 | }]]; 217 | [self presentViewController:alert animated:YES completion:NULL]; 218 | } 219 | 220 | // 输入框 221 | - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler { 222 | 223 | UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"输入框" message:prompt preferredStyle:UIAlertControllerStyleAlert]; 224 | [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 225 | textField.textColor = [UIColor blackColor]; 226 | textField.placeholder = defaultText; 227 | }]; 228 | [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 229 | completionHandler([[alert.textFields lastObject] text]); 230 | }]]; 231 | [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 232 | completionHandler(nil); 233 | }]]; 234 | [self presentViewController:alert animated:YES completion:NULL]; 235 | } 236 | 237 | #pragma mark - UIScrollViewDelegate 238 | 239 | - (void)scrollViewDidScroll:(UIScrollView *)scrollView { 240 | [self.webView evaluateJavaScript:@"parseFloat(document.getElementById(\"AppAppendDIV\").style.width);" completionHandler:^(id value, NSError * _Nullable error) { 241 | NSLog(@"======= %@", value); 242 | }]; 243 | } 244 | 245 | #pragma mark - Setter & Getter 246 | 247 | - (OCJSHelper *)ocjsHelper { 248 | if (!_ocjsHelper) { 249 | _ocjsHelper = [[OCJSHelper alloc] initWithDelegate:(id)self vc:self]; 250 | } 251 | return _ocjsHelper; 252 | } 253 | 254 | - (UIView *)addView { 255 | if (!_addView) { 256 | _addView = [[UIView alloc] init]; 257 | _addView.backgroundColor = [UIColor redColor]; 258 | } 259 | return _addView; 260 | } 261 | 262 | // UIWebView 使用的权限认证方式, 263 | //- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { 264 | // if ([navigationAction.request.URL.absoluteString containsString:@"https://"] && IOSVersion < 9.0 && !self.httpsAuth) { 265 | // self.originRequest = navigationAction.request; 266 | // self.httpsUrlConnection = [[NSURLConnection alloc] initWithRequest:self.originRequest delegate:self]; 267 | // [self.httpsUrlConnection start]; 268 | // decisionHandler(WKNavigationActionPolicyCancel); 269 | // return; 270 | // } 271 | // decisionHandler(WKNavigationActionPolicyAllow); 272 | //} 273 | // 274 | //- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 275 | // if ([challenge previousFailureCount] == 0) { 276 | // self.httpsAuth = YES; 277 | // NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; 278 | // [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; 279 | // } else { 280 | // [[challenge sender] cancelAuthenticationChallenge:challenge]; 281 | // } 282 | //} 283 | // 284 | //- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 285 | // self.httpsAuth = YES; 286 | // [self.webView loadRequest:self.originRequest]; 287 | // [self.httpsUrlConnection cancel]; 288 | //} 289 | // 290 | //- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 291 | // return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; 292 | //} 293 | 294 | @end 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // WKWebViewDemo 4 | // 5 | // Created by hujunhua on 2016/11/17. 6 | // Copyright © 2016年 hujunhua. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /WKWebViewDemo/WKWebViewDemo/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 22 | js call native api 23 | 24 | 30 | 31 | 32 | 33 |
    34 |
  • 弹提示框
  • 35 |
  • 弹确认框
  • 36 |
  • 弹输入框
  • 37 |
  • 获取设备ID
  • 38 | 39 |
40 |
41 | 42 | 64 | 65 | 66 | --------------------------------------------------------------------------------