Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CDVViewController.m 29KB

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