FLEXWebViewController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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:@"<pre>%@</pre>", [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)viewDidLoad
  46. {
  47. [super viewDidLoad];
  48. [self.view addSubview:self.webView];
  49. self.webView.frame = self.view.bounds;
  50. self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  51. if ([self.originalText length] > 0) {
  52. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Copy" style:UIBarButtonItemStylePlain target:self action:@selector(copyButtonTapped:)];
  53. }
  54. }
  55. - (void)copyButtonTapped:(id)sender
  56. {
  57. [[UIPasteboard generalPasteboard] setString:self.originalText];
  58. }
  59. #pragma mark - UIWebView Delegate
  60. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
  61. {
  62. BOOL shouldStart = NO;
  63. if (navigationType == UIWebViewNavigationTypeOther) {
  64. // Allow the initial load
  65. shouldStart = YES;
  66. } else {
  67. // For clicked links, push another web view controller onto the navigation stack so that hitting the back button works as expected.
  68. // Don't allow the current web view do handle the navigation.
  69. FLEXWebViewController *webVC = [[[self class] alloc] initWithURL:[request URL]];
  70. [self.navigationController pushViewController:webVC animated:YES];
  71. }
  72. return shouldStart;
  73. }
  74. #pragma mark - Class Helpers
  75. + (BOOL)supportsPathExtension:(NSString *)extension
  76. {
  77. BOOL supported = NO;
  78. NSSet *supportedExtensions = [self webViewSupportedPathExtensions];
  79. if ([supportedExtensions containsObject:[extension lowercaseString]]) {
  80. supported = YES;
  81. }
  82. return supported;
  83. }
  84. + (NSSet *)webViewSupportedPathExtensions
  85. {
  86. static NSSet *pathExtenstions = nil;
  87. static dispatch_once_t onceToken;
  88. dispatch_once(&onceToken, ^{
  89. // Note that this is not exhaustive, but all these extensions should work well in the web view.
  90. // See https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/CreatingContentforSafarioniPhone/CreatingContentforSafarioniPhone.html#//apple_ref/doc/uid/TP40006482-SW7
  91. pathExtenstions = [NSSet setWithArray:@[@"jpg", @"jpeg", @"png", @"gif", @"pdf", @"svg", @"tiff", @"3gp", @"3gpp", @"3g2",
  92. @"3gp2", @"aiff", @"aif", @"aifc", @"cdda", @"amr", @"mp3", @"swa", @"mp4", @"mpeg",
  93. @"mpg", @"mp3", @"wav", @"bwf", @"m4a", @"m4b", @"m4p", @"mov", @"qt", @"mqv", @"m4v"]];
  94. });
  95. return pathExtenstions;
  96. }
  97. @end