You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.2 KiB

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