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

CDVURLSchemeHandler.m 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "CDVURLSchemeHandler.h"
  18. #import <MobileCoreServices/MobileCoreServices.h>
  19. @implementation CDVURLSchemeHandler
  20. - (instancetype)initWithVC:(CDVViewController *)controller
  21. {
  22. self = [super init];
  23. if (self) {
  24. _viewController = controller;
  25. }
  26. return self;
  27. }
  28. - (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)urlSchemeTask
  29. {
  30. NSString * startPath = [[NSBundle mainBundle] pathForResource:self.viewController.wwwFolderName ofType: nil];
  31. NSURL * url = urlSchemeTask.request.URL;
  32. NSString * stringToLoad = url.path;
  33. NSString * scheme = url.scheme;
  34. if ([scheme isEqualToString:self.viewController.appScheme]) {
  35. if ([stringToLoad hasPrefix:@"/_app_file_"]) {
  36. startPath = [stringToLoad stringByReplacingOccurrencesOfString:@"/_app_file_" withString:@""];
  37. } else {
  38. if ([stringToLoad isEqualToString:@""] || [url.pathExtension isEqualToString:@""]) {
  39. startPath = [startPath stringByAppendingPathComponent:self.viewController.startPage];
  40. } else {
  41. startPath = [startPath stringByAppendingPathComponent:stringToLoad];
  42. }
  43. }
  44. }
  45. NSError * fileError = nil;
  46. NSData * data = nil;
  47. if ([self isMediaExtension:url.pathExtension]) {
  48. data = [NSData dataWithContentsOfFile:startPath options:NSDataReadingMappedIfSafe error:&fileError];
  49. }
  50. if (!data || fileError) {
  51. data = [[NSData alloc] initWithContentsOfFile:startPath];
  52. }
  53. NSInteger statusCode = 200;
  54. if (!data) {
  55. statusCode = 404;
  56. }
  57. NSURL * localUrl = [NSURL URLWithString:url.absoluteString];
  58. NSString * mimeType = [self getMimeType:url.pathExtension];
  59. id response = nil;
  60. if (data && [self isMediaExtension:url.pathExtension]) {
  61. response = [[NSURLResponse alloc] initWithURL:localUrl MIMEType:mimeType expectedContentLength:data.length textEncodingName:nil];
  62. } else {
  63. NSDictionary * headers = @{ @"Content-Type" : mimeType, @"Cache-Control": @"no-cache"};
  64. response = [[NSHTTPURLResponse alloc] initWithURL:localUrl statusCode:statusCode HTTPVersion:nil headerFields:headers];
  65. }
  66. [urlSchemeTask didReceiveResponse:response];
  67. [urlSchemeTask didReceiveData:data];
  68. [urlSchemeTask didFinish];
  69. }
  70. - (void)webView:(nonnull WKWebView *)webView stopURLSchemeTask:(nonnull id<WKURLSchemeTask>)urlSchemeTask
  71. {
  72. }
  73. -(NSString *) getMimeType:(NSString *)fileExtension {
  74. if (fileExtension && ![fileExtension isEqualToString:@""]) {
  75. NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExtension, NULL);
  76. NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);
  77. return contentType ? contentType : @"application/octet-stream";
  78. } else {
  79. return @"text/html";
  80. }
  81. }
  82. -(BOOL) isMediaExtension:(NSString *) pathExtension {
  83. NSArray * mediaExtensions = @[@"m4v", @"mov", @"mp4",
  84. @"aac", @"ac3", @"aiff", @"au", @"flac", @"m4a", @"mp3", @"wav"];
  85. if ([mediaExtensions containsObject:pathExtension.lowercaseString]) {
  86. return YES;
  87. }
  88. return NO;
  89. }
  90. @end