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

CDVHandleOpenURL.m 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "CDVHandleOpenURL.h"
  18. #import <Cordova/CDV.h>
  19. @implementation CDVHandleOpenURL
  20. - (void)pluginInitialize
  21. {
  22. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationLaunchedWithUrl:) name:CDVPluginHandleOpenURLNotification object:nil];
  23. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationPageDidLoad:) name:CDVPageDidLoadNotification object:nil];
  24. }
  25. - (void)applicationLaunchedWithUrl:(NSNotification*)notification
  26. {
  27. NSURL* url = [notification object];
  28. self.url = url;
  29. // warm-start handler
  30. if (self.pageLoaded) {
  31. [self processOpenUrl:self.url pageLoaded:YES];
  32. self.url = nil;
  33. }
  34. }
  35. - (void)applicationPageDidLoad:(NSNotification*)notification
  36. {
  37. // cold-start handler
  38. self.pageLoaded = YES;
  39. if (self.url) {
  40. [self processOpenUrl:self.url pageLoaded:YES];
  41. self.url = nil;
  42. }
  43. }
  44. - (void)processOpenUrl:(NSURL*)url pageLoaded:(BOOL)pageLoaded
  45. {
  46. __weak __typeof(self) weakSelf = self;
  47. dispatch_block_t handleOpenUrl = ^(void) {
  48. // calls into javascript global function 'handleOpenURL'
  49. NSString* jsString = [NSString stringWithFormat:@"document.addEventListener('deviceready',function(){if (typeof handleOpenURL === 'function') { handleOpenURL(\"%@\");}});", url.absoluteString];
  50. [weakSelf.webViewEngine evaluateJavaScript:jsString completionHandler:nil];
  51. };
  52. if (!pageLoaded) {
  53. NSString* jsString = @"document.readystate";
  54. [self.webViewEngine evaluateJavaScript:jsString
  55. completionHandler:^(id object, NSError* error) {
  56. if ((error == nil) && [object isKindOfClass:[NSString class]]) {
  57. NSString* readyState = (NSString*)object;
  58. BOOL ready = [readyState isEqualToString:@"loaded"] || [readyState isEqualToString:@"complete"];
  59. if (ready) {
  60. handleOpenUrl();
  61. } else {
  62. self.url = url;
  63. }
  64. }
  65. }];
  66. } else {
  67. handleOpenUrl();
  68. }
  69. }
  70. @end