★ wanayoo — archive 1999 https://github.com/marcuswestin/WebViewJavascriptBridgeNouvelle recherche | Portail wanayoo

A standalone iOS class for sending messages to and from javascript in a UIWebView

Read+Write access

Latest commit to the master branch

name age message
directory WebViewJavascriptBridge.xcodeproj/ Disable ARC for WebViewJavascriptBridge.m [psineur]
directory WebViewJavascriptBridge/ There is no need for -initWithDelegate, -init and +javascriptBridge. … [marcuswestin]
file .gitignore Seed project with an example app and the stub implementation of a Web… [marcuswestin]
file Changelog Add a Changelog [marcuswestin]
file LICENSE Add MIT license [marcuswestin]
file README.md Clean up README instrutions and code [marcuswestin]
README.md

WebViewJavascriptBridge

An iOS bridge for sending messages to and from javascript in a UIWebView

Getting started

Just open the Xcode project (requires Xcode > 4.2) and hit run to see the example application work.

Usage

See ExampleAppDelegate.* for example code. To use it in your own project:

1) Copy Classes/WebViewJavascriptBridge.h and Classes/WebViewJavascriptBridge.m into your Xcode project

2) Instantiate a UIWebView, a WebViewJavascriptBridge, and set yourself as the bridge's delegate

#import <UIKit/UIKit.h>
#import "WebViewJavascriptBridge.h"

@interface ExampleAppDelegate : UIResponder <UIApplicationDelegate, WebViewJavascriptBridgeDelegate>

@end

@implementation ExampleAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.webView = [[UIWebView alloc] initWithFrame:self.window.bounds];
    [self.window addSubview:webView];
    self.javascriptBridge = [WebViewJavascriptBridge javascriptBridgeWithDelegate:self];
    self.webView.delegate = javascriptBridge;

    [self.window makeKeyAndVisible];
    return YES;
}

- (void)javascriptBridge:(WebViewJavascriptBridge *)bridge receivedMessage:(NSString *)message fromWebView:(UIWebView *)webView 
{
    NSLog(@"MyJavascriptBridgeDelegate received message: %@", message);
}

@end

3) Go ahead and send some messages from Objc to javascript

[self.javascriptBridge sendMessage:@"Well hello there" toWebView:self.webView];

4) Finally, set up the javascript side of things

document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady() {
    WebViewJavascriptBridge.setMessageHandler(function(message) {
        alert('Received message: ' + message)
    })
    WebViewJavascriptBridge.sendMessage('Hello from the javascript')
}, false)

ARC

If you're using ARC in your project, add -fno-objc-arc as a compiler flag to the WebViewJavascriptBridge.m file.

Contributors



Markdown Cheat Sheet

Format Text

Headers

# This is an <h1> tag
## This is an <h2> tag
###### This is an <h6> tag

Text styles

*This text will be italic*
_This will also be italic_
**This text will be bold**
__This will also be bold__

*You **can** combine them*

Lists

Unordered

* Item 1
* Item 2
  * Item 2a
  * Item 2b

Ordered

1. Item 1
2. Item 2
3. Item 3
   * Item 3a
   * Item 3b

Miscellaneous

Images

![GitHub Logo](/images/logo.png)
Format: ![Alt Text](url)

Links

http://github.com - automatic!
[GitHub](http://github.com)

Blockquotes

As Kanye West said:

> We're living the future so
> the present is our past.

Code Examples in Markdown

Syntax highlighting with GFM

```javascript
function fancyAlert(arg) {
  if(arg) {
    $.facebox({div:'#foo'})
  }
}
```

Or, indent your code 4 spaces

Here is a Python code example
without syntax highlighting:

    def foo:
      if not bar:
        return true

Inline code for comments

I think you should use an
`<addr>` element here instead.

Something went wrong with that request. Please try again. Dismiss