Skip to content

Instantly share code, notes, and snippets.

@yonekawa
Created November 28, 2012 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yonekawa/4159669 to your computer and use it in GitHub Desktop.
Save yonekawa/4159669 to your computer and use it in GitHub Desktop.
Fix PhoneGap2.2-iOS does not work with RequireJS
diff --git a/cordova/ios/CordovaLib/Classes/CDVCommandDelegate.h b/cordova/ios/CordovaLib/Classes/CDVCommandDelegate.h
--- a/cordova/ios/CordovaLib/Classes/CDVCommandDelegate.h
+++ b/cordova/ios/CordovaLib/Classes/CDVCommandDelegate.h
@@ -42,6 +42,8 @@
// without reason. Without the run-loop delay, alerts used in JS callbacks may result
// in dead-lock. This method must be called from the UI thread.
- (void)evalJs:(NSString*)js scheduledOnRunLoop:(BOOL)scheduledOnRunLoop;
+// Patch for using RequireJS.
+- (void)evalJs:(NSString*)js scheduledOnRunLoop:(BOOL)scheduledOnRunLoop useNativeEvalAndFetch:(BOOL)useNativeEvalAndFetch;
// Runs the given block on a background thread using a shared thread-pool.
- (void)runInBackground:(void (^)())block;
diff --git a/cordova/ios/CordovaLib/Classes/CDVCommandDelegateImpl.m b/cordova/ios/CordovaLib/Classes/CDVCommandDelegateImpl.m
--- a/cordova/ios/CordovaLib/Classes/CDVCommandDelegateImpl.m
+++ b/cordova/ios/CordovaLib/Classes/CDVCommandDelegateImpl.m
@@ -100,7 +100,16 @@
- (void)evalJs:(NSString*)js scheduledOnRunLoop:(BOOL)scheduledOnRunLoop
{
- js = [NSString stringWithFormat:@"cordova.require('cordova/exec').nativeEvalAndFetch(function(){%@})", js];
+ [self evalJs:js scheduledOnRunLoop:scheduledOnRunLoop useNativeEvalAndFetch:YES];
+}
+
+// Patch for using RequireJS.
+// cordova is undefined here when used RequireJS to loading Cordova,
+- (void)evalJs:(NSString*)js scheduledOnRunLoop:(BOOL)scheduledOnRunLoop useNativeEvalAndFetch:(BOOL)useNativeEvalAndFetch
+{
+ if (useNativeEvalAndFetch) {
+ js = [NSString stringWithFormat:@"cordova.require('cordova/exec').nativeEvalAndFetch(function(){%@})", js];
+ }
if (scheduledOnRunLoop) {
[self evalJsHelper:js];
} else {
diff --git a/cordova/ios/CordovaLib/Classes/CDVViewController.m b/cordova/ios/CordovaLib/Classes/CDVViewController.m
--- a/cordova/ios/CordovaLib/Classes/CDVViewController.m
+++ b/cordova/ios/CordovaLib/Classes/CDVViewController.m
@@ -490,8 +490,13 @@
// The iOSVCAddr is used to identify the WebView instance when using one of the XHR js->native bridge modes.
// The .onNativeReady().fire() will work when cordova.js is already loaded.
// The _nativeReady = true; is used when this is run before cordova.js is loaded.
- NSString* nativeReady = [NSString stringWithFormat:@"cordova.iOSVCAddr='%lld';try{cordova.require('cordova/channel').onNativeReady.fire();}catch(e){window._nativeReady = true;}", (long long)self];
- [self.commandDelegate evalJs:nativeReady];
+ // Patch for using RequireJS: cordova.iOSVCAddr -> window.iOSVCAddr
+ NSString* nativeReady = [NSString stringWithFormat:@"window.iOSVCAddr='%lld';try{cordova.require('cordova/channel').onNativeReady.fire();}catch(e){window._nativeReady = true;}", (long long)self];
+ if ([[self.webView stringByEvaluatingJavaScriptFromString:@"typeof(cordova);"] isEqualToString:@"undefined"]) {
+ [self.commandDelegate evalJs:nativeReady scheduledOnRunLoop:YES useNativeEvalAndFetch:NO];
+ } else {
+ [self.commandDelegate evalJs:nativeReady];
+ }
}
diff --git a/cordova/www/js/lib/cordova/cordova-2.2.0-ios.js b/cordova/www/js/lib/cordova/cordova-2.2.0-ios.js
--- a/cordova/www/js/lib/cordova/cordova-2.2.0-ios.js
+++ b/cordova/www/js/lib/cordova/cordova-2.2.0-ios.js
@@ -1021,7 +1021,8 @@ function iOSExec() {
// Changing this to a GET will make the XHR reach the URIProtocol on 4.2.
// For some reason it still doesn't work though...
execXhr.open('HEAD', "/!gap_exec", true);
- execXhr.setRequestHeader('vc', cordova.iOSVCAddr);
+ // Patch for using RequireJS: cordova.iOSVCAddr -> window.iOSVCAddr
+ execXhr.setRequestHeader('vc', window.iOSVCAddr);
execXhr.setRequestHeader('rc', ++requestCount);
if (shouldBundleCommandJson()) {
execXhr.setRequestHeader('cmds', iOSExec.nativeFetchMessages());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment