{"id":32317966,"url":"https://github.com/eroscai/szwebviewbridge","last_synced_at":"2025-10-23T11:22:46.902Z","repository":{"id":62454849,"uuid":"214848132","full_name":"eroscai/SZWebViewBridge","owner":"eroscai","description":"Swift WebView bridge. Based on message handler.","archived":false,"fork":false,"pushed_at":"2019-11-25T01:31:55.000Z","size":152,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-01T04:48:56.015Z","etag":null,"topics":["bridge","callback","handler","message","message-handler","swift","webkit","webview","wkwebview"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eroscai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-13T15:50:56.000Z","updated_at":"2024-04-20T17:14:19.000Z","dependencies_parsed_at":"2022-11-02T00:16:35.860Z","dependency_job_id":null,"html_url":"https://github.com/eroscai/SZWebViewBridge","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/eroscai/SZWebViewBridge","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eroscai%2FSZWebViewBridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eroscai%2FSZWebViewBridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eroscai%2FSZWebViewBridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eroscai%2FSZWebViewBridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eroscai","download_url":"https://codeload.github.com/eroscai/SZWebViewBridge/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eroscai%2FSZWebViewBridge/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280607878,"owners_count":26359686,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-23T02:00:06.710Z","response_time":142,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bridge","callback","handler","message","message-handler","swift","webkit","webview","wkwebview"],"created_at":"2025-10-23T11:22:45.076Z","updated_at":"2025-10-23T11:22:46.895Z","avatar_url":"https://github.com/eroscai.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SZWebViewBridge\n\n[![CI Status](https://img.shields.io/travis/eroscai/SZWebViewBridge.svg?style=flat)](https://travis-ci.org/eroscai/SZWebViewBridge)\n[![Version](https://img.shields.io/cocoapods/v/SZWebViewBridge.svg?style=flat)](https://cocoapods.org/pods/SZWebViewBridge)\n[![License](https://img.shields.io/cocoapods/l/SZWebViewBridge.svg?style=flat)](https://cocoapods.org/pods/SZWebViewBridge)\n[![Platform](https://img.shields.io/cocoapods/p/SZWebViewBridge.svg?style=flat)](https://cocoapods.org/pods/SZWebViewBridge)\n\nSZWebViewBridge is a lightweight, pure-Swift library for sending messages between WKWebView and Web client. Based on message handler.\n\n## Requirements\n\n- iOS 10.0+\n- Swift 5.0+\n\n## Installation\n\nSZWebViewBridge is available through [CocoaPods](https://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'SZWebViewBridge'\n```\n\n## Principle\n\n1. Encapsulates the JS code and Native code for comunication, and automatically inject JS code when initializing the `Bridge` on the Native side, without any additional configuration.\n\nBelow is the sample code for sending a message on the JS side：\n\n```js\n// post message with params\nfunction testAlert() {\n    window.szBridge.post('alert', {\n        'title': 'This is alert title',\n        'message': 'This is alert message'\n})\n}\n\n// post message with params and callback\nfunction testReceiveSuccess() {\n    window.szBridge.post('testSuccess', {\n        'key1': 'value1',\n        'key2': {\n            'subkey1': 'subvalue2'\n        }\n    }, function (result, error) {\n        alertUsingBridge(JSON.stringify(result))\n    })\n}\n```\nThe sample code for Native side：\n\n```swift\n// post message\nwebViewBridge.post(action: \"change\", params: nil)\n\n// callback\nlet response = [\n    \"param1\": \"value1\",\n    \"param2\": \"value2\",\n]\nlet success: SZWebViewBridgeResult = .success(response)\nwebViewBridge.callback(callbackId: callbackId, result: success)\n```\n\n2. Native side need `Handler` to receive message sent by JS side. The passed parameters will be automatically parsed into the `params` variable, and other useful parameters also defined in the `Handler`. For full list of avaiable parameters, refer to `SZWebViewBridgeBaseHandler`.\n\nBelow is the sample code for receive a message and sends a callback.\n\n```swift\nimport UIKit\nimport SZWebViewBridge\n\nclass TestReceiveSuccessHandler: SZWebViewBridgeBaseHandler {\n\n    override func invoke() throws {\n        guard let params = params else { return }\n\n        print(params)\n        if let webViewBridge = webViewBridge {\n            if isValidCallbackId {\n                let response = [\n                    \"param1\": \"value1\",\n                    \"param2\": \"value2\",\n                ]\n                let success: SZWebViewBridgeResult = .success(response)\n                webViewBridge.callback(callbackId: callbackId, result: success)\n            }\n        }\n\n    }\n    \n}\n```\n\n3. Because all handlers must adopt the same protocol, it can be easily get parameters and various variables needed.\n4. Each handler is a separate class/file that can be easily added and removed.\n\n## Usage\n\n1. Init `SZWebViewBridge` with a `WKWebView` and `UIViewController`. (The `UIViewController` will assign to handler and would be used in the future.(e.g., push or present to another `UIViewController`))\n\n```swift\nlet bridge = SZWebViewBridge(webView: webView, viewController: self)\nwebViewBridge = bridge  // make sure bridge will not be deinit.\n```\n2. Add some predefined handlers. For example define a `TitleHandler`\n\n```swift\nimport UIKit\nimport SZWebViewBridge\n\nclass TitleHandler: SZWebViewBridgeBaseHandler {\n\n    override func invoke() throws {\n        guard let params = params else { return }\n\n        if let title = params[\"title\"] as? String {\n            if let viewController = viewController {\n                viewController.title = title\n            }\n        }\n    }\n\n}\n```\n\nThen use function `addHandlers` or `removeHandlers` to add/remove handlers.\n\n```swift\nvar handlers: SZWebViewBridgeBaseHandlerObject {\n    return [\n        \"setTitle\": TitleHandler.self,\n        ...\n    ]\n}\nwebViewBridge.addHandlers(handlers: handlers)\n```\n\n3. All Done!  More detail usage please check example project.\n\n\n## Example\n\nTo run the example project, clone the repo, and run `pod install` from the Example directory first.\n\n## Author\n\neroscai, csz0102@gmail.com\n\n## License\n\nSZWebViewBridge is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feroscai%2Fszwebviewbridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feroscai%2Fszwebviewbridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feroscai%2Fszwebviewbridge/lists"}