You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2013/01/18 20:48:36 UTC
[2/3] ios commit: Refactor User-Agent logic into a helper class.
Refactor User-Agent logic into a helper class.
Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/92fa3e45
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/92fa3e45
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/92fa3e45
Branch: refs/heads/master
Commit: 92fa3e4538f7d96117c507dfb89c4e5d388ec6dd
Parents: f392937
Author: Andrew Grieve <ag...@chromium.org>
Authored: Fri Jan 18 10:36:15 2013 -0500
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Fri Jan 18 14:48:07 2013 -0500
----------------------------------------------------------------------
CordovaLib/Classes/CDVInAppBrowser.m | 8 +-
CordovaLib/Classes/CDVUserAgentUtil.h | 25 ++++++
CordovaLib/Classes/CDVUserAgentUtil.m | 76 ++++++++++++++++++
CordovaLib/Classes/CDVViewController.h | 1 -
CordovaLib/Classes/CDVViewController.m | 44 +----------
CordovaLib/CordovaLib.xcodeproj/project.pbxproj | 8 ++
6 files changed, 115 insertions(+), 47 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/92fa3e45/CordovaLib/Classes/CDVInAppBrowser.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVInAppBrowser.m b/CordovaLib/Classes/CDVInAppBrowser.m
index 1411d2c..6c87d45 100644
--- a/CordovaLib/Classes/CDVInAppBrowser.m
+++ b/CordovaLib/Classes/CDVInAppBrowser.m
@@ -20,6 +20,7 @@
#import "CDVInAppBrowser.h"
#import "CDVPluginResult.h"
#import "CDVViewController.h"
+#import "CDVUserAgentUtil.h"
#define kInAppBrowserTargetSelf @"_self"
#define kInAppBrowserTargetSystem @"_system"
@@ -91,7 +92,7 @@
- (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
{
if (self.inAppBrowserViewController == nil) {
- NSString* originalUA = [CDVViewController originalUserAgent];
+ NSString* originalUA = [CDVUserAgentUtil originalUserAgent];
self.inAppBrowserViewController = [[CDVInAppBrowserViewController alloc] initWithUserAgent:originalUA];
self.inAppBrowserViewController.navigationDelegate = self;
@@ -229,10 +230,7 @@
webViewBounds.size.height -= FOOTER_HEIGHT;
if (!self.webView) {
- // 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:self.userAgent, @"UserAgent", nil];
- [[NSUserDefaults standardUserDefaults] registerDefaults:dict];
+ [CDVUserAgentUtil setUserAgent:self.userAgent];
self.webView = [[UIWebView alloc] initWithFrame:webViewBounds];
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/92fa3e45/CordovaLib/Classes/CDVUserAgentUtil.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVUserAgentUtil.h b/CordovaLib/Classes/CDVUserAgentUtil.h
new file mode 100644
index 0000000..fdea596
--- /dev/null
+++ b/CordovaLib/Classes/CDVUserAgentUtil.h
@@ -0,0 +1,25 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+@interface CDVUserAgentUtil : NSObject
++ (NSString*)originalUserAgent;
++ (void)setUserAgent:(NSString*)newValue;
+@end
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/92fa3e45/CordovaLib/Classes/CDVUserAgentUtil.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVUserAgentUtil.m b/CordovaLib/Classes/CDVUserAgentUtil.m
new file mode 100644
index 0000000..dd24fdc
--- /dev/null
+++ b/CordovaLib/Classes/CDVUserAgentUtil.m
@@ -0,0 +1,76 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+#import "CDVUserAgentUtil.h"
+
+#import <UIKit/UIKit.h>
+
+static NSString* gOriginalUserAgent = nil;
+static NSString* kCdvUserAgentKey = @"Cordova-User-Agent";
+static NSString* kCdvUserAgentVersionKey = @"Cordova-User-Agent-Version";
+
+@implementation CDVUserAgentUtil
+
++ (NSString*)originalUserAgent
+{
+ if (gOriginalUserAgent == nil) {
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppLocaleDidChange:)
+ name:NSCurrentLocaleDidChangeNotification object:nil];
+
+ NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
+ NSString* systemVersion = [[UIDevice currentDevice] systemVersion];
+ NSString* localeStr = [[NSLocale currentLocale] localeIdentifier];
+ NSString* systemAndLocale = [NSString stringWithFormat:@"%@ %@", systemVersion, localeStr];
+
+ NSString* cordovaUserAgentVersion = [userDefaults stringForKey:kCdvUserAgentVersionKey];
+ gOriginalUserAgent = [userDefaults stringForKey:kCdvUserAgentKey];
+ BOOL cachedValueIsOld = ![systemAndLocale isEqualToString:cordovaUserAgentVersion];
+
+ if ((gOriginalUserAgent == nil) || cachedValueIsOld) {
+ UIWebView* sampleWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
+ gOriginalUserAgent = [sampleWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
+
+ [userDefaults setObject:gOriginalUserAgent forKey:kCdvUserAgentKey];
+ [userDefaults setObject:systemAndLocale forKey:kCdvUserAgentVersionKey];
+
+ [userDefaults synchronize];
+ }
+ }
+ return gOriginalUserAgent;
+}
+
++ (void)setUserAgent:(NSString*)newValue
+{
+ NSLog(@"Set UA to %@", newValue);
+ // Setting the UserAgent must occur before a UIWebView is instantiated.
+ // It is read per instantiation, so it does not affect previously created views.
+ // Except! When a PDF is loaded, all currently active UIWebViews reload their
+ // User-Agent from the NSUserDefaults some time after the DidFinishLoad of the PDF bah!
+ NSDictionary* dict = [[NSDictionary alloc] initWithObjectsAndKeys:newValue, @"UserAgent", nil];
+ [[NSUserDefaults standardUserDefaults] registerDefaults:dict];
+}
+
++ (void)onAppLocaleDidChange:(NSNotification*)notification
+{
+ // TODO: We should figure out how to update the user-agent of existing UIWebViews when this happens.
+ // Maybe use the PDF bug (noted in setUserAgent:).
+ gOriginalUserAgent = nil;
+}
+
+@end
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/92fa3e45/CordovaLib/Classes/CDVViewController.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVViewController.h b/CordovaLib/Classes/CDVViewController.h
index 9ec9a5c..ed6f7fc 100644
--- a/CordovaLib/Classes/CDVViewController.h
+++ b/CordovaLib/Classes/CDVViewController.h
@@ -57,7 +57,6 @@
+ (NSDictionary*)getBundlePlist:(NSString*)plistName;
+ (NSString*)applicationDocumentsDirectory;
-+ (NSString*)originalUserAgent;
- (void)printMultitaskingInfo;
- (void)createGapView;
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/92fa3e45/CordovaLib/Classes/CDVViewController.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVViewController.m b/CordovaLib/Classes/CDVViewController.m
index 802f2dd..ef91f5f 100644
--- a/CordovaLib/Classes/CDVViewController.m
+++ b/CordovaLib/Classes/CDVViewController.m
@@ -22,12 +22,9 @@
#import "CDVCommandQueue.h"
#import "CDVCommandDelegateImpl.h"
#import "CDVConfigParser.h"
+#import "CDVUserAgentUtil.h"
#define degreesToRadian(x) (M_PI * (x) / 180.0)
-#define CDV_USER_AGENT_KEY @"Cordova-User-Agent"
-#define CDV_USER_AGENT_VERSION_KEY @"Cordova-User-Agent-Version"
-
-static NSString* gOriginalUserAgent = nil;
@interface CDVViewController ()
@@ -71,8 +68,6 @@ static NSString* gOriginalUserAgent = nil;
name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppLocaleDidChange:)
- name:NSCurrentLocaleDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification object:nil];
@@ -418,35 +413,10 @@ static NSString* gOriginalUserAgent = nil;
return [[UIWebView alloc] initWithFrame:bounds];
}
-+ (NSString*)originalUserAgent
-{
- if (gOriginalUserAgent == nil) {
- NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
- NSString* systemVersion = [[UIDevice currentDevice] systemVersion];
- NSString* localeStr = [[NSLocale currentLocale] localeIdentifier];
- NSString* systemAndLocale = [NSString stringWithFormat:@"%@ %@", systemVersion, localeStr];
-
- NSString* cordovaUserAgentVersion = [userDefaults stringForKey:CDV_USER_AGENT_VERSION_KEY];
- gOriginalUserAgent = [userDefaults stringForKey:CDV_USER_AGENT_KEY];
- BOOL cachedValueIsOld = ![systemAndLocale isEqualToString:cordovaUserAgentVersion];
-
- if ((gOriginalUserAgent == nil) || cachedValueIsOld) {
- UIWebView* sampleWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
- gOriginalUserAgent = [sampleWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
-
- [userDefaults setObject:gOriginalUserAgent forKey:CDV_USER_AGENT_KEY];
- [userDefaults setObject:systemAndLocale forKey:CDV_USER_AGENT_VERSION_KEY];
-
- [userDefaults synchronize];
- }
- }
- return gOriginalUserAgent;
-}
-
- (NSString*)userAgent
{
if (_userAgent == nil) {
- NSString* originalUserAgent = [[self class] originalUserAgent];
+ NSString* originalUserAgent = [CDVUserAgentUtil originalUserAgent];
// Use our address as a unique number to append to the User-Agent.
_userAgent = [NSString stringWithFormat:@"%@ (%lld)", originalUserAgent, (long long)self];
}
@@ -460,10 +430,7 @@ static NSString* gOriginalUserAgent = nil;
webViewBounds.origin = self.view.bounds.origin;
if (!self.webView) {
- // 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:self.userAgent, @"UserAgent", nil];
- [[NSUserDefaults standardUserDefaults] registerDefaults:dict];
+ [CDVUserAgentUtil setUserAgent:self.userAgent];
self.webView = [self newCordovaViewWithFrame:webViewBounds];
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
@@ -946,11 +913,6 @@ BOOL gSplashScreenShown = NO;
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('pause', null, true);" scheduledOnRunLoop:NO];
}
-- (void)onAppLocaleDidChange:(NSNotification*)notification
-{
- gOriginalUserAgent = nil;
-}
-
// ///////////////////////
- (void)handleOpenURL:(NSNotification*)notification
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/92fa3e45/CordovaLib/CordovaLib.xcodeproj/project.pbxproj
----------------------------------------------------------------------
diff --git a/CordovaLib/CordovaLib.xcodeproj/project.pbxproj b/CordovaLib/CordovaLib.xcodeproj/project.pbxproj
index f1f4a27..74ba2d5 100644
--- a/CordovaLib/CordovaLib.xcodeproj/project.pbxproj
+++ b/CordovaLib/CordovaLib.xcodeproj/project.pbxproj
@@ -80,6 +80,8 @@
EB3B357D161F2A45003DBE7D /* CDVCommandDelegateImpl.m in Sources */ = {isa = PBXBuildFile; fileRef = EB3B357B161F2A45003DBE7D /* CDVCommandDelegateImpl.m */; };
EB80C2AC15DEA63D004D9E7B /* CDVEcho.h in Headers */ = {isa = PBXBuildFile; fileRef = EB80C2AA15DEA63D004D9E7B /* CDVEcho.h */; settings = {ATTRIBUTES = (Public, ); }; };
EB80C2AD15DEA63D004D9E7B /* CDVEcho.m in Sources */ = {isa = PBXBuildFile; fileRef = EB80C2AB15DEA63D004D9E7B /* CDVEcho.m */; };
+ EB96673B16A8970A00D86CDF /* CDVUserAgentUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = EB96673916A8970900D86CDF /* CDVUserAgentUtil.h */; };
+ EB96673C16A8970A00D86CDF /* CDVUserAgentUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = EB96673A16A8970900D86CDF /* CDVUserAgentUtil.m */; };
EBA3557315ABD38C00F4DE24 /* NSArray+Comparisons.h in Headers */ = {isa = PBXBuildFile; fileRef = EBA3557115ABD38C00F4DE24 /* NSArray+Comparisons.h */; settings = {ATTRIBUTES = (Public, ); }; };
EBA3557515ABD38C00F4DE24 /* NSArray+Comparisons.m in Sources */ = {isa = PBXBuildFile; fileRef = EBA3557215ABD38C00F4DE24 /* NSArray+Comparisons.m */; };
F858FBC6166009A8007DA594 /* CDVConfigParser.h in Headers */ = {isa = PBXBuildFile; fileRef = F858FBC4166009A8007DA594 /* CDVConfigParser.h */; };
@@ -174,6 +176,8 @@
EB3B357B161F2A45003DBE7D /* CDVCommandDelegateImpl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CDVCommandDelegateImpl.m; path = Classes/CDVCommandDelegateImpl.m; sourceTree = "<group>"; };
EB80C2AA15DEA63D004D9E7B /* CDVEcho.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CDVEcho.h; path = Classes/CDVEcho.h; sourceTree = "<group>"; };
EB80C2AB15DEA63D004D9E7B /* CDVEcho.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CDVEcho.m; path = Classes/CDVEcho.m; sourceTree = "<group>"; };
+ EB96673916A8970900D86CDF /* CDVUserAgentUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CDVUserAgentUtil.h; path = Classes/CDVUserAgentUtil.h; sourceTree = "<group>"; };
+ EB96673A16A8970900D86CDF /* CDVUserAgentUtil.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CDVUserAgentUtil.m; path = Classes/CDVUserAgentUtil.m; sourceTree = "<group>"; };
EBA3557115ABD38C00F4DE24 /* NSArray+Comparisons.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSArray+Comparisons.h"; path = "Classes/NSArray+Comparisons.h"; sourceTree = "<group>"; };
EBA3557215ABD38C00F4DE24 /* NSArray+Comparisons.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSArray+Comparisons.m"; path = "Classes/NSArray+Comparisons.m"; sourceTree = "<group>"; };
F858FBC4166009A8007DA594 /* CDVConfigParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CDVConfigParser.h; path = Classes/CDVConfigParser.h; sourceTree = "<group>"; };
@@ -313,6 +317,8 @@
3073E9EC1656D51200957977 /* CDVScreenOrientationDelegate.h */,
30F39309169F839700B22307 /* CDVJSON.h */,
30F3930A169F839700B22307 /* CDVJSON.m */,
+ EB96673916A8970900D86CDF /* CDVUserAgentUtil.h */,
+ EB96673A16A8970900D86CDF /* CDVUserAgentUtil.m */,
);
name = Commands;
sourceTree = "<group>";
@@ -393,6 +399,7 @@
3073E9ED1656D51200957977 /* CDVScreenOrientationDelegate.h in Headers */,
F858FBC6166009A8007DA594 /* CDVConfigParser.h in Headers */,
30F3930B169F839700B22307 /* CDVJSON.h in Headers */,
+ EB96673B16A8970A00D86CDF /* CDVUserAgentUtil.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -486,6 +493,7 @@
3073E9EA1656D37700957977 /* CDVInAppBrowser.m in Sources */,
F858FBC7166009A8007DA594 /* CDVConfigParser.m in Sources */,
30F3930C169F839700B22307 /* CDVJSON.m in Sources */,
+ EB96673C16A8970A00D86CDF /* CDVUserAgentUtil.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};