Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: objective-c
before_script:
- brew update
- brew upgrade xctool || true
script:
- xctool -project WebViewJavascriptBridge.xcodeproj -scheme WebViewJavascriptBridge -configuration Release -sdk iphonesimulator test
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
WebViewJavascriptBridge
=======================

[![Build Status](https://travis-ci.org/marcuswestin/WebViewJavascriptBridge.svg)](https://travis-ci.org/marcuswestin/WebViewJavascriptBridge)

An iOS/OSX bridge for sending messages between Obj-C and JavaScript in UIWebViews/WebViews.

If you like WebViewJavascriptBridge you may also want to check out [WebViewProxy](https://github.com/marcuswestin/WebViewProxy).
Expand Down
523 changes: 523 additions & 0 deletions WebViewJavascriptBridge.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3D0FE4691AE2886400BB4104"
BuildableName = "libWebViewJavascriptBridge.a"
BlueprintName = "WebViewJavascriptBridge"
ReferencedContainer = "container:WebViewJavascriptBridge.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3D0FE4741AE2886500BB4104"
BuildableName = "WebViewJavascriptBridgeTests.xctest"
BlueprintName = "WebViewJavascriptBridgeTests"
ReferencedContainer = "container:WebViewJavascriptBridge.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3D0FE4741AE2886500BB4104"
BuildableName = "WebViewJavascriptBridgeTests.xctest"
BlueprintName = "WebViewJavascriptBridgeTests"
ReferencedContainer = "container:WebViewJavascriptBridge.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3D0FE4691AE2886400BB4104"
BuildableName = "libWebViewJavascriptBridge.a"
BlueprintName = "WebViewJavascriptBridge"
ReferencedContainer = "container:WebViewJavascriptBridge.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3D0FE4691AE2886400BB4104"
BuildableName = "libWebViewJavascriptBridge.a"
BlueprintName = "WebViewJavascriptBridge"
ReferencedContainer = "container:WebViewJavascriptBridge.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3D0FE4691AE2886400BB4104"
BuildableName = "libWebViewJavascriptBridge.a"
BlueprintName = "WebViewJavascriptBridge"
ReferencedContainer = "container:WebViewJavascriptBridge.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
1 change: 1 addition & 0 deletions WebViewJavascriptBridge/WebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
if (_numRequestsLoading == 0 && ![[webView stringByEvaluatingJavaScriptFromString:@"typeof WebViewJavascriptBridge == 'object'"] isEqualToString:@"true"]) {
NSBundle *bundle = _resourceBundle ? _resourceBundle : [NSBundle mainBundle];
NSString *filePath = [bundle pathForResource:@"WebViewJavascriptBridge.js" ofType:@"txt"];
NSAssert(filePath != nil, @"WebViewJavascriptBridge.js.txt is missing. Your bundle is probably not setup correctly (using bundle %@)", bundle);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this check. In an app that I'm writing, we've pulled the js file from our application bundle and are loading it on our server. This would result in runtime errors in my application while debugging/developing locally.

NSString *js = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[webView stringByEvaluatingJavaScriptFromString:js];
}
Expand Down
16 changes: 16 additions & 0 deletions WebViewJavascriptBridgeTestHost/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// AppDelegate.h
// WebViewJavascriptBridgeTestHost
//
// Created by Pieter De Baets on 18/04/2015.
// Copyright (c) 2015 marcuswestin. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

43 changes: 43 additions & 0 deletions WebViewJavascriptBridgeTestHost/AppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// AppDelegate.m
// WebViewJavascriptBridgeTestHost
//
// Created by Pieter De Baets on 18/04/2015.
// Copyright (c) 2015 marcuswestin. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

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

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
// 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.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
// 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.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
// 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.
}

- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
34 changes: 34 additions & 0 deletions WebViewJavascriptBridgeTestHost/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>in.marcuswestin.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
</dict>
</plist>
16 changes: 16 additions & 0 deletions WebViewJavascriptBridgeTestHost/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// main.m
// WebViewJavascriptBridgeTestHost
//
// Created by Pieter De Baets on 18/04/2015.
// Copyright (c) 2015 marcuswestin. All rights reserved.
//

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

int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
108 changes: 108 additions & 0 deletions WebViewJavascriptBridgeTests/BridgeTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// BridgeTests.m
// WebViewJavascriptBridge
//
// Created by Pieter De Baets on 18/04/2015.
// Copyright (c) 2015 marcuswestin. All rights reserved.
//

#import <XCTest/XCTest.h>

#import "WebViewJavascriptBridge.h"
#import "AppDelegate.h"

static NSString *const echoHandler = @"echoHandler";
static const WVJBHandler nullHandler = ^(id data, WVJBResponseCallback callback) {};

@interface BridgeTests : XCTestCase

@end

@implementation BridgeTests {
UIWebView *_webView;
}

- (void)setUp
{
[super setUp];

UIViewController *rootVC = [[(AppDelegate *)[[UIApplication sharedApplication] delegate] window] rootViewController];
_webView = [[UIWebView alloc] initWithFrame:rootVC.view.bounds];
[rootVC.view addSubview:_webView];
}

- (void)tearDown
{
[super tearDown];
[_webView removeFromSuperview];
}

static void loadEchoSample(UIWebView *webView)
{
NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"echo" withExtension:@"html"]];
[webView loadRequest:request];
}

- (void)testInitialization
{
XCTestExpectation *startup = [self expectationWithDescription:@"Startup completed"];
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:^(id data, WVJBResponseCallback responseCallback) {
XCTAssertEqualObjects(data, @"Hello world");
[startup fulfill];
}];
XCTAssertNotNil(bridge);

loadEchoSample(_webView);
[self waitForExpectationsWithTimeout:10 handler:NULL];
}

- (void)testResponseHandler
{
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:nullHandler];

XCTestExpectation *callbackInvoked = [self expectationWithDescription:@"Callback invoked"];
[bridge send:@"testResponseHandler" responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, @"testResponseHandler");
[callbackInvoked fulfill];
}];

loadEchoSample(_webView);
[self waitForExpectationsWithTimeout:10 handler:NULL];
}

- (void)testEchoHandler
{
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:nullHandler];

XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
[bridge callHandler:echoHandler data:@"testEchoHandler" responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, @"testEchoHandler");
[callbackInvocked fulfill];
}];

loadEchoSample(_webView);
[self waitForExpectationsWithTimeout:10 handler:NULL];
}

- (void)testObjectEncoding
{
WebViewJavascriptBridge *bridge = [WebViewJavascriptBridge bridgeForWebView:_webView handler:nullHandler];

void (^echoObject)(id) = ^void(id object) {
XCTestExpectation *callbackInvocked = [self expectationWithDescription:@"Callback invoked"];
[bridge callHandler:echoHandler data:object responseCallback:^(id responseData) {
XCTAssertEqualObjects(responseData, object);
[callbackInvocked fulfill];
}];
};

echoObject(@"A string sent over the wire");
echoObject(@"A string with '\"'/\\");
echoObject(@[ @1, @2, @3 ]);
echoObject(@{ @"a" : @1, @"b" : @2 });

loadEchoSample(_webView);
[self waitForExpectationsWithTimeout:10 handler:NULL];
}

@end
24 changes: 24 additions & 0 deletions WebViewJavascriptBridgeTests/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>in.marcuswestin.WebViewJavascriptBridge.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
Loading