FLEXWebViewController.m 3.9 KB

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