{"id":13393813,"url":"https://github.com/marcuswestin/WebViewJavascriptBridge","last_synced_at":"2025-03-13T19:31:47.494Z","repository":{"id":40643741,"uuid":"2281775","full_name":"marcuswestin/WebViewJavascriptBridge","owner":"marcuswestin","description":"An iOS/OSX bridge for sending messages between Obj-C and JavaScript in UIWebViews/WebViews","archived":false,"fork":false,"pushed_at":"2024-08-01T09:17:17.000Z","size":522,"stargazers_count":14289,"open_issues_count":103,"forks_count":2982,"subscribers_count":510,"default_branch":"master","last_synced_at":"2024-10-29T10:57:28.009Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://marcuswest.in","language":"Objective-C","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/marcuswestin.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":"Roadmap.md","authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-08-28T02:25:27.000Z","updated_at":"2024-10-29T09:47:59.000Z","dependencies_parsed_at":"2022-08-02T20:30:53.782Z","dependency_job_id":"dd80f96b-9c4c-4417-a01d-ab36139a456d","html_url":"https://github.com/marcuswestin/WebViewJavascriptBridge","commit_stats":{"total_commits":392,"total_committers":62,"mean_commits":6.32258064516129,"dds":"0.35969387755102045","last_synced_commit":"9a1ae72d99241065cdad6e56f9474c107820e61a"},"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcuswestin%2FWebViewJavascriptBridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcuswestin%2FWebViewJavascriptBridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcuswestin%2FWebViewJavascriptBridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcuswestin%2FWebViewJavascriptBridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcuswestin","download_url":"https://codeload.github.com/marcuswestin/WebViewJavascriptBridge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243469213,"owners_count":20295710,"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","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":[],"created_at":"2024-07-30T17:01:00.670Z","updated_at":"2025-03-13T19:31:47.470Z","avatar_url":"https://github.com/marcuswestin.png","language":"Objective-C","readme":"WebViewJavascriptBridge\n=======================\n\n[![Circle CI](https://img.shields.io/circleci/project/github/marcuswestin/WebViewJavascriptBridge.svg)](https://circleci.com/gh/marcuswestin/WebViewJavascriptBridge)\n\nAn iOS/OSX bridge for sending messages between Obj-C and JavaScript in WKWebViews, UIWebViews \u0026 WebViews.\n\nMigration Guide\n---------------\n\nWhen upgrading from v5.0.x to 6.0.x you will have to update the `setupWebViewJavascriptBridge` javascript snippet. See https://github.com/marcuswestin/WebViewJavascriptBridge#usage part 4).\n\nWho uses WebViewJavascriptBridge?\n---------------------------------\nWebViewJavascriptBridge is used by a range of companies and projects. This is a small and incomplete sample list:\n\n- [Facebook Messenger](https://www.facebook.com/mobile/messenger)\n- [Facebook Paper](https://facebook.com/paper)\n- [Yardsale](http://www.getyardsale.com/)\n- [EverTrue](http://www.evertrue.com/)\n- [Game Insight](http://www.game-insight.com/)\n- [Sush.io](http://www.sush.io)\n- [Imbed](http://imbed.github.io/)\n- [CareZone](https://carezone.com)\n- [Hemlig](http://www.hemlig.co)\n- [Altralogica](http://www.altralogica.it)\n- [鼎盛中华](https://itunes.apple.com/us/app/ding-sheng-zhong-hua/id537273940?mt=8)\n- [FRIL](https://fril.jp)\n- [留白·WHITE](http://liubaiapp.com)\n- [BrowZine](http://thirdiron.com/browzine/)\n- ... \u0026 many more!\n\nInstallation (iOS \u0026 OSX)\n------------------------\n\n### Installation with CocoaPods\nAdd this to your [podfile](https://guides.cocoapods.org/using/getting-started.html) and run `pod install` to install:\n\n```ruby\npod 'WebViewJavascriptBridge', '~\u003e 6.0'\n```\n\n### Manual installation\n\nDrag the `WebViewJavascriptBridge` folder into your project.\n\nIn the dialog that appears, uncheck \"Copy items into destination group's folder\" and select \"Create groups for any folders\".\n\nExamples\n--------\n\nSee the `Example Apps/` folder. Open either the iOS or OSX project and hit run to see it in action.\n\nTo use a WebViewJavascriptBridge in your own project:\n\nUsage\n-----\n\n1) Import the header file and declare an ivar property:\n\n```objc\n#import \"WebViewJavascriptBridge.h\"\n```\n\n...\n\n```objc\n@property WebViewJavascriptBridge* bridge;\n```\n\n2) Instantiate WebViewJavascriptBridge with a WKWebView, UIWebView (iOS) or WebView (OSX):\n\n```objc\nself.bridge = [WebViewJavascriptBridge bridgeForWebView:webView];\n```\n\n3) Register a handler in ObjC, and call a JS handler:\n\n```objc\n[self.bridge registerHandler:@\"ObjC Echo\" handler:^(id data, WVJBResponseCallback responseCallback) {\n\tNSLog(@\"ObjC Echo called with: %@\", data);\n\tresponseCallback(data);\n}];\n[self.bridge callHandler:@\"JS Echo\" data:nil responseCallback:^(id responseData) {\n\tNSLog(@\"ObjC received response: %@\", responseData);\n}];\n```\n\n4) Copy and paste `setupWebViewJavascriptBridge` into your JS:\n\t\n```javascript\nfunction setupWebViewJavascriptBridge(callback) {\n\tif (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }\n\tif (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }\n\twindow.WVJBCallbacks = [callback];\n\tvar WVJBIframe = document.createElement('iframe');\n\tWVJBIframe.style.display = 'none';\n\tWVJBIframe.src = 'https://__bridge_loaded__';\n\tdocument.documentElement.appendChild(WVJBIframe);\n\tsetTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)\n}\n```\n\n5) Finally, call `setupWebViewJavascriptBridge` and then use the bridge to register handlers and call ObjC handlers:\n\n```javascript\nsetupWebViewJavascriptBridge(function(bridge) {\n\t\n\t/* Initialize your app here */\n\n\tbridge.registerHandler('JS Echo', function(data, responseCallback) {\n\t\tconsole.log(\"JS Echo called with:\", data)\n\t\tresponseCallback(data)\n\t})\n\tbridge.callHandler('ObjC Echo', {'key':'value'}, function responseCallback(responseData) {\n\t\tconsole.log(\"JS received response:\", responseData)\n\t})\n})\n```\n\nAutomatic reference counting (ARC)\n----------------------------------\nThis library relies on ARC, so if you use ARC in you project, all works fine.\nBut if your project have no ARC support, be sure to do next steps:\n\n1) In your Xcode project open project settings -\u003e 'Build Phases'\n\n2) Expand 'Compile Sources' header and find all *.m files which are belongs to this library. Make attention on the 'Compiler Flags' in front of each source file in this list\n\n3) For each file add '-fobjc-arc' flag\n\nNow all WVJB files will be compiled with ARC support.\n\nContributors \u0026 Forks\n--------------------\nContributors: https://github.com/marcuswestin/WebViewJavascriptBridge/graphs/contributors\n\nForks: https://github.com/marcuswestin/WebViewJavascriptBridge/network/members\n\nAPI Reference\n-------------\n\n### ObjC API\n\n##### `[WebViewJavascriptBridge bridgeForWebView:(WKWebVIew/UIWebView/WebView*)webview`\n\nCreate a javascript bridge for the given web view.\n\nExample:\n\n```objc\t\n[WebViewJavascriptBridge bridgeForWebView:webView];\n```\n\n##### `[bridge registerHandler:(NSString*)handlerName handler:(WVJBHandler)handler]`\n\nRegister a handler called `handlerName`. The javascript can then call this handler with `WebViewJavascriptBridge.callHandler(\"handlerName\")`.\n\nExample:\n\n```objc\n[self.bridge registerHandler:@\"getScreenHeight\" handler:^(id data, WVJBResponseCallback responseCallback) {\n\tresponseCallback([NSNumber numberWithInt:[UIScreen mainScreen].bounds.size.height]);\n}];\n[self.bridge registerHandler:@\"log\" handler:^(id data, WVJBResponseCallback responseCallback) {\n\tNSLog(@\"Log: %@\", data);\n}];\n\n```\n\n##### `[bridge callHandler:(NSString*)handlerName data:(id)data]`\n##### `[bridge callHandler:(NSString*)handlerName data:(id)data responseCallback:(WVJBResponseCallback)callback]`\n\nCall the javascript handler called `handlerName`. If a `responseCallback` block is given the javascript handler can respond.\n\nExample:\n\n```objc\n[self.bridge callHandler:@\"showAlert\" data:@\"Hi from ObjC to JS!\"];\n[self.bridge callHandler:@\"getCurrentPageUrl\" data:nil responseCallback:^(id responseData) {\n\tNSLog(@\"Current UIWebView page URL is: %@\", responseData);\n}];\n```\n\n#### `[bridge setWebViewDelegate:(id)webViewDelegate]`\n\nOptionally, set a `WKNavigationDelegate/UIWebViewDelegate` if you need to respond to the [web view's lifecycle events](https://developer.apple.com/reference/uikit/uiwebviewdelegate).\n\n##### `[bridge disableJavscriptAlertBoxSafetyTimeout]`\n\nUNSAFE. Speed up bridge message passing by disabling the setTimeout safety check. It is only safe to disable this safety check if you do not call any of the javascript popup box functions (alert, confirm, and prompt). If you call any of these functions from the bridged javascript code, the app will hang.\n\nExample:\n\n\t[self.bridge disableJavscriptAlertBoxSafetyTimeout];\n\n\n\n### Javascript API\n\n##### `bridge.registerHandler(\"handlerName\", function(responseData) { ... })`\n\nRegister a handler called `handlerName`. The ObjC can then call this handler with `[bridge callHandler:\"handlerName\" data:@\"Foo\"]` and `[bridge callHandler:\"handlerName\" data:@\"Foo\" responseCallback:^(id responseData) { ... }]`\n\nExample:\n\n```javascript\nbridge.registerHandler(\"showAlert\", function(data) { alert(data) })\nbridge.registerHandler(\"getCurrentPageUrl\", function(data, responseCallback) {\n\tresponseCallback(document.location.toString())\n})\n```\n\n\n##### `bridge.callHandler(\"handlerName\", data)`\n##### `bridge.callHandler(\"handlerName\", data, function responseCallback(responseData) { ... })`\n\nCall an ObjC handler called `handlerName`. If a `responseCallback` function is given the ObjC handler can respond.\n\nExample:\n\n```javascript\nbridge.callHandler(\"Log\", \"Foo\")\nbridge.callHandler(\"getScreenHeight\", null, function(response) {\n\talert('Screen height:' + response)\n})\n```\n\n\n##### `bridge.disableJavscriptAlertBoxSafetyTimeout()`\n\nCalling `bridge.disableJavscriptAlertBoxSafetyTimeout()` has the same effect as calling `[bridge disableJavscriptAlertBoxSafetyTimeout];` in ObjC.\n\nExample:\n\n```javascript\nbridge.disableJavscriptAlertBoxSafetyTimeout()\n```\n","funding_links":[],"categories":["Object-C 库","框架和库","Bridging","Objective-C","Foundation","[Objective-C](#objective-c)","Objective-C  Stars 1000以内排名整理","etc","OOM-Leaks-Crash","9. 其他与展望"],"sub_categories":["Other free courses","Getting Started","WebView-ProgressBar","5. 参考材料"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcuswestin%2FWebViewJavascriptBridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcuswestin%2FWebViewJavascriptBridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcuswestin%2FWebViewJavascriptBridge/lists"}