PageRenderTime 29ms CodeModel.GetById 335ms RepoModel.GetById 1ms app.codeStats 0ms

/platforms/ios/CordovaLib/Classes/Public/CDVViewController.m

https://gitlab.com/blocknotary/IonicInterviews
Objective C | 784 lines | 557 code | 136 blank | 91 comment | 119 complexity | 9c581d67409c5c3f0cb3d8f7036b4d15 MD5 | raw file
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import <objc/message.h>
  18. #import "CDV.h"
  19. #import "CDVPlugin+Private.h"
  20. #import "CDVUIWebViewDelegate.h"
  21. #import "CDVConfigParser.h"
  22. #import "CDVUserAgentUtil.h"
  23. #import <AVFoundation/AVFoundation.h>
  24. #import "NSDictionary+CordovaPreferences.h"
  25. #import "CDVLocalStorage.h"
  26. #import "CDVCommandDelegateImpl.h"
  27. #import <Foundation/NSCharacterSet.h>
  28. @interface CDVViewController () {
  29. NSInteger _userAgentLockToken;
  30. }
  31. @property (nonatomic, readwrite, strong) NSXMLParser* configParser;
  32. @property (nonatomic, readwrite, strong) NSMutableDictionary* settings;
  33. @property (nonatomic, readwrite, strong) NSMutableDictionary* pluginObjects;
  34. @property (nonatomic, readwrite, strong) NSMutableArray* startupPluginNames;
  35. @property (nonatomic, readwrite, strong) NSDictionary* pluginsMap;
  36. @property (nonatomic, readwrite, strong) id <CDVWebViewEngineProtocol> webViewEngine;
  37. @property (readwrite, assign) BOOL initialized;
  38. @property (atomic, strong) NSURL* openURL;
  39. @end
  40. @implementation CDVViewController
  41. @synthesize supportedOrientations;
  42. @synthesize pluginObjects, pluginsMap, startupPluginNames;
  43. @synthesize configParser, settings;
  44. @synthesize wwwFolderName, startPage, initialized, openURL, baseUserAgent;
  45. @synthesize commandDelegate = _commandDelegate;
  46. @synthesize commandQueue = _commandQueue;
  47. @synthesize webViewEngine = _webViewEngine;
  48. @dynamic webView;
  49. - (void)__init
  50. {
  51. if ((self != nil) && !self.initialized) {
  52. _commandQueue = [[CDVCommandQueue alloc] initWithViewController:self];
  53. _commandDelegate = [[CDVCommandDelegateImpl alloc] initWithViewController:self];
  54. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillTerminate:)
  55. name:UIApplicationWillTerminateNotification object:nil];
  56. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillResignActive:)
  57. name:UIApplicationWillResignActiveNotification object:nil];
  58. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidBecomeActive:)
  59. name:UIApplicationDidBecomeActiveNotification object:nil];
  60. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillEnterForeground:)
  61. name:UIApplicationWillEnterForegroundNotification object:nil];
  62. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidEnterBackground:)
  63. name:UIApplicationDidEnterBackgroundNotification object:nil];
  64. // read from UISupportedInterfaceOrientations (or UISupportedInterfaceOrientations~iPad, if its iPad) from -Info.plist
  65. self.supportedOrientations = [self parseInterfaceOrientations:
  66. [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"]];
  67. [self printVersion];
  68. [self printMultitaskingInfo];
  69. [self printPlatformVersionWarning];
  70. self.initialized = YES;
  71. }
  72. }
  73. - (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
  74. {
  75. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  76. [self __init];
  77. return self;
  78. }
  79. - (id)initWithCoder:(NSCoder*)aDecoder
  80. {
  81. self = [super initWithCoder:aDecoder];
  82. [self __init];
  83. return self;
  84. }
  85. - (id)init
  86. {
  87. self = [super init];
  88. [self __init];
  89. return self;
  90. }
  91. - (void)printVersion
  92. {
  93. NSLog(@"Apache Cordova native platform version %@ is starting.", CDV_VERSION);
  94. }
  95. - (void)printPlatformVersionWarning
  96. {
  97. if (!IsAtLeastiOSVersion(@"8.0")) {
  98. NSLog(@"CRITICAL: For Cordova 4.0.0 and above, you will need to upgrade to at least iOS 8.0 or greater. Your current version of iOS is %@.",
  99. [[UIDevice currentDevice] systemVersion]
  100. );
  101. }
  102. }
  103. - (void)printMultitaskingInfo
  104. {
  105. UIDevice* device = [UIDevice currentDevice];
  106. BOOL backgroundSupported = NO;
  107. if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
  108. backgroundSupported = device.multitaskingSupported;
  109. }
  110. NSNumber* exitsOnSuspend = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIApplicationExitsOnSuspend"];
  111. if (exitsOnSuspend == nil) { // if it's missing, it should be NO (i.e. multi-tasking on by default)
  112. exitsOnSuspend = [NSNumber numberWithBool:NO];
  113. }
  114. NSLog(@"Multi-tasking -> Device: %@, App: %@", (backgroundSupported ? @"YES" : @"NO"), (![exitsOnSuspend intValue]) ? @"YES" : @"NO");
  115. }
  116. -(NSString*)configFilePath{
  117. NSString* path = self.configFile ?: @"config.xml";
  118. // if path is relative, resolve it against the main bundle
  119. if(![path isAbsolutePath]){
  120. NSString* absolutePath = [[NSBundle mainBundle] pathForResource:path ofType:nil];
  121. if(!absolutePath){
  122. NSAssert(NO, @"ERROR: %@ not found in the main bundle!", path);
  123. }
  124. path = absolutePath;
  125. }
  126. // Assert file exists
  127. if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
  128. NSAssert(NO, @"ERROR: %@ does not exist. Please run cordova-ios/bin/cordova_plist_to_config_xml path/to/project.", path);
  129. return nil;
  130. }
  131. return path;
  132. }
  133. - (void)parseSettingsWithParser:(NSObject <NSXMLParserDelegate>*)delegate
  134. {
  135. // read from config.xml in the app bundle
  136. NSString* path = [self configFilePath];
  137. NSURL* url = [NSURL fileURLWithPath:path];
  138. self.configParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
  139. if (self.configParser == nil) {
  140. NSLog(@"Failed to initialize XML parser.");
  141. return;
  142. }
  143. [self.configParser setDelegate:((id < NSXMLParserDelegate >)delegate)];
  144. [self.configParser parse];
  145. }
  146. - (void)loadSettings
  147. {
  148. CDVConfigParser* delegate = [[CDVConfigParser alloc] init];
  149. [self parseSettingsWithParser:delegate];
  150. // Get the plugin dictionary, whitelist and settings from the delegate.
  151. self.pluginsMap = delegate.pluginsDict;
  152. self.startupPluginNames = delegate.startupPluginNames;
  153. self.settings = delegate.settings;
  154. // And the start folder/page.
  155. if(self.wwwFolderName == nil){
  156. self.wwwFolderName = @"www";
  157. }
  158. if(delegate.startPage && self.startPage == nil){
  159. self.startPage = delegate.startPage;
  160. }
  161. if (self.startPage == nil) {
  162. self.startPage = @"index.html";
  163. }
  164. // Initialize the plugin objects dict.
  165. self.pluginObjects = [[NSMutableDictionary alloc] initWithCapacity:20];
  166. }
  167. - (NSURL*)appUrl
  168. {
  169. NSURL* appURL = nil;
  170. if ([self.startPage rangeOfString:@"://"].location != NSNotFound) {
  171. appURL = [NSURL URLWithString:self.startPage];
  172. } else if ([self.wwwFolderName rangeOfString:@"://"].location != NSNotFound) {
  173. appURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.wwwFolderName, self.startPage]];
  174. } else if([self.wwwFolderName rangeOfString:@".bundle"].location != NSNotFound){
  175. // www folder is actually a bundle
  176. NSBundle* bundle = [NSBundle bundleWithPath:self.wwwFolderName];
  177. appURL = [bundle URLForResource:self.startPage withExtension:nil];
  178. } else if([self.wwwFolderName rangeOfString:@".framework"].location != NSNotFound){
  179. // www folder is actually a framework
  180. NSBundle* bundle = [NSBundle bundleWithPath:self.wwwFolderName];
  181. appURL = [bundle URLForResource:self.startPage withExtension:nil];
  182. } else {
  183. // CB-3005 strip parameters from start page to check if page exists in resources
  184. NSURL* startURL = [NSURL URLWithString:self.startPage];
  185. NSString* startFilePath = [self.commandDelegate pathForResource:[startURL path]];
  186. if (startFilePath == nil) {
  187. appURL = nil;
  188. } else {
  189. appURL = [NSURL fileURLWithPath:startFilePath];
  190. // CB-3005 Add on the query params or fragment.
  191. NSString* startPageNoParentDirs = self.startPage;
  192. NSRange r = [startPageNoParentDirs rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"?#"] options:0];
  193. if (r.location != NSNotFound) {
  194. NSString* queryAndOrFragment = [self.startPage substringFromIndex:r.location];
  195. appURL = [NSURL URLWithString:queryAndOrFragment relativeToURL:appURL];
  196. }
  197. }
  198. }
  199. return appURL;
  200. }
  201. - (NSURL*)errorURL
  202. {
  203. NSURL* errorUrl = nil;
  204. id setting = [self.settings cordovaSettingForKey:@"ErrorUrl"];
  205. if (setting) {
  206. NSString* errorUrlString = (NSString*)setting;
  207. if ([errorUrlString rangeOfString:@"://"].location != NSNotFound) {
  208. errorUrl = [NSURL URLWithString:errorUrlString];
  209. } else {
  210. NSURL* url = [NSURL URLWithString:(NSString*)setting];
  211. NSString* errorFilePath = [self.commandDelegate pathForResource:[url path]];
  212. if (errorFilePath) {
  213. errorUrl = [NSURL fileURLWithPath:errorFilePath];
  214. }
  215. }
  216. }
  217. return errorUrl;
  218. }
  219. - (UIView*)webView
  220. {
  221. if (self.webViewEngine != nil) {
  222. return self.webViewEngine.engineWebView;
  223. }
  224. return nil;
  225. }
  226. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  227. - (void)viewDidLoad
  228. {
  229. [super viewDidLoad];
  230. // Load settings
  231. [self loadSettings];
  232. NSString* backupWebStorageType = @"cloud"; // default value
  233. id backupWebStorage = [self.settings cordovaSettingForKey:@"BackupWebStorage"];
  234. if ([backupWebStorage isKindOfClass:[NSString class]]) {
  235. backupWebStorageType = backupWebStorage;
  236. }
  237. [self.settings setCordovaSetting:backupWebStorageType forKey:@"BackupWebStorage"];
  238. [CDVLocalStorage __fixupDatabaseLocationsWithBackupType:backupWebStorageType];
  239. // // Instantiate the WebView ///////////////
  240. if (!self.webView) {
  241. [self createGapView];
  242. }
  243. // /////////////////
  244. /*
  245. * Fire up CDVLocalStorage to work-around WebKit storage limitations: on all iOS 5.1+ versions for local-only backups, but only needed on iOS 5.1 for cloud backup.
  246. With minimum iOS 7/8 supported, only first clause applies.
  247. */
  248. if ([backupWebStorageType isEqualToString:@"local"]) {
  249. NSString* localStorageFeatureName = @"localstorage";
  250. if ([self.pluginsMap objectForKey:localStorageFeatureName]) { // plugin specified in config
  251. [self.startupPluginNames addObject:localStorageFeatureName];
  252. }
  253. }
  254. if ([self.startupPluginNames count] > 0) {
  255. [CDVTimer start:@"TotalPluginStartup"];
  256. for (NSString* pluginName in self.startupPluginNames) {
  257. [CDVTimer start:pluginName];
  258. [self getCommandInstance:pluginName];
  259. [CDVTimer stop:pluginName];
  260. }
  261. [CDVTimer stop:@"TotalPluginStartup"];
  262. }
  263. // /////////////////
  264. NSURL* appURL = [self appUrl];
  265. __weak __typeof__(self) weakSelf = self;
  266. [CDVUserAgentUtil acquireLock:^(NSInteger lockToken) {
  267. // Fix the memory leak caused by the strong reference.
  268. [weakSelf setLockToken:lockToken];
  269. if (appURL) {
  270. NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
  271. [self.webViewEngine loadRequest:appReq];
  272. } else {
  273. NSString* loadErr = [NSString stringWithFormat:@"ERROR: Start Page at '%@/%@' was not found.", self.wwwFolderName, self.startPage];
  274. NSLog(@"%@", loadErr);
  275. NSURL* errorUrl = [self errorURL];
  276. if (errorUrl) {
  277. errorUrl = [NSURL URLWithString:[NSString stringWithFormat:@"?error=%@", [loadErr stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLPathAllowedCharacterSet]] relativeToURL:errorUrl];
  278. NSLog(@"%@", [errorUrl absoluteString]);
  279. [self.webViewEngine loadRequest:[NSURLRequest requestWithURL:errorUrl]];
  280. } else {
  281. NSString* html = [NSString stringWithFormat:@"<html><body> %@ </body></html>", loadErr];
  282. [self.webViewEngine loadHTMLString:html baseURL:nil];
  283. }
  284. }
  285. }];
  286. // /////////////////
  287. NSString* bgColorString = [self.settings cordovaSettingForKey:@"BackgroundColor"];
  288. UIColor* bgColor = [self colorFromColorString:bgColorString];
  289. [self.webView setBackgroundColor:bgColor];
  290. }
  291. - (void)setLockToken:(NSInteger)lockToken
  292. {
  293. _userAgentLockToken = lockToken;
  294. [CDVUserAgentUtil setUserAgent:self.userAgent lockToken:lockToken];
  295. }
  296. -(void)viewWillAppear:(BOOL)animated
  297. {
  298. [super viewWillAppear:animated];
  299. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewWillAppearNotification object:nil]];
  300. }
  301. -(void)viewDidAppear:(BOOL)animated
  302. {
  303. [super viewDidAppear:animated];
  304. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewDidAppearNotification object:nil]];
  305. }
  306. -(void)viewWillDisappear:(BOOL)animated
  307. {
  308. [super viewWillDisappear:animated];
  309. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewWillDisappearNotification object:nil]];
  310. }
  311. -(void)viewDidDisappear:(BOOL)animated
  312. {
  313. [super viewDidDisappear:animated];
  314. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewDidDisappearNotification object:nil]];
  315. }
  316. -(void)viewWillLayoutSubviews
  317. {
  318. [super viewWillLayoutSubviews];
  319. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewWillLayoutSubviewsNotification object:nil]];
  320. }
  321. -(void)viewDidLayoutSubviews
  322. {
  323. [super viewDidLayoutSubviews];
  324. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewDidLayoutSubviewsNotification object:nil]];
  325. }
  326. -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  327. {
  328. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  329. [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewWillTransitionToSizeNotification object:[NSValue valueWithCGSize:size]]];
  330. }
  331. - (UIColor*)colorFromColorString:(NSString*)colorString
  332. {
  333. // No value, nothing to do
  334. if (!colorString) {
  335. return nil;
  336. }
  337. // Validate format
  338. NSError* error = NULL;
  339. NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"^(#[0-9A-F]{3}|(0x|#)([0-9A-F]{2})?[0-9A-F]{6})$" options:NSRegularExpressionCaseInsensitive error:&error];
  340. NSUInteger countMatches = [regex numberOfMatchesInString:colorString options:0 range:NSMakeRange(0, [colorString length])];
  341. if (!countMatches) {
  342. return nil;
  343. }
  344. // #FAB to #FFAABB
  345. if ([colorString hasPrefix:@"#"] && [colorString length] == 4) {
  346. NSString* r = [colorString substringWithRange:NSMakeRange(1, 1)];
  347. NSString* g = [colorString substringWithRange:NSMakeRange(2, 1)];
  348. NSString* b = [colorString substringWithRange:NSMakeRange(3, 1)];
  349. colorString = [NSString stringWithFormat:@"#%@%@%@%@%@%@", r, r, g, g, b, b];
  350. }
  351. // #RRGGBB to 0xRRGGBB
  352. colorString = [colorString stringByReplacingOccurrencesOfString:@"#" withString:@"0x"];
  353. // 0xRRGGBB to 0xAARRGGBB
  354. if ([colorString hasPrefix:@"0x"] && [colorString length] == 8) {
  355. colorString = [@"0xFF" stringByAppendingString:[colorString substringFromIndex:2]];
  356. }
  357. // 0xAARRGGBB to int
  358. unsigned colorValue = 0;
  359. NSScanner *scanner = [NSScanner scannerWithString:colorString];
  360. if (![scanner scanHexInt:&colorValue]) {
  361. return nil;
  362. }
  363. // int to UIColor
  364. return [UIColor colorWithRed:((float)((colorValue & 0x00FF0000) >> 16))/255.0
  365. green:((float)((colorValue & 0x0000FF00) >> 8))/255.0
  366. blue:((float)((colorValue & 0x000000FF) >> 0))/255.0
  367. alpha:((float)((colorValue & 0xFF000000) >> 24))/255.0];
  368. }
  369. - (NSArray*)parseInterfaceOrientations:(NSArray*)orientations
  370. {
  371. NSMutableArray* result = [[NSMutableArray alloc] init];
  372. if (orientations != nil) {
  373. NSEnumerator* enumerator = [orientations objectEnumerator];
  374. NSString* orientationString;
  375. while (orientationString = [enumerator nextObject]) {
  376. if ([orientationString isEqualToString:@"UIInterfaceOrientationPortrait"]) {
  377. [result addObject:[NSNumber numberWithInt:UIInterfaceOrientationPortrait]];
  378. } else if ([orientationString isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]) {
  379. [result addObject:[NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown]];
  380. } else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeLeft"]) {
  381. [result addObject:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]];
  382. } else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) {
  383. [result addObject:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight]];
  384. }
  385. }
  386. }
  387. // default
  388. if ([result count] == 0) {
  389. [result addObject:[NSNumber numberWithInt:UIInterfaceOrientationPortrait]];
  390. }
  391. return result;
  392. }
  393. - (BOOL)shouldAutorotate
  394. {
  395. return YES;
  396. }
  397. // CB-12098
  398. #if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
  399. - (NSUInteger)supportedInterfaceOrientations
  400. #else
  401. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  402. #endif
  403. {
  404. NSUInteger ret = 0;
  405. if ([self supportsOrientation:UIInterfaceOrientationPortrait]) {
  406. ret = ret | (1 << UIInterfaceOrientationPortrait);
  407. }
  408. if ([self supportsOrientation:UIInterfaceOrientationPortraitUpsideDown]) {
  409. ret = ret | (1 << UIInterfaceOrientationPortraitUpsideDown);
  410. }
  411. if ([self supportsOrientation:UIInterfaceOrientationLandscapeRight]) {
  412. ret = ret | (1 << UIInterfaceOrientationLandscapeRight);
  413. }
  414. if ([self supportsOrientation:UIInterfaceOrientationLandscapeLeft]) {
  415. ret = ret | (1 << UIInterfaceOrientationLandscapeLeft);
  416. }
  417. return ret;
  418. }
  419. - (BOOL)supportsOrientation:(UIInterfaceOrientation)orientation
  420. {
  421. return [self.supportedOrientations containsObject:[NSNumber numberWithInt:orientation]];
  422. }
  423. - (UIView*)newCordovaViewWithFrame:(CGRect)bounds
  424. {
  425. NSString* defaultWebViewEngineClass = [self.settings cordovaSettingForKey:@"CordovaDefaultWebViewEngine"];
  426. NSString* webViewEngineClass = [self.settings cordovaSettingForKey:@"CordovaWebViewEngine"];
  427. if (!defaultWebViewEngineClass) {
  428. defaultWebViewEngineClass = @"CDVUIWebViewEngine";
  429. }
  430. if (!webViewEngineClass) {
  431. webViewEngineClass = defaultWebViewEngineClass;
  432. }
  433. // Find webViewEngine
  434. if (NSClassFromString(webViewEngineClass)) {
  435. self.webViewEngine = [[NSClassFromString(webViewEngineClass) alloc] initWithFrame:bounds];
  436. // if a webView engine returns nil (not supported by the current iOS version) or doesn't conform to the protocol, or can't load the request, we use UIWebView
  437. if (!self.webViewEngine || ![self.webViewEngine conformsToProtocol:@protocol(CDVWebViewEngineProtocol)] || ![self.webViewEngine canLoadRequest:[NSURLRequest requestWithURL:self.appUrl]]) {
  438. self.webViewEngine = [[NSClassFromString(defaultWebViewEngineClass) alloc] initWithFrame:bounds];
  439. }
  440. } else {
  441. self.webViewEngine = [[NSClassFromString(defaultWebViewEngineClass) alloc] initWithFrame:bounds];
  442. }
  443. if ([self.webViewEngine isKindOfClass:[CDVPlugin class]]) {
  444. [self registerPlugin:(CDVPlugin*)self.webViewEngine withClassName:webViewEngineClass];
  445. }
  446. return self.webViewEngine.engineWebView;
  447. }
  448. - (NSString*)userAgent
  449. {
  450. if (_userAgent != nil) {
  451. return _userAgent;
  452. }
  453. NSString* localBaseUserAgent;
  454. if (self.baseUserAgent != nil) {
  455. localBaseUserAgent = self.baseUserAgent;
  456. } else if ([self.settings cordovaSettingForKey:@"OverrideUserAgent"] != nil) {
  457. localBaseUserAgent = [self.settings cordovaSettingForKey:@"OverrideUserAgent"];
  458. } else {
  459. localBaseUserAgent = [CDVUserAgentUtil originalUserAgent];
  460. }
  461. NSString* appendUserAgent = [self.settings cordovaSettingForKey:@"AppendUserAgent"];
  462. if (appendUserAgent) {
  463. _userAgent = [NSString stringWithFormat:@"%@ %@", localBaseUserAgent, appendUserAgent];
  464. } else {
  465. // Use our address as a unique number to append to the User-Agent.
  466. _userAgent = localBaseUserAgent;
  467. }
  468. return _userAgent;
  469. }
  470. - (void)createGapView
  471. {
  472. CGRect webViewBounds = self.view.bounds;
  473. webViewBounds.origin = self.view.bounds.origin;
  474. UIView* view = [self newCordovaViewWithFrame:webViewBounds];
  475. view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
  476. [self.view addSubview:view];
  477. [self.view sendSubviewToBack:view];
  478. }
  479. - (void)didReceiveMemoryWarning
  480. {
  481. // iterate through all the plugin objects, and call hasPendingOperation
  482. // if at least one has a pending operation, we don't call [super didReceiveMemoryWarning]
  483. NSEnumerator* enumerator = [self.pluginObjects objectEnumerator];
  484. CDVPlugin* plugin;
  485. BOOL doPurge = YES;
  486. while ((plugin = [enumerator nextObject])) {
  487. if (plugin.hasPendingOperation) {
  488. NSLog(@"Plugin '%@' has a pending operation, memory purge is delayed for didReceiveMemoryWarning.", NSStringFromClass([plugin class]));
  489. doPurge = NO;
  490. }
  491. }
  492. if (doPurge) {
  493. // Releases the view if it doesn't have a superview.
  494. [super didReceiveMemoryWarning];
  495. }
  496. // Release any cached data, images, etc. that aren't in use.
  497. }
  498. - (void)viewDidUnload
  499. {
  500. // Release any retained subviews of the main view.
  501. // e.g. self.myOutlet = nil;
  502. [CDVUserAgentUtil releaseLock:&_userAgentLockToken];
  503. [super viewDidUnload];
  504. }
  505. #pragma mark CordovaCommands
  506. - (void)registerPlugin:(CDVPlugin*)plugin withClassName:(NSString*)className
  507. {
  508. if ([plugin respondsToSelector:@selector(setViewController:)]) {
  509. [plugin setViewController:self];
  510. }
  511. if ([plugin respondsToSelector:@selector(setCommandDelegate:)]) {
  512. [plugin setCommandDelegate:_commandDelegate];
  513. }
  514. [self.pluginObjects setObject:plugin forKey:className];
  515. [plugin pluginInitialize];
  516. }
  517. - (void)registerPlugin:(CDVPlugin*)plugin withPluginName:(NSString*)pluginName
  518. {
  519. if ([plugin respondsToSelector:@selector(setViewController:)]) {
  520. [plugin setViewController:self];
  521. }
  522. if ([plugin respondsToSelector:@selector(setCommandDelegate:)]) {
  523. [plugin setCommandDelegate:_commandDelegate];
  524. }
  525. NSString* className = NSStringFromClass([plugin class]);
  526. [self.pluginObjects setObject:plugin forKey:className];
  527. [self.pluginsMap setValue:className forKey:[pluginName lowercaseString]];
  528. [plugin pluginInitialize];
  529. }
  530. /**
  531. Returns an instance of a CordovaCommand object, based on its name. If one exists already, it is returned.
  532. */
  533. - (id)getCommandInstance:(NSString*)pluginName
  534. {
  535. // first, we try to find the pluginName in the pluginsMap
  536. // (acts as a whitelist as well) if it does not exist, we return nil
  537. // NOTE: plugin names are matched as lowercase to avoid problems - however, a
  538. // possible issue is there can be duplicates possible if you had:
  539. // "org.apache.cordova.Foo" and "org.apache.cordova.foo" - only the lower-cased entry will match
  540. NSString* className = [self.pluginsMap objectForKey:[pluginName lowercaseString]];
  541. if (className == nil) {
  542. return nil;
  543. }
  544. id obj = [self.pluginObjects objectForKey:className];
  545. if (!obj) {
  546. obj = [[NSClassFromString(className)alloc] initWithWebViewEngine:_webViewEngine];
  547. if (obj != nil) {
  548. [self registerPlugin:obj withClassName:className];
  549. } else {
  550. NSLog(@"CDVPlugin class %@ (pluginName: %@) does not exist.", className, pluginName);
  551. }
  552. }
  553. return obj;
  554. }
  555. #pragma mark -
  556. - (NSString*)appURLScheme
  557. {
  558. NSString* URLScheme = nil;
  559. NSArray* URLTypes = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleURLTypes"];
  560. if (URLTypes != nil) {
  561. NSDictionary* dict = [URLTypes objectAtIndex:0];
  562. if (dict != nil) {
  563. NSArray* URLSchemes = [dict objectForKey:@"CFBundleURLSchemes"];
  564. if (URLSchemes != nil) {
  565. URLScheme = [URLSchemes objectAtIndex:0];
  566. }
  567. }
  568. }
  569. return URLScheme;
  570. }
  571. #pragma mark -
  572. #pragma mark UIApplicationDelegate impl
  573. /*
  574. This method lets your application know that it is about to be terminated and purged from memory entirely
  575. */
  576. - (void)onAppWillTerminate:(NSNotification*)notification
  577. {
  578. // empty the tmp directory
  579. NSFileManager* fileMgr = [[NSFileManager alloc] init];
  580. NSError* __autoreleasing err = nil;
  581. // clear contents of NSTemporaryDirectory
  582. NSString* tempDirectoryPath = NSTemporaryDirectory();
  583. NSDirectoryEnumerator* directoryEnumerator = [fileMgr enumeratorAtPath:tempDirectoryPath];
  584. NSString* fileName = nil;
  585. BOOL result;
  586. while ((fileName = [directoryEnumerator nextObject])) {
  587. NSString* filePath = [tempDirectoryPath stringByAppendingPathComponent:fileName];
  588. result = [fileMgr removeItemAtPath:filePath error:&err];
  589. if (!result && err) {
  590. NSLog(@"Failed to delete: %@ (error: %@)", filePath, err);
  591. }
  592. }
  593. }
  594. /*
  595. This method is called to let your application know that it is about to move from the active to inactive state.
  596. You should use this method to pause ongoing tasks, disable timer, ...
  597. */
  598. - (void)onAppWillResignActive:(NSNotification*)notification
  599. {
  600. // NSLog(@"%@",@"applicationWillResignActive");
  601. [self.commandDelegate evalJs:@"cordova.fireDocumentEvent('resign');" scheduledOnRunLoop:NO];
  602. }
  603. /*
  604. In iOS 4.0 and later, this method is called as part of the transition from the background to the inactive state.
  605. You can use this method to undo many of the changes you made to your application upon entering the background.
  606. invariably followed by applicationDidBecomeActive
  607. */
  608. - (void)onAppWillEnterForeground:(NSNotification*)notification
  609. {
  610. // NSLog(@"%@",@"applicationWillEnterForeground");
  611. [self.commandDelegate evalJs:@"cordova.fireDocumentEvent('resume');"];
  612. /** Clipboard fix **/
  613. UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
  614. NSString* string = pasteboard.string;
  615. if (string) {
  616. [pasteboard setValue:string forPasteboardType:@"public.text"];
  617. }
  618. }
  619. // This method is called to let your application know that it moved from the inactive to active state.
  620. - (void)onAppDidBecomeActive:(NSNotification*)notification
  621. {
  622. // NSLog(@"%@",@"applicationDidBecomeActive");
  623. [self.commandDelegate evalJs:@"cordova.fireDocumentEvent('active');"];
  624. }
  625. /*
  626. In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method
  627. when the user quits an application that supports background execution.
  628. */
  629. - (void)onAppDidEnterBackground:(NSNotification*)notification
  630. {
  631. // NSLog(@"%@",@"applicationDidEnterBackground");
  632. [self.commandDelegate evalJs:@"cordova.fireDocumentEvent('pause', null, true);" scheduledOnRunLoop:NO];
  633. }
  634. // ///////////////////////
  635. - (void)dealloc
  636. {
  637. [[NSNotificationCenter defaultCenter] removeObserver:self];
  638. [CDVUserAgentUtil releaseLock:&_userAgentLockToken];
  639. [_commandQueue dispose];
  640. [[self.pluginObjects allValues] makeObjectsPerformSelector:@selector(dispose)];
  641. }
  642. - (NSInteger*)userAgentLockToken
  643. {
  644. return &_userAgentLockToken;
  645. }
  646. @end