FLEXWebViewController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // FLEXWebViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/10/14.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXWebViewController.h"
  9. #import "FLEXUtility.h"
  10. #import <WebKit/WebKit.h>
  11. #import <TargetConditionals.h>
  12. @interface FLEXWebViewController () <WKNavigationDelegate>
  13. #if !TARGET_OS_TV
  14. @property (nonatomic) WKWebView *webView;
  15. #else
  16. @property (nonatomic) UIWebView *webView;
  17. #endif
  18. @property (nonatomic) NSString *originalText;
  19. @end
  20. @implementation FLEXWebViewController
  21. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  22. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  23. if (self) {
  24. #if !TARGET_OS_TV
  25. WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
  26. if (@available(iOS 10.0, *)) {
  27. configuration.dataDetectorTypes = UIDataDetectorTypeLink;
  28. }
  29. self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
  30. self.webView.navigationDelegate = self;
  31. #else
  32. self.webView = [[UIWebView alloc] initWithFrame:CGRectZero];
  33. self.webView.delegate = self;
  34. #endif
  35. }
  36. return self;
  37. }
  38. - (id)initWithText:(NSString *)text {
  39. self = [self initWithNibName:nil bundle:nil];
  40. if (self) {
  41. self.originalText = text;
  42. NSString *htmlString = [NSString stringWithFormat:@"<head><meta name='viewport' content='initial-scale=1.0'></head><body><pre>%@</pre></body>", [FLEXUtility stringByEscapingHTMLEntitiesInString:text]];
  43. [self.webView loadHTMLString:htmlString baseURL:nil];
  44. }
  45. return self;
  46. }
  47. - (id)initWithURL:(NSURL *)url {
  48. self = [self initWithNibName:nil bundle:nil];
  49. if (self) {
  50. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  51. [self.webView loadRequest:request];
  52. }
  53. return self;
  54. }
  55. - (void)dealloc {
  56. // WKWebView's delegate is assigned so we need to clear it manually.
  57. #if !TARGET_OS_TV
  58. if (_webView.navigationDelegate == self) {
  59. _webView.navigationDelegate = nil;
  60. }
  61. #else
  62. if (_webView.delegate = self){
  63. _webView.delegate = nil;
  64. }
  65. #endif
  66. }
  67. - (void)viewDidLoad {
  68. [super viewDidLoad];
  69. [self.view addSubview:self.webView];
  70. self.webView.frame = self.view.bounds;
  71. self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  72. if (self.originalText.length > 0) {
  73. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Copy" style:UIBarButtonItemStylePlain target:self action:@selector(copyButtonTapped:)];
  74. }
  75. }
  76. - (void)copyButtonTapped:(id)sender {
  77. #if !TARGET_OS_TV
  78. [UIPasteboard.generalPasteboard setString:self.originalText];
  79. #endif
  80. }
  81. #pragma mark - UIWebView Delegate
  82. -(void) webViewDidStartLoad:(UIWebView *)webView {
  83. LOG_SELF;
  84. }
  85. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  86. FXLog(@"navtype: %lu", navigationType);
  87. FXLog(@"urL: %@", request.URL);
  88. FXLog(@"scheme: %@", request.URL.scheme);
  89. FXLog(@"navigationType: %lu", navigationType);
  90. if (navigationType == UIWebViewNavigationTypeOther){
  91. return YES;
  92. } else {
  93. FLEXWebViewController *webVC = [[[self class] alloc] initWithURL:[request URL]];
  94. webVC.title = [[request URL] absoluteString];
  95. [self.navigationController pushViewController:webVC animated:YES];
  96. return NO;//? maybe?
  97. }
  98. return YES;
  99. }
  100. -(void) webViewDidFinishLoad:(UIWebView *)webView {
  101. LOG_SELF;
  102. }
  103. #pragma mark - WKWebView Delegate
  104. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  105. WKNavigationActionPolicy policy = WKNavigationActionPolicyCancel;
  106. if (navigationAction.navigationType == WKNavigationTypeOther) {
  107. // Allow the initial load
  108. policy = WKNavigationActionPolicyAllow;
  109. } else {
  110. // For clicked links, push another web view controller onto the navigation stack so that hitting the back button works as expected.
  111. // Don't allow the current web view to handle the navigation.
  112. NSURLRequest *request = navigationAction.request;
  113. FLEXWebViewController *webVC = [[[self class] alloc] initWithURL:[request URL]];
  114. webVC.title = [[request URL] absoluteString];
  115. [self.navigationController pushViewController:webVC animated:YES];
  116. }
  117. decisionHandler(policy);
  118. }
  119. #pragma mark - Class Helpers
  120. + (BOOL)supportsPathExtension:(NSString *)extension {
  121. BOOL supported = NO;
  122. NSSet<NSString *> *supportedExtensions = [self webViewSupportedPathExtensions];
  123. if ([supportedExtensions containsObject:[extension lowercaseString]]) {
  124. supported = YES;
  125. }
  126. return supported;
  127. }
  128. + (NSSet<NSString *> *)webViewSupportedPathExtensions {
  129. static NSSet<NSString *> *pathExtensions = nil;
  130. static dispatch_once_t onceToken;
  131. dispatch_once(&onceToken, ^{
  132. // Note that this is not exhaustive, but all these extensions should work well in the web view.
  133. // See https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/CreatingContentforSafarioniPhone/CreatingContentforSafarioniPhone.html#//apple_ref/doc/uid/TP40006482-SW7
  134. pathExtensions = [NSSet<NSString *> setWithArray:@[@"jpg", @"jpeg", @"png", @"gif", @"pdf", @"svg", @"tiff", @"3gp", @"3gpp", @"3g2",
  135. @"3gp2", @"aiff", @"aif", @"aifc", @"cdda", @"amr", @"mp3", @"swa", @"mp4", @"mpeg",
  136. @"mpg", @"mp3", @"wav", @"bwf", @"m4a", @"m4b", @"m4p", @"mov", @"qt", @"mqv", @"m4v"]];
  137. });
  138. return pathExtensions;
  139. }
  140. @end