You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sh...@apache.org on 2012/11/21 09:07:46 UTC

ios commit: Update fix to CB-1695 - the main Cordova UIWebView has a unique User-Agent now.

Updated Branches:
  refs/heads/master 181dd19e0 -> b51fdb3e2


Update fix to CB-1695 - the main Cordova UIWebView has a unique User-Agent now.

Previously it used the default, which would have meant other UIWebViews in an app would have been under the white-list as well (since they would have the same User-Agent)


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/b51fdb3e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/b51fdb3e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/b51fdb3e

Branch: refs/heads/master
Commit: b51fdb3e2feaf51305ee712c05b7059273d3efe9
Parents: 181dd19
Author: Shazron Abdullah <sh...@apache.org>
Authored: Wed Nov 21 00:07:37 2012 -0800
Committer: Shazron Abdullah <sh...@apache.org>
Committed: Wed Nov 21 00:07:37 2012 -0800

----------------------------------------------------------------------
 CordovaLib/Classes/CDVViewController.m |   21 ++++++++++++++++++++-
 1 files changed, 20 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/b51fdb3e/CordovaLib/Classes/CDVViewController.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVViewController.m b/CordovaLib/Classes/CDVViewController.m
index 8637bb4..2ea2cca 100644
--- a/CordovaLib/Classes/CDVViewController.m
+++ b/CordovaLib/Classes/CDVViewController.m
@@ -403,6 +403,13 @@
     return [[CDVCordovaView alloc] initWithFrame:bounds];
 }
 
+- (NSString*)originalUserAgent
+{
+    UIWebView* testWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
+
+    return [testWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
+}
+
 - (void)createGapView
 {
     CGRect webViewBounds = self.view.bounds;
@@ -410,6 +417,18 @@
     webViewBounds.origin = self.view.bounds.origin;
 
     if (!self.webView) {
+        // generate a GUID to append to the User-Agent
+        CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
+        CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
+        NSString* modifiedUA = [NSString stringWithFormat:@"%@ (%@)", [self originalUserAgent], uuidString];
+        CFRelease(uuidString);
+        CFRelease(uuidRef);
+
+        // setting the UserAgent must occur before the UIWebView is instantiated.
+        // This is read per instantiation, so it does not affect the main Cordova UIWebView
+        NSDictionary* dict = [[NSDictionary alloc] initWithObjectsAndKeys:modifiedUA, @"UserAgent", nil];
+        [[NSUserDefaults standardUserDefaults] registerDefaults:dict];
+
         self.webView = [self newCordovaViewWithFrame:webViewBounds];
         self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
 
@@ -418,7 +437,7 @@
 
         self.webView.delegate = self;
 
-        // register this viewcontroller with the NSURLProtocol
+        // register this viewcontroller with the NSURLProtocol, only after the User-Agent is set
         [CDVURLProtocol registerViewController:self];
     }
 }