├── .gitignore ├── CXYWebScript.podspec ├── CXYWebScript ├── CXYWebScript.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ ├── IDEWorkspaceChecks.plist │ │ │ └── WorkspaceSettings.xcsettings │ └── xcshareddata │ │ └── xcschemes │ │ └── CXYWebScript.xcscheme ├── CXYWebScript │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ │ ├── AccentColor.colorset │ │ │ └── Contents.json │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── Contents.json │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ ├── CXYWebScript-Bridging-Header.h │ ├── CXYWebScript │ │ ├── CXYWebScript.h │ │ └── CXYWebScript.m │ ├── DetailController.swift │ ├── Info.plist │ ├── SceneDelegate.h │ ├── SceneDelegate.m │ ├── ViewController.h │ ├── ViewController.m │ ├── demo.html │ └── main.m └── demo.html ├── LICENSE ├── README.md └── screenshot.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | # CocoaPods 34 | # 35 | # We recommend against adding the Pods directory to your .gitignore. However 36 | # you should judge for yourself, the pros and cons are mentioned at: 37 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 38 | # 39 | # Pods/ 40 | # 41 | # Add this line if you want to avoid checking in source code from the Xcode workspace 42 | # *.xcworkspace 43 | 44 | # Carthage 45 | # 46 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 47 | # Carthage/Checkouts 48 | 49 | Carthage/Build/ 50 | 51 | # fastlane 52 | # 53 | # It is recommended to not store the screenshots in the git repo. 54 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 55 | # For more information about the recommended setup visit: 56 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 57 | 58 | fastlane/report.xml 59 | fastlane/Preview.html 60 | fastlane/screenshots/**/*.png 61 | fastlane/test_output 62 | 63 | # Code Injection 64 | # 65 | # After new code Injection tools there's a generated folder /iOSInjectionProject 66 | # https://github.com/johnno1962/injectionforxcode 67 | 68 | iOSInjectionProject/ 69 | -------------------------------------------------------------------------------- /CXYWebScript.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod spec lint CXYWebScript.podspec' to ensure this is a 3 | # valid spec and to remove all comments including this before submitting the spec. 4 | # 5 | # To learn more about Podspec attributes see https://guides.cocoapods.org/syntax/podspec.html 6 | # To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ 7 | # 8 | 9 | Pod::Spec.new do |spec| 10 | 11 | # ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 12 | # 13 | # These will help people to find your library, and whilst it 14 | # can feel like a chore to fill in it's definitely to your advantage. The 15 | # summary should be tweet-length, and the description more in depth. 16 | # 17 | 18 | spec.name = "CXYWebScript" 19 | spec.version = "0.0.1" 20 | spec.summary = "CXYWebScript simplifies the interaction between App and H5(WKWebView)" 21 | 22 | # This description is used to generate tags and improve search results. 23 | # * Think: What does it do? Why did you write it? What is the focus? 24 | # * Try to keep it short, snappy and to the point. 25 | # * Write the description between the DESC delimiters below. 26 | # * Finally, don't worry about the indent, CocoaPods strips it! 27 | spec.description = <<-DESC 28 | CXYWebScript simplifies the interaction between App and H5(WKWebView). 29 | DESC 30 | 31 | spec.homepage = "https://github.com/iHongRen/CXYWebScript" 32 | spec.screenshots = "https://raw.githubusercontent.com/iHongRen/CXYWebScript/main/screenshot.png" 33 | 34 | 35 | # ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 36 | # 37 | # Licensing your code is important. See https://choosealicense.com for more info. 38 | # CocoaPods will detect a license file if there is a named LICENSE* 39 | # Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'. 40 | # 41 | 42 | # spec.license = "MIT (example)" 43 | spec.license = { :type => "MIT", :file => "LICENSE" } 44 | 45 | 46 | # ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 47 | # 48 | # Specify the authors of the library, with email addresses. Email addresses 49 | # of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also 50 | # accepts just a name if you'd rather not provide an email address. 51 | # 52 | # Specify a social_media_url where others can refer to, for example a twitter 53 | # profile URL. 54 | # 55 | 56 | spec.author = { "@cxy" => "iamhongren@gmail.com" } 57 | # Or just: spec.author = "chenxianyin" 58 | # spec.authors = { "chenxianyin" => "iamhongren@gmail.com" } 59 | spec.social_media_url = "https://github.com/iHongRen/CXYWebScript" 60 | 61 | # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 62 | # 63 | # If this Pod runs only on iOS or OS X, then specify the platform and 64 | # the deployment target. You can optionally include the target after the platform. 65 | # 66 | 67 | # spec.platform = :ios 68 | # spec.platform = :ios, "5.0" 69 | 70 | # When using multiple platforms 71 | spec.ios.deployment_target = "10.0" 72 | spec.osx.deployment_target = "10.13" 73 | # spec.watchos.deployment_target = "2.0" 74 | # spec.tvos.deployment_target = "9.0" 75 | # spec.visionos.deployment_target = "1.0" 76 | 77 | 78 | # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 79 | # 80 | # Specify the location from where the source should be retrieved. 81 | # Supports git, hg, bzr, svn and HTTP. 82 | # 83 | 84 | spec.source = { :git => "https://github.com/iHongRen/CXYWebScript.git", :tag => "#{spec.version}" } 85 | 86 | 87 | # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 88 | # 89 | # CocoaPods is smart about how it includes source code. For source files 90 | # giving a folder will include any swift, h, m, mm, c & cpp files. 91 | # For header files it will include any header in the folder. 92 | # Not including the public_header_files will make all headers public. 93 | # 94 | 95 | spec.source_files = "CXYWebScript/CXYWebScript/CXYWebScript/*.{h,m}" 96 | # spec.exclude_files = "Classes/Exclude" 97 | 98 | # spec.public_header_files = "Classes/**/*.h" 99 | 100 | 101 | end 102 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 56; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 8080C9912BC51730008408FB /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 8080C9902BC51730008408FB /* AppDelegate.m */; }; 11 | 8080C9942BC51730008408FB /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 8080C9932BC51730008408FB /* SceneDelegate.m */; }; 12 | 8080C9972BC51730008408FB /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8080C9962BC51730008408FB /* ViewController.m */; }; 13 | 8080C99A2BC51730008408FB /* Base in Resources */ = {isa = PBXBuildFile; fileRef = 8080C9992BC51730008408FB /* Base */; }; 14 | 8080C99C2BC51732008408FB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8080C99B2BC51732008408FB /* Assets.xcassets */; }; 15 | 8080C99F2BC51732008408FB /* Base in Resources */ = {isa = PBXBuildFile; fileRef = 8080C99E2BC51732008408FB /* Base */; }; 16 | 8080C9A22BC51732008408FB /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8080C9A12BC51732008408FB /* main.m */; }; 17 | 8080C9AA2BC5193C008408FB /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8080C9A92BC5193C008408FB /* WebKit.framework */; }; 18 | 8080C9AC2BC51B12008408FB /* demo.html in Resources */ = {isa = PBXBuildFile; fileRef = 8080C9AB2BC51B12008408FB /* demo.html */; }; 19 | 808869AA2BC7855F006900C0 /* DetailController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 808869A92BC7855F006900C0 /* DetailController.swift */; }; 20 | 80E48FDA2BC51F21000B67E2 /* CXYWebScript.m in Sources */ = {isa = PBXBuildFile; fileRef = 80E48FD92BC51F21000B67E2 /* CXYWebScript.m */; }; 21 | /* End PBXBuildFile section */ 22 | 23 | /* Begin PBXFileReference section */ 24 | 8080C98C2BC51730008408FB /* CXYWebScript.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CXYWebScript.app; sourceTree = BUILT_PRODUCTS_DIR; }; 25 | 8080C98F2BC51730008408FB /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 26 | 8080C9902BC51730008408FB /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 27 | 8080C9922BC51730008408FB /* SceneDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SceneDelegate.h; sourceTree = ""; }; 28 | 8080C9932BC51730008408FB /* SceneDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SceneDelegate.m; sourceTree = ""; }; 29 | 8080C9952BC51730008408FB /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 30 | 8080C9962BC51730008408FB /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 31 | 8080C9992BC51730008408FB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 32 | 8080C99B2BC51732008408FB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 33 | 8080C99E2BC51732008408FB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 34 | 8080C9A02BC51732008408FB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 35 | 8080C9A12BC51732008408FB /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 36 | 8080C9A92BC5193C008408FB /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; }; 37 | 8080C9AB2BC51B12008408FB /* demo.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = demo.html; sourceTree = ""; }; 38 | 808869A82BC7855E006900C0 /* CXYWebScript-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "CXYWebScript-Bridging-Header.h"; sourceTree = ""; }; 39 | 808869A92BC7855F006900C0 /* DetailController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailController.swift; sourceTree = ""; }; 40 | 80E48FD82BC51F21000B67E2 /* CXYWebScript.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CXYWebScript.h; sourceTree = ""; }; 41 | 80E48FD92BC51F21000B67E2 /* CXYWebScript.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CXYWebScript.m; sourceTree = ""; }; 42 | /* End PBXFileReference section */ 43 | 44 | /* Begin PBXFrameworksBuildPhase section */ 45 | 8080C9892BC51730008408FB /* Frameworks */ = { 46 | isa = PBXFrameworksBuildPhase; 47 | buildActionMask = 2147483647; 48 | files = ( 49 | 8080C9AA2BC5193C008408FB /* WebKit.framework in Frameworks */, 50 | ); 51 | runOnlyForDeploymentPostprocessing = 0; 52 | }; 53 | /* End PBXFrameworksBuildPhase section */ 54 | 55 | /* Begin PBXGroup section */ 56 | 8080C9832BC51730008408FB = { 57 | isa = PBXGroup; 58 | children = ( 59 | 8080C98E2BC51730008408FB /* CXYWebScript */, 60 | 8080C98D2BC51730008408FB /* Products */, 61 | 8080C9A82BC5193C008408FB /* Frameworks */, 62 | ); 63 | sourceTree = ""; 64 | }; 65 | 8080C98D2BC51730008408FB /* Products */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | 8080C98C2BC51730008408FB /* CXYWebScript.app */, 69 | ); 70 | name = Products; 71 | sourceTree = ""; 72 | }; 73 | 8080C98E2BC51730008408FB /* CXYWebScript */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | 80E48FD72BC51EF2000B67E2 /* CXYWebScript */, 77 | 8080C98F2BC51730008408FB /* AppDelegate.h */, 78 | 8080C9902BC51730008408FB /* AppDelegate.m */, 79 | 8080C9922BC51730008408FB /* SceneDelegate.h */, 80 | 8080C9932BC51730008408FB /* SceneDelegate.m */, 81 | 8080C9952BC51730008408FB /* ViewController.h */, 82 | 8080C9962BC51730008408FB /* ViewController.m */, 83 | 808869A92BC7855F006900C0 /* DetailController.swift */, 84 | 8080C9982BC51730008408FB /* Main.storyboard */, 85 | 8080C99B2BC51732008408FB /* Assets.xcassets */, 86 | 8080C9AB2BC51B12008408FB /* demo.html */, 87 | 8080C99D2BC51732008408FB /* LaunchScreen.storyboard */, 88 | 8080C9A02BC51732008408FB /* Info.plist */, 89 | 8080C9A12BC51732008408FB /* main.m */, 90 | 808869A82BC7855E006900C0 /* CXYWebScript-Bridging-Header.h */, 91 | ); 92 | path = CXYWebScript; 93 | sourceTree = ""; 94 | }; 95 | 8080C9A82BC5193C008408FB /* Frameworks */ = { 96 | isa = PBXGroup; 97 | children = ( 98 | 8080C9A92BC5193C008408FB /* WebKit.framework */, 99 | ); 100 | name = Frameworks; 101 | sourceTree = ""; 102 | }; 103 | 80E48FD72BC51EF2000B67E2 /* CXYWebScript */ = { 104 | isa = PBXGroup; 105 | children = ( 106 | 80E48FD82BC51F21000B67E2 /* CXYWebScript.h */, 107 | 80E48FD92BC51F21000B67E2 /* CXYWebScript.m */, 108 | ); 109 | path = CXYWebScript; 110 | sourceTree = ""; 111 | }; 112 | /* End PBXGroup section */ 113 | 114 | /* Begin PBXNativeTarget section */ 115 | 8080C98B2BC51730008408FB /* CXYWebScript */ = { 116 | isa = PBXNativeTarget; 117 | buildConfigurationList = 8080C9A52BC51732008408FB /* Build configuration list for PBXNativeTarget "CXYWebScript" */; 118 | buildPhases = ( 119 | 8080C9882BC51730008408FB /* Sources */, 120 | 8080C9892BC51730008408FB /* Frameworks */, 121 | 8080C98A2BC51730008408FB /* Resources */, 122 | ); 123 | buildRules = ( 124 | ); 125 | dependencies = ( 126 | ); 127 | name = CXYWebScript; 128 | productName = CXYWebScriptManager; 129 | productReference = 8080C98C2BC51730008408FB /* CXYWebScript.app */; 130 | productType = "com.apple.product-type.application"; 131 | }; 132 | /* End PBXNativeTarget section */ 133 | 134 | /* Begin PBXProject section */ 135 | 8080C9842BC51730008408FB /* Project object */ = { 136 | isa = PBXProject; 137 | attributes = { 138 | BuildIndependentTargetsInParallel = 1; 139 | LastUpgradeCheck = 1530; 140 | TargetAttributes = { 141 | 8080C98B2BC51730008408FB = { 142 | CreatedOnToolsVersion = 15.3; 143 | LastSwiftMigration = 1530; 144 | }; 145 | }; 146 | }; 147 | buildConfigurationList = 8080C9872BC51730008408FB /* Build configuration list for PBXProject "CXYWebScript" */; 148 | compatibilityVersion = "Xcode 14.0"; 149 | developmentRegion = en; 150 | hasScannedForEncodings = 0; 151 | knownRegions = ( 152 | en, 153 | Base, 154 | ); 155 | mainGroup = 8080C9832BC51730008408FB; 156 | productRefGroup = 8080C98D2BC51730008408FB /* Products */; 157 | projectDirPath = ""; 158 | projectRoot = ""; 159 | targets = ( 160 | 8080C98B2BC51730008408FB /* CXYWebScript */, 161 | ); 162 | }; 163 | /* End PBXProject section */ 164 | 165 | /* Begin PBXResourcesBuildPhase section */ 166 | 8080C98A2BC51730008408FB /* Resources */ = { 167 | isa = PBXResourcesBuildPhase; 168 | buildActionMask = 2147483647; 169 | files = ( 170 | 8080C99C2BC51732008408FB /* Assets.xcassets in Resources */, 171 | 8080C9AC2BC51B12008408FB /* demo.html in Resources */, 172 | 8080C99F2BC51732008408FB /* Base in Resources */, 173 | 8080C99A2BC51730008408FB /* Base in Resources */, 174 | ); 175 | runOnlyForDeploymentPostprocessing = 0; 176 | }; 177 | /* End PBXResourcesBuildPhase section */ 178 | 179 | /* Begin PBXSourcesBuildPhase section */ 180 | 8080C9882BC51730008408FB /* Sources */ = { 181 | isa = PBXSourcesBuildPhase; 182 | buildActionMask = 2147483647; 183 | files = ( 184 | 80E48FDA2BC51F21000B67E2 /* CXYWebScript.m in Sources */, 185 | 808869AA2BC7855F006900C0 /* DetailController.swift in Sources */, 186 | 8080C9972BC51730008408FB /* ViewController.m in Sources */, 187 | 8080C9912BC51730008408FB /* AppDelegate.m in Sources */, 188 | 8080C9A22BC51732008408FB /* main.m in Sources */, 189 | 8080C9942BC51730008408FB /* SceneDelegate.m in Sources */, 190 | ); 191 | runOnlyForDeploymentPostprocessing = 0; 192 | }; 193 | /* End PBXSourcesBuildPhase section */ 194 | 195 | /* Begin PBXVariantGroup section */ 196 | 8080C9982BC51730008408FB /* Main.storyboard */ = { 197 | isa = PBXVariantGroup; 198 | children = ( 199 | 8080C9992BC51730008408FB /* Base */, 200 | ); 201 | name = Main.storyboard; 202 | sourceTree = ""; 203 | }; 204 | 8080C99D2BC51732008408FB /* LaunchScreen.storyboard */ = { 205 | isa = PBXVariantGroup; 206 | children = ( 207 | 8080C99E2BC51732008408FB /* Base */, 208 | ); 209 | name = LaunchScreen.storyboard; 210 | sourceTree = ""; 211 | }; 212 | /* End PBXVariantGroup section */ 213 | 214 | /* Begin XCBuildConfiguration section */ 215 | 8080C9A32BC51732008408FB /* Debug */ = { 216 | isa = XCBuildConfiguration; 217 | buildSettings = { 218 | ALWAYS_SEARCH_USER_PATHS = NO; 219 | ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; 220 | CLANG_ANALYZER_NONNULL = YES; 221 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 222 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 223 | CLANG_ENABLE_MODULES = YES; 224 | CLANG_ENABLE_OBJC_ARC = YES; 225 | CLANG_ENABLE_OBJC_WEAK = YES; 226 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 227 | CLANG_WARN_BOOL_CONVERSION = YES; 228 | CLANG_WARN_COMMA = YES; 229 | CLANG_WARN_CONSTANT_CONVERSION = YES; 230 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 231 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 232 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 233 | CLANG_WARN_EMPTY_BODY = YES; 234 | CLANG_WARN_ENUM_CONVERSION = YES; 235 | CLANG_WARN_INFINITE_RECURSION = YES; 236 | CLANG_WARN_INT_CONVERSION = YES; 237 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 238 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 239 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 240 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 241 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 242 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 243 | CLANG_WARN_STRICT_PROTOTYPES = YES; 244 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 245 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 246 | CLANG_WARN_UNREACHABLE_CODE = YES; 247 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 248 | COPY_PHASE_STRIP = NO; 249 | DEBUG_INFORMATION_FORMAT = dwarf; 250 | ENABLE_STRICT_OBJC_MSGSEND = YES; 251 | ENABLE_TESTABILITY = YES; 252 | ENABLE_USER_SCRIPT_SANDBOXING = YES; 253 | GCC_C_LANGUAGE_STANDARD = gnu17; 254 | GCC_DYNAMIC_NO_PIC = NO; 255 | GCC_NO_COMMON_BLOCKS = YES; 256 | GCC_OPTIMIZATION_LEVEL = 0; 257 | GCC_PREPROCESSOR_DEFINITIONS = ( 258 | "DEBUG=1", 259 | "$(inherited)", 260 | ); 261 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 262 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 263 | GCC_WARN_UNDECLARED_SELECTOR = YES; 264 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 265 | GCC_WARN_UNUSED_FUNCTION = YES; 266 | GCC_WARN_UNUSED_VARIABLE = YES; 267 | IPHONEOS_DEPLOYMENT_TARGET = 12.0; 268 | LOCALIZATION_PREFERS_STRING_CATALOGS = YES; 269 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 270 | MTL_FAST_MATH = YES; 271 | ONLY_ACTIVE_ARCH = YES; 272 | SDKROOT = iphoneos; 273 | }; 274 | name = Debug; 275 | }; 276 | 8080C9A42BC51732008408FB /* Release */ = { 277 | isa = XCBuildConfiguration; 278 | buildSettings = { 279 | ALWAYS_SEARCH_USER_PATHS = NO; 280 | ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; 281 | CLANG_ANALYZER_NONNULL = YES; 282 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 283 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 284 | CLANG_ENABLE_MODULES = YES; 285 | CLANG_ENABLE_OBJC_ARC = YES; 286 | CLANG_ENABLE_OBJC_WEAK = YES; 287 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 288 | CLANG_WARN_BOOL_CONVERSION = YES; 289 | CLANG_WARN_COMMA = YES; 290 | CLANG_WARN_CONSTANT_CONVERSION = YES; 291 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 292 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 293 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 294 | CLANG_WARN_EMPTY_BODY = YES; 295 | CLANG_WARN_ENUM_CONVERSION = YES; 296 | CLANG_WARN_INFINITE_RECURSION = YES; 297 | CLANG_WARN_INT_CONVERSION = YES; 298 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 299 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 300 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 301 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 302 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 303 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 304 | CLANG_WARN_STRICT_PROTOTYPES = YES; 305 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 306 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 307 | CLANG_WARN_UNREACHABLE_CODE = YES; 308 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 309 | COPY_PHASE_STRIP = NO; 310 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 311 | ENABLE_NS_ASSERTIONS = NO; 312 | ENABLE_STRICT_OBJC_MSGSEND = YES; 313 | ENABLE_USER_SCRIPT_SANDBOXING = YES; 314 | GCC_C_LANGUAGE_STANDARD = gnu17; 315 | GCC_NO_COMMON_BLOCKS = YES; 316 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 317 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 318 | GCC_WARN_UNDECLARED_SELECTOR = YES; 319 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 320 | GCC_WARN_UNUSED_FUNCTION = YES; 321 | GCC_WARN_UNUSED_VARIABLE = YES; 322 | IPHONEOS_DEPLOYMENT_TARGET = 12.0; 323 | LOCALIZATION_PREFERS_STRING_CATALOGS = YES; 324 | MTL_ENABLE_DEBUG_INFO = NO; 325 | MTL_FAST_MATH = YES; 326 | SDKROOT = iphoneos; 327 | VALIDATE_PRODUCT = YES; 328 | }; 329 | name = Release; 330 | }; 331 | 8080C9A62BC51732008408FB /* Debug */ = { 332 | isa = XCBuildConfiguration; 333 | buildSettings = { 334 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 335 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 336 | CLANG_ENABLE_MODULES = YES; 337 | CODE_SIGN_STYLE = Automatic; 338 | CURRENT_PROJECT_VERSION = 1; 339 | DEVELOPMENT_TEAM = C98SFK75FC; 340 | GENERATE_INFOPLIST_FILE = YES; 341 | INFOPLIST_FILE = CXYWebScript/Info.plist; 342 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 343 | INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; 344 | INFOPLIST_KEY_UIMainStoryboardFile = Main; 345 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 346 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 347 | IPHONEOS_DEPLOYMENT_TARGET = 13.0; 348 | LD_RUNPATH_SEARCH_PATHS = ( 349 | "$(inherited)", 350 | "@executable_path/Frameworks", 351 | ); 352 | MARKETING_VERSION = 1.0; 353 | PRODUCT_BUNDLE_IDENTIFIER = com.cxy.ihongren.webscript; 354 | PRODUCT_NAME = "$(TARGET_NAME)"; 355 | SWIFT_EMIT_LOC_STRINGS = YES; 356 | SWIFT_OBJC_BRIDGING_HEADER = "CXYWebScript/CXYWebScript-Bridging-Header.h"; 357 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 358 | SWIFT_VERSION = 5.0; 359 | TARGETED_DEVICE_FAMILY = "1,2"; 360 | }; 361 | name = Debug; 362 | }; 363 | 8080C9A72BC51732008408FB /* Release */ = { 364 | isa = XCBuildConfiguration; 365 | buildSettings = { 366 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 367 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 368 | CLANG_ENABLE_MODULES = YES; 369 | CODE_SIGN_STYLE = Automatic; 370 | CURRENT_PROJECT_VERSION = 1; 371 | DEVELOPMENT_TEAM = C98SFK75FC; 372 | GENERATE_INFOPLIST_FILE = YES; 373 | INFOPLIST_FILE = CXYWebScript/Info.plist; 374 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 375 | INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; 376 | INFOPLIST_KEY_UIMainStoryboardFile = Main; 377 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 378 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 379 | IPHONEOS_DEPLOYMENT_TARGET = 13.0; 380 | LD_RUNPATH_SEARCH_PATHS = ( 381 | "$(inherited)", 382 | "@executable_path/Frameworks", 383 | ); 384 | MARKETING_VERSION = 1.0; 385 | PRODUCT_BUNDLE_IDENTIFIER = com.cxy.ihongren.webscript; 386 | PRODUCT_NAME = "$(TARGET_NAME)"; 387 | SWIFT_EMIT_LOC_STRINGS = YES; 388 | SWIFT_OBJC_BRIDGING_HEADER = "CXYWebScript/CXYWebScript-Bridging-Header.h"; 389 | SWIFT_VERSION = 5.0; 390 | TARGETED_DEVICE_FAMILY = "1,2"; 391 | }; 392 | name = Release; 393 | }; 394 | /* End XCBuildConfiguration section */ 395 | 396 | /* Begin XCConfigurationList section */ 397 | 8080C9872BC51730008408FB /* Build configuration list for PBXProject "CXYWebScript" */ = { 398 | isa = XCConfigurationList; 399 | buildConfigurations = ( 400 | 8080C9A32BC51732008408FB /* Debug */, 401 | 8080C9A42BC51732008408FB /* Release */, 402 | ); 403 | defaultConfigurationIsVisible = 0; 404 | defaultConfigurationName = Release; 405 | }; 406 | 8080C9A52BC51732008408FB /* Build configuration list for PBXNativeTarget "CXYWebScript" */ = { 407 | isa = XCConfigurationList; 408 | buildConfigurations = ( 409 | 8080C9A62BC51732008408FB /* Debug */, 410 | 8080C9A72BC51732008408FB /* Release */, 411 | ); 412 | defaultConfigurationIsVisible = 0; 413 | defaultConfigurationName = Release; 414 | }; 415 | /* End XCConfigurationList section */ 416 | }; 417 | rootObject = 8080C9842BC51730008408FB /* Project object */; 418 | } 419 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript.xcodeproj/xcshareddata/xcschemes/CXYWebScript.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 9 | 10 | 16 | 22 | 23 | 24 | 25 | 26 | 32 | 33 | 43 | 45 | 51 | 52 | 53 | 54 | 58 | 59 | 60 | 61 | 67 | 69 | 75 | 76 | 77 | 78 | 80 | 81 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // CXYWebScriptManager 4 | // 5 | // Created by cxy on 2024/4/9. 6 | // 7 | 8 | #import 9 | 10 | @interface AppDelegate : UIResponder 11 | 12 | 13 | @end 14 | 15 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // CXYWebScriptManager 4 | // 5 | // Created by cxy on 2024/4/9. 6 | // 7 | 8 | #import "AppDelegate.h" 9 | 10 | @interface AppDelegate () 11 | 12 | @end 13 | 14 | @implementation AppDelegate 15 | 16 | 17 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 18 | // Override point for customization after application launch. 19 | return YES; 20 | } 21 | 22 | 23 | #pragma mark - UISceneSession lifecycle 24 | 25 | 26 | - (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options { 27 | // Called when a new scene session is being created. 28 | // Use this method to select a configuration to create the new scene with. 29 | return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role]; 30 | } 31 | 32 | 33 | - (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet *)sceneSessions { 34 | // Called when the user discards a scene session. 35 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 36 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 37 | } 38 | 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "platform" : "ios", 6 | "size" : "1024x1024" 7 | } 8 | ], 9 | "info" : { 10 | "author" : "xcode", 11 | "version" : 1 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/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 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/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 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 66 | 74 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/CXYWebScript-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // Use this file to import your target's public headers that you would like to expose to Swift. 3 | // 4 | 5 | #import "CXYWebScript.h" 6 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/CXYWebScript/CXYWebScript.h: -------------------------------------------------------------------------------- 1 | // 2 | // CXYWebScript.h 3 | // CXYWebScript 4 | // 5 | // Created by cxy on 2024/4/9. 6 | // 7 | 8 | #import 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | typedef void(^CXYStrBlock)(NSString* _Nullable str); 14 | typedef void(^CXYAsyncBlock)(NSArray *args, CXYStrBlock returnBlock); 15 | 16 | typedef NSString* _Nullable (^CXYBlock)(NSArray *args); 17 | 18 | @interface CXYWebScript : NSObject 19 | 20 | @property (nonatomic, strong, readonly) WKWebView *webView; 21 | 22 | - (instancetype)initWithWebView:(WKWebView*)webView; 23 | - (instancetype)initWithWebView:(WKWebView*)webView injectName:(nullable NSString*)injectName; 24 | 25 | - (void)useUIDelegate; 26 | 27 | - (void)addJsFunc:(NSString*)jsFunc block:(CXYBlock)block; 28 | 29 | // OC是异步的,js是同步的 30 | - (void)addJsFunc:(NSString*)jsFunc asyncBlock:(CXYAsyncBlock)block; 31 | 32 | - (void)addTarget:(id)target jsFunc:(NSString*)jsFunc ocSel:(SEL)sel; 33 | - (void)removeScripts; 34 | 35 | - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id result, NSError * _Nullable error))completionHandler; 36 | 37 | - (void)runJavaScriptPrompt:(NSString *)prompt 38 | defaultText:(NSString *)defaultText 39 | completionHandler:(void (^)(NSString * _Nullable))completionHandler; 40 | 41 | @end 42 | 43 | NS_ASSUME_NONNULL_END 44 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/CXYWebScript/CXYWebScript.m: -------------------------------------------------------------------------------- 1 | // 2 | // CXYWebScript.m 3 | // CXYWebScript 4 | // 5 | // Created by cxy on 2024/4/9. 6 | // 7 | 8 | #import "CXYWebScript.h" 9 | 10 | @interface NSObject (CXY) 11 | @end 12 | 13 | @implementation NSObject (CXY) 14 | 15 | - (id)cxy_performSelector:(SEL)aSelector withObjects:(NSArray *)objects { 16 | if (!aSelector || ![self respondsToSelector:aSelector] || (objects && ![objects isKindOfClass:[NSArray class]])) { 17 | return nil; 18 | } 19 | NSMethodSignature *signature = [self methodSignatureForSelector:aSelector]; 20 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 21 | [invocation setTarget:self]; 22 | [invocation setSelector:aSelector]; 23 | NSInteger arguments = [signature numberOfArguments]-2; 24 | NSInteger count = MIN(arguments, objects.count); 25 | for (NSUInteger i=0; i0) { 145 | NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding]; 146 | NSArray *ret = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 147 | return ret?:@[]; 148 | } 149 | return @[]; 150 | } 151 | 152 | - (void)runJavaScriptPrompt:(NSString *)prompt 153 | defaultText:(NSString *)defaultText 154 | completionHandler:(void (^)(NSString * _Nullable))completionHandler { 155 | NSArray *args = [self arrayWithJSON:defaultText]; 156 | if (_blockMap[prompt]) { 157 | CXYBlock block = _blockMap[prompt]; 158 | NSString *ret = block(args); 159 | [self completion:ret handler:completionHandler]; 160 | } else if (_asyncBlockMap[prompt]) { 161 | CXYAsyncBlock asyncBlock = _asyncBlockMap[prompt]; 162 | asyncBlock(args, ^(NSString *ret){ 163 | [self completion:ret handler:completionHandler]; 164 | }); 165 | } else { 166 | NSString *ret = nil; 167 | NSString *selString = self.scriptMap[prompt]; 168 | NSObject *target = [self.targetMap objectForKey:prompt]; 169 | if (selString && target) { 170 | SEL sel = NSSelectorFromString(selString); 171 | ret = [target cxy_performSelector:sel withObjects:args]; 172 | } 173 | [self completion:ret handler:completionHandler]; 174 | } 175 | } 176 | 177 | - (void)completion:(NSString*)res handler:(void (^)(NSString * _Nullable))completionHandler { 178 | NSString *ret = res; 179 | if (ret && ![ret isKindOfClass:NSString.class]) { 180 | NSAssert(NO, @"只接受字符串或nil类型的返回值"); 181 | ret = nil; 182 | } 183 | if (completionHandler) { 184 | completionHandler(ret); 185 | } 186 | } 187 | 188 | #pragma mark - WKUIDelegate 189 | - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler { 190 | [self runJavaScriptPrompt:prompt 191 | defaultText:defaultText 192 | completionHandler:completionHandler]; 193 | } 194 | 195 | @end 196 | 197 | 198 | 199 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/DetailController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DetailController.swift 3 | // CXYWebScript 4 | // 5 | // Created by cxy on 2024/4/11. 6 | // 7 | 8 | import UIKit 9 | 10 | class DetailController: UIViewController { 11 | 12 | @IBOutlet weak var webView: WKWebView! 13 | 14 | private lazy var webScript: CXYWebScript = { 15 | let webScript = CXYWebScript(webView: webView) 16 | webScript.useUIDelegate() 17 | return webScript 18 | }() 19 | 20 | override func viewDidLoad() { 21 | super.viewDidLoad() 22 | let url = Bundle.main.url(forResource: "demo", withExtension: "html")! 23 | webView.load(URLRequest(url: url)) 24 | webView.navigationDelegate = self; 25 | setupWebScript() 26 | } 27 | 28 | deinit { 29 | print("deinit") 30 | } 31 | 32 | func setupWebScript() { 33 | 34 | webScript.addJsFunc("onSayHello") { args in 35 | print(args) 36 | return "只支持返回字符串或nil,如何需要返回其他类型,可先将其转为JSON字符串再返回" 37 | } 38 | 39 | // async-block 方式,Swift 异步返回,js 同步接收返回数据 40 | // webScript.addJsFunc("onSayHello") { args, returnBlock in 41 | // print(args) 42 | // DispatchQueue.main.asyncAfter(deadline: .now()+2) { 43 | // // 只支持返回字符串或nil,如何需要返回其他类型,可先将其转为JSON字符串再返回 44 | // returnBlock("我是异步返回值,2秒后才返回"); // returnBlock 必须要执行 45 | // } 46 | // } 47 | 48 | webScript.addTarget(self, jsFunc: "onPreviewImages", ocSel: #selector(onPreviewImages(_:_:))) 49 | 50 | webScript.addTarget(self, jsFunc: "onShareObj", ocSel: #selector(onShareObject(_:))) 51 | 52 | webScript.addJsFunc("onJumpToPage") { [weak self] args in 53 | self?.navigationController?.popViewController(animated: true) 54 | return "" 55 | } 56 | 57 | } 58 | 59 | @objc func onPreviewImages(_ imgs: [String], _ currentIndex: NSNumber) { 60 | print(imgs) 61 | print(currentIndex) 62 | } 63 | 64 | @objc func onShareObject(_ obj: AnyObject) { 65 | print(obj) 66 | } 67 | 68 | } 69 | 70 | extension DetailController: WKNavigationDelegate { 71 | func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { 72 | 73 | // 修改第四个按钮的标题 74 | self.webScript.evaluateJavaScript("modifyBtn4Title(\"返回\")") { res, err in 75 | 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | UIApplicationSceneManifest 6 | 7 | UIApplicationSupportsMultipleScenes 8 | 9 | UISceneConfigurations 10 | 11 | UIWindowSceneSessionRoleApplication 12 | 13 | 14 | UISceneConfigurationName 15 | Default Configuration 16 | UISceneDelegateClassName 17 | SceneDelegate 18 | UISceneStoryboardFile 19 | Main 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/SceneDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // SceneDelegate.h 3 | // CXYWebScriptManager 4 | // 5 | // Created by cxy on 2024/4/9. 6 | // 7 | 8 | #import 9 | 10 | @interface SceneDelegate : UIResponder 11 | 12 | @property (strong, nonatomic) UIWindow * window; 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/SceneDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // SceneDelegate.m 3 | // CXYWebScriptManager 4 | // 5 | // Created by cxy on 2024/4/9. 6 | // 7 | 8 | #import "SceneDelegate.h" 9 | 10 | @interface SceneDelegate () 11 | 12 | @end 13 | 14 | @implementation SceneDelegate 15 | 16 | 17 | - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions { 18 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 19 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 20 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 21 | } 22 | 23 | 24 | - (void)sceneDidDisconnect:(UIScene *)scene { 25 | // Called as the scene is being released by the system. 26 | // This occurs shortly after the scene enters the background, or when its session is discarded. 27 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 28 | // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead). 29 | } 30 | 31 | 32 | - (void)sceneDidBecomeActive:(UIScene *)scene { 33 | // Called when the scene has moved from an inactive state to an active state. 34 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 35 | } 36 | 37 | 38 | - (void)sceneWillResignActive:(UIScene *)scene { 39 | // Called when the scene will move from an active state to an inactive state. 40 | // This may occur due to temporary interruptions (ex. an incoming phone call). 41 | } 42 | 43 | 44 | - (void)sceneWillEnterForeground:(UIScene *)scene { 45 | // Called as the scene transitions from the background to the foreground. 46 | // Use this method to undo the changes made on entering the background. 47 | } 48 | 49 | 50 | - (void)sceneDidEnterBackground:(UIScene *)scene { 51 | // Called as the scene transitions from the foreground to the background. 52 | // Use this method to save data, release shared resources, and store enough scene-specific state information 53 | // to restore the scene back to its current state. 54 | } 55 | 56 | 57 | @end 58 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // CXYWebScriptManager 4 | // 5 | // Created by cxy on 2024/4/9. 6 | // 7 | 8 | #import 9 | 10 | @interface ViewController : UIViewController 11 | 12 | 13 | @end 14 | 15 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // CXYWebScriptManager 4 | // 5 | // Created by cxy on 2024/4/9. 6 | // 7 | 8 | #import "ViewController.h" 9 | #import "CXYWebScript.h" 10 | #import "CXYWebScript-Swift.h" 11 | 12 | @interface ViewController () 13 | @property (weak, nonatomic) IBOutlet WKWebView *webView; 14 | @property (nonatomic, strong) CXYWebScript *webScript; 15 | @end 16 | 17 | @implementation ViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | [self configWebView]; 22 | } 23 | 24 | - (void)dealloc { 25 | [self.webScript removeScripts]; 26 | } 27 | 28 | - (void)configWebView { 29 | NSURL *url = [[NSBundle mainBundle] URLForResource:@"demo" withExtension:@"html"]; 30 | [self.webView loadRequest:[NSURLRequest requestWithURL:url]]; 31 | 32 | if (@available(iOS 16.4, *)) { 33 | self.webView.inspectable = YES; 34 | } 35 | 36 | [self setupWebScript]; 37 | } 38 | 39 | - (void)setupWebScript { 40 | self.webScript = [[CXYWebScript alloc] initWithWebView:self.webView]; 41 | [self.webScript useUIDelegate]; 42 | 43 | /** 自定义injectName: CXY,那么 JS调用时就是 window.CXY.onSayHello('Hello') */ 44 | //self.webScript = [[CXYWebScript alloc] initWithWebView:self.webView injectName:@"CXY"]; 45 | 46 | /** 如果不使用[self.webScript useUIDelegate]; 就要自己实现UIDelegate代理 47 | //self.webView.UIDelegate = self; 48 | 49 | - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler { 50 | 51 | [self.webScript runJavaScriptPrompt:prompt 52 | defaultText:defaultText 53 | completionHandler:completionHandler]; 54 | } 55 | */ 56 | 57 | /* 添加与H5交互的方法 */ 58 | // 添加了相同的 jsFunc ,执行优先 block > async-block > target-action 59 | 60 | 61 | // 使用 target-action 方式 62 | [self.webScript addTarget:self 63 | jsFunc:@"onSayHello" 64 | ocSel:@selector(onSayHello:)]; 65 | 66 | // 使用 block 方式,可同步返回值 67 | // __weak typeof(self) weakSelf = self; 68 | // [self.webScript addJsFunc:@"onSayHello" block:^NSString * _Nullable(NSArray *args) { 69 | // NSLog(@"args: %@", args); 70 | // NSLog(@"%@", weakSelf.webView.URL); 71 | // return @"只支持返回字符串或nil,如何需要返回其他类型,可先将其转为JSON字符串再返回"; 72 | // }]; 73 | 74 | // 使用 async-block 可异步返回值,返回值类型只支持字符串或nil,其他类型,可先将其转为JSON字符串 75 | // 这种方式OC是异步的,js是同步的 76 | [self.webScript addJsFunc:@"onSayHello" asyncBlock:^(NSArray * _Nonnull args, CXYStrBlock _Nonnull returnBlock) { 77 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 78 | returnBlock(@"我是异步返回值,2秒后才返回"); // returnBlock 必须要执行 79 | }); 80 | }]; 81 | 82 | [self.webScript addTarget:self 83 | jsFunc:@"onPreviewImages" 84 | ocSel:@selector(onPreviewImages:currentIndex:)]; 85 | 86 | [self.webScript addTarget:self 87 | jsFunc:@"onShareObj" 88 | ocSel:@selector(onShareObject:)]; 89 | 90 | [self.webScript addTarget:self 91 | jsFunc:@"onJumpToPage" 92 | ocSel:@selector(onJumpToPage:)]; 93 | 94 | 95 | } 96 | 97 | - (NSString*)onSayHello:(NSString*)param { 98 | NSLog(@"param: %@", param); 99 | return @"只支持返回字符串或nil,如何需要返回其他类型,可先将其转为JSON字符串再返回"; 100 | } 101 | 102 | - (void)onPreviewImages:(NSArray*)images currentIndex:(NSString*)currentIndex { 103 | NSLog(@"images: %@", images); 104 | NSLog(@"content: %@", currentIndex); 105 | } 106 | 107 | - (void)onShareObject:(id)obj { 108 | NSLog(@"obj-class: %@", [obj class]); //__NSDictionaryI 109 | NSLog(@"obj: %@", obj); 110 | } 111 | 112 | - (void)onJumpToPage:(NSString*)url { 113 | // page=xxx, 你可以解析url,跳转到指定页面 114 | DetailController *c = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailController"]; 115 | [self.navigationController pushViewController:c animated:YES]; 116 | } 117 | 118 | // 缩放字体 119 | - (IBAction)onScaleFontClick:(UIButton*)sender { 120 | sender.selected = !sender.isSelected; 121 | CGFloat scale = sender.selected ? 100*1.3 : 100; 122 | 123 | NSString *textJS = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust='%@%%'",@(scale)]; 124 | [self.webScript evaluateJavaScript:textJS completionHandler:^(id result, NSError * _Nullable error) { 125 | NSLog(@"result: %@", result); 126 | }]; 127 | } 128 | 129 | // 修改body背景色 130 | - (IBAction)onChangeBgColor:(id)sender { 131 | // 生成随机的16进制颜色值 132 | NSString *hexColor = [NSString stringWithFormat:@"#%06X", arc4random_uniform(0xFFFFFF)]; 133 | NSString *js = [NSString stringWithFormat:@"onChangeTheme(\"%@\")",hexColor]; 134 | [self.webScript evaluateJavaScript:js completionHandler:^(id _Nullable result, NSError * _Nullable error) { 135 | NSLog(@"result: %@", result); //'修改成功' 136 | }]; 137 | } 138 | 139 | - (IBAction)onReload:(id)sender { 140 | [self.webView reload]; 141 | } 142 | 143 | //#pragma mark - WKUIDelegate 144 | //- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler { 145 | // 146 | // [self.webScript runJavaScriptPrompt:prompt 147 | // defaultText:defaultText 148 | // completionHandler:completionHandler]; 149 | //} 150 | @end 151 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/demo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | H5与App交互 7 | 8 | 9 | 10 | 24 | 25 |

H5与App交互

26 | 27 | 28 | 29 | 30 | 31 |

32 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /CXYWebScript/CXYWebScript/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // CXYWebScriptManager 4 | // 5 | // Created by cxy on 2024/4/9. 6 | // 7 | 8 | #import 9 | #import "AppDelegate.h" 10 | 11 | int main(int argc, char * argv[]) { 12 | NSString * appDelegateClassName; 13 | @autoreleasepool { 14 | // Setup code that might create autoreleased objects goes here. 15 | appDelegateClassName = NSStringFromClass([AppDelegate class]); 16 | } 17 | return UIApplicationMain(argc, argv, nil, appDelegateClassName); 18 | } 19 | -------------------------------------------------------------------------------- /CXYWebScript/demo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | H5与App交互 7 | 8 | 9 | 10 | 23 | 24 |

H5与App交互

25 | 26 | 27 | 28 | 29 | 30 |

31 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 iHongRen 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 | # [CXYWebScript](https://github.com/iHongRen/CXYWebScript) 2 | 简化 iOS App 与 H5 交互,H5 直接调用 window.App?.onSayHello('Hello'),即可完成对原生 App `onSayHello:` 方法的调用。如此,与 H5 在 Android 端调用一致。 3 | 4 | 支持 **CocoaPods** 5 | 6 | ```ruby 7 | pod 'CXYWebScript' 8 | ``` 9 | 10 | 11 | 12 | ##### H5 端的简化 13 | 14 | ```js 15 | // 以前 需要区分环境 16 | if (isAndroid) { 17 | window.App.onSayHello('Hello'); 18 | } 19 | 20 | if (isiOS) { 21 | window.webkit.messageHandlers.onSayHello.postMessage('Hello'); 22 | } 23 | 24 | // 现在 iOS使用CXYWebScript后, H5无需引入任何库,iOS和Android统一调用: 25 | window.App?.onSayHello('Hello') 26 | ``` 27 | 28 | > Tip1: 以上 window.App 是可以自定义的,如 window.CXY 29 | > 30 | > Tip2: 判断当前环境是客户端还是其他H5端,可直接使用 if (window.App) 就行 31 | 32 | 33 | 34 | ##### iOS 端的简化 35 | 36 | ```objective-c 37 | // 以前 38 | CXYWeakScriptMsgHander *weakSelf = [[CXYWeakScriptMsgHander alloc] initWithHandler:self]; 39 | WKUserContentController *contentController = self.webView.configuration.userContentController; 40 | [contentController addScriptMessageHandler:weakSelf name:@"onSayHello"]; 41 | [contentController addScriptMessageHandler:weakSelf name:@"onPreviewImages"]; 42 | [contentController addScriptMessageHandler:weakSelf name:@"onShareObj"]; 43 | 44 | #pragma mark - WKScriptMessageHandler 45 | - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { 46 | 47 | NSString *selector = message.name; 48 | id body = message.body; 49 | 50 | if ([selector isEqualToString:@"onSayHello"]) { 51 | 52 | } else if ([selector isEqualToString:@"onPreviewImages"]) { 53 | 54 | } else if ([selector isEqualToString:@"onShareObj"]) { 55 | 56 | } 57 | } 58 | 59 | // 现在 60 | self.webScript = [[CXYWebScript alloc] initWithWebView:self.webView]; 61 | [self.webScript useUIDelegate]; 62 | 63 | // block 方式 64 | [self.webScript addJsFunc:@"onSayHello" block:^NSString * _Nullable(NSArray *args) { 65 | return @"支持返回字符串或nil,给JS"; 66 | }]; 67 | 68 | [self.webScript addJsFunc:@"onPreviewImages" block:^NSString * _Nullable(NSArray *args) { 69 | return @"支持返回字符串或nil,给JS"; 70 | }]; 71 | 72 | // 或者 action-target 方式 73 | [self.webScript addTarget:self 74 | jsFunc:@"onPreviewImages" 75 | ocSel:@selector(onPreviewImages:currentIndex:)]; 76 | 77 | [self.webScript addTarget:self 78 | jsFunc:@"onShareObj" 79 | ocSel:@selector(onShareObject:)]; 80 | 81 | ``` 82 | 83 | 84 | 85 | ### [CXYWebScript](https://github.com/iHongRen/CXYWebScript) 有以下特点: 86 | 87 | - H5 在调用原生 App 方法是时,不需要区分是 Android 还是 iOS 环境 88 | - 支持使用 block,async-block 和 target-action 方式注册 js 方法 89 | - 支持原生 App 传递返回值给 H5 (限字符串类型或 nil ) 90 | - 支持 OC 与 Swift, **iOS 10+** 91 | - 不到 **200** 行代码 92 | 93 | 94 | 95 | 96 | 97 | ### 如何使用 98 | 99 | #### H5 端 JS代码,详细见 [Demo.html](./CXYWebScript/Demo.html) 100 | 101 | ```js 102 | // 调用原生App方法,并能接收App端方法的返回值 103 | function onBtn1Click() { 104 | if (window.App) { 105 | // 接收 App 端的返回值 106 | const ret = window.App.onSayHello('Hello') 107 | console.log(ret) 108 | } 109 | } 110 | 111 | // 传递多个参数给App端 112 | function onBtn2Click() { 113 | const imgs = [ 114 | 'https://www.baidu.com/img/1.png', 115 | 'https://www.baidu.com/img/2.png', 116 | 'https://www.baidu.com/img/3.png'] 117 | 118 | window.App?.onPreviewImages(imgs, 1) 119 | } 120 | 121 | // App 端调用此方法,修改body背景 122 | function onChangeTheme(theme) { 123 | document.body.style.backgroundColor = theme 124 | return '修改成功' // 返回一个字符串给App端 125 | } 126 | ``` 127 | 128 | #### App 端 OC代码,详细见 [ViewController.m](./CXYWebScript/CXYWebScript/ViewController.m): 129 | 130 | ```objective-c 131 | #import "CXYWebScript.h" 132 | 133 | - (void)setupWebScript { 134 | // 初始化 CXYWebScript 135 | self.webScript = [[CXYWebScript alloc] initWithWebView:self.webView]; 136 | [self.webScript useUIDelegate]; 137 | 138 | /* 添加与H5交互的方法 */ 139 | // 添加了相同的 jsFunc ,执行优先 block > async-block > target-action 140 | 141 | // 使用 target-action 方式 142 | [self.webScript addTarget:self 143 | jsFunc:@"onSayHello" 144 | ocSel:@selector(onSayHello:)]; 145 | 146 | // 使用 block 方式, 147 | // 如果 target-action 和 block 添加了相同的 jsFunc ,则只执行 block 方式的 148 | // block 方式因为参数封装在数组里,所以参数意义不是很明确 149 | __weak typeof(self) weakSelf = self; 150 | [self.webScript addJsFunc:@"onSayHello" block:^NSString * _Nullable(NSArray *args) { 151 | NSLog(@"args: %@", args); 152 | NSLog(@"%@", weakSelf.webView.URL); 153 | return @"只支持返回字符串或nil,如何需要返回其他类型,可先将其转为JSON字符串再返回"; 154 | }]; 155 | 156 | // 使用 async-block 可异步返回值,返回值类型只支持字符串或nil,其他类型,可先将其转为JSON字符串 157 | // 这种方式OC是异步的,js是同步的 158 | [self.webScript addJsFunc:@"onSayHello" asyncBlock:^(NSArray * _Nonnull args, CXYStrBlock _Nonnull returnBlock) { 159 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 160 | returnBlock(@"我是异步返回值,2秒后才返回"); // returnBlock 必须要执行 161 | }); 162 | }]; 163 | 164 | // 多参数使用 165 | [self.webScript addTarget:self 166 | jsFunc:@"onPreviewImages" 167 | ocSel:@selector(onPreviewImages:currentIndex:)]; 168 | 169 | } 170 | 171 | - (NSString*)onSayHello:(NSString*)param { 172 | NSLog(@"param: %@", param); 173 | return @"只支持返回字符串或nil,如何需要返回其他类型,可先将其转为JSON字符串再返回"; 174 | } 175 | 176 | - (void)onPreviewImages:(NSArray*)images currentIndex:(NSString*)currentIndex { 177 | NSLog(@"images: %@", images); 178 | NSLog(@"content: %@", currentIndex); 179 | } 180 | 181 | // 修改body背景色 182 | - (IBAction)onChangeBgColor:(id)sender { 183 | NSString *hexColor = [NSString stringWithFormat:@"#%06X", arc4random_uniform(0xFFFFFF)]; 184 | NSString *js = [NSString stringWithFormat:@"onChangeTheme(\"%@\")",hexColor]; 185 | 186 | // App 调用 H5 方法 187 | [self.webScript evaluateJavaScript:js completionHandler:^(id _Nullable result, NSError * _Nullable error) { 188 | NSLog(@"result: %@", result); //'修改成功' 189 | }]; 190 | } 191 | 192 | ``` 193 | 194 | 195 | 196 | #### Swift 中使用,详细见 [DetailController.swift](./CXYWebScript/CXYWebScript/DetailController.swift) 197 | 198 | ```swift 199 | // 懒加载初始化 200 | private lazy var webScript: CXYWebScript = { 201 | let webScript = CXYWebScript(webView: webView) 202 | webScript.useUIDelegate() 203 | return webScript 204 | }() 205 | 206 | 207 | webScript.addJsFunc("onSayHello") { args in 208 | print(args) 209 | return "只支持返回字符串或nil,如何需要返回其他类型,可先将其转为JSON字符串再返回" 210 | } 211 | 212 | webScript.addJsFunc("onSayHello") { args, returnBlock in 213 | print(args) 214 | DispatchQueue.main.asyncAfter(deadline: .now()+2) { 215 | // 只支持返回字符串或nil,如何需要返回其他类型,可先将其转为JSON字符串再返回 216 | returnBlock("我是异步返回值,2秒后才返回"); // returnBlock 必须要执行 217 | } 218 | } 219 | 220 | webScript.addTarget(self, jsFunc: "onPreviewImages", ocSel: #selector(onPreviewImages(_:_:))) 221 | 222 | webScript.addTarget(self, jsFunc: "onShareObj", ocSel: #selector(onShareObject(_:))) 223 | 224 | webScript.addJsFunc("onJumpToPage") { [weak self] args in 225 | self?.navigationController?.popViewController(animated: true) 226 | return "" 227 | } 228 | 229 | 230 | @objc func onPreviewImages(_ imgs: [String], _ currentIndex: NSNumber) { 231 | print(imgs) 232 | print(currentIndex) 233 | } 234 | 235 | @objc func onShareObject(_ obj: AnyObject) { 236 | print(obj) 237 | } 238 | ``` 239 | 240 | 241 | 242 | ### 原理解释: 243 | 244 | 1、让 WKWebView 注入下面的代码到 JS 环境中: 245 | 246 | ```objective-c 247 | NSString *js = 248 | @"window.App = new Proxy({},{ \ 249 | get: function (target, name) { \ 250 | return function (...args) { \ 251 | return window.prompt(name,JSON.stringify(args)); \ 252 | }; \ 253 | } \ 254 | });" 255 | 256 | WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime (WKUserScriptInjectionTimeAtDocumentStart) forMainFrameOnly:NO]; 257 | [self.webView.configuration.userContentController addUserScript:script]; 258 | ``` 259 | 260 | > 提示: 261 | > 262 | > 使用` window.webkit.messageHandlers[name].postMessage(args);` 亦可,只是后续实现略有不同。 263 | > 264 | > 问:为什么选用window.prompt方式呢? 265 | > 266 | > 答:因为它能直接同步返回值。 267 | 268 | 269 | 270 | **Proxy** 的作用是拦截对 `window.App` 对象属性的访问。重写` window.App` 的 `get` 方法,当访问它的属性(name)时,会返回一个**匿名函数** ; 271 | 272 | - 该函数的参数是可变的,类数组 273 | 274 | - 该函数返回了` window.prompt(window.App.属性名, args的json字符串数组);` 275 | 276 | 那么当 H5 端 js 调用: 277 | 278 | ```js 279 | window.App.onSayHello('Hello') 等价于 => window.prompt('onSayHello',\"['Hello']\"); 280 | ``` 281 | 282 | 283 | 284 | 2、而在 iOS H5 里调用 `window.prompt` 会执行 WKWebView.UIDelegate 的方法: 285 | 286 | ```objective-c 287 | - (void)webView:(WKWebView *)webView 288 | runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt 289 | defaultText:(nullable NSString *)defaultText 290 | initiatedByFrame:(WKFrameInfo *)frame 291 | completionHandler:(void (^)(NSString * _Nullable result))completionHandler; 292 | 293 | // 调用completionHandler后,能同步返回值给H5端。 294 | completionHandler('返回值') 295 | ``` 296 | 297 | 其中的两个参数分别对应**方法名**和**参数列表JSON数组**: 298 | 299 | ``` 300 | (NSString *)prompt => 'onSayHello' 301 | (nullable NSString *)defaultText => '[\'Hello\']' 302 | ``` 303 | 304 | 305 | 306 | 3、对于 **target-action** 方式,根据方法名得到对应的 `SEL`,使用`NSInvocation`类,可以构造一个表示方法调用的对象,包括方法选择器、目标对象、参数和返回值。可以处理具有多个参数的方法调用。 307 | 308 | ```objective-c 309 | self.scriptMap = {@"onSayHello": NSStringFromSelector(onSayHello:)} 310 | NSString *selString = self.scriptMap[prompt]; 311 | SEL sel = NSSelectorFromString(selString); 312 | NSArray *args = [self arrayWithJSON:defaultText]; 313 | 314 | NSMethodSignature *signature = [self methodSignatureForSelector:aSelector]; 315 | NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 316 | [invocation setTarget:self]; 317 | [invocation setSelector:aSelector]; 318 | ... 详细见源码 319 | ``` 320 | 321 | 322 | 323 | 4、对于 **block**、**async-block** 方式,根据方法名找到对应的 block,直接执行 block 就行: 324 | 325 | ```objective-c 326 | // self.blockMap = {@"onSayHello": CXYBlock} 327 | CXYBlock block = self.blockMap[prompt]; 328 | NSArray *args = [self arrayWithJSON:defaultText]; 329 | block(args); 330 | ``` 331 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iHongRen/CXYWebScript/20f07c354ac8991b989c6c2b2a13d065fe3d207c/screenshot.png --------------------------------------------------------------------------------