DetailCellWeb.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // DetailCellWeb.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 14-3-7.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "DetailCellWeb.h"
  9. @implementation DetailCellWeb
  10. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  11. {
  12. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  13. if (self) {
  14. // Initialization code
  15. }
  16. return self;
  17. }
  18. - (id)init
  19. {
  20. self = [super init];
  21. if(self)
  22. {
  23. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  24. WKPreferences *preferences = [WKPreferences new];
  25. preferences.javaScriptCanOpenWindowsAutomatically = YES;
  26. // preferences.minimumFontSize = 40.0;
  27. configuration.preferences = preferences;
  28. self.webView = [[WKWebView alloc] initWithFrame:self.bounds configuration:configuration];
  29. // self.webView.UIDelegate = self;
  30. // self.wkwebView.navigationDelegate = self;
  31. self.webView.scrollView.bounces = false;
  32. [self addSubview:self.webView];
  33. // __weak typeof(self) weakSelf = self;
  34. // [self.webView.scrollView setEmptyContentViewBackgroupColor:UIColor.whiteColor];
  35. // self.webView.scrollView.emptyView = [RAEmptyView emptyViewWithTitle:@"can not reach the server.\ntap to reload" clickHandler:^(id sender) {
  36. // [weakSelf LoadFromURL:weakSelf.url];
  37. // }];
  38. }
  39. return self;
  40. }
  41. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  42. {
  43. [super setSelected:selected animated:animated];
  44. // Configure the view for the selected state
  45. }
  46. - (void)willMoveToSuperview:(UIView *)newSuperview {
  47. // NSString* source = @"var meta = document.createElement('meta');meta.name = 'viewport';meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';var head = document.getElementsByTagName('head')[0];head.appendChild(meta);";
  48. // WKUserScript * script= [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:true];
  49. //// WKUserContentController* cc = [[WKUserContentController alloc] init];
  50. // [self.webView.configuration.userContentController addUserScript:script];
  51. // let userContentController: WKUserContentController = WKUserContentController()
  52. // let conf = WKWebViewConfiguration()
  53. // conf.userContentController = userContentController
  54. // userContentController.addUserScript(script)
  55. if (newSuperview) {
  56. NSString* source = @"var meta = document.createElement('meta');meta.name = 'viewport';meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0 user-scalable=no';var head = document.getElementsByTagName('head')[0];head.appendChild(meta);";
  57. WKUserScript * script= [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:true];
  58. [self.webView.configuration.userContentController addUserScript:script];
  59. NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
  60. [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:options context:nil];
  61. } else {
  62. [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
  63. }
  64. }
  65. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  66. if ([keyPath isEqualToString:@"contentSize"]) {
  67. // NSLog(@"contentSize");
  68. // CGSize old = [change[@"old"] CGSizeValue];
  69. CGSize new = [change[@"new"] CGSizeValue];
  70. if (new.height != 0) {
  71. self.webView.scrollView.contentSize = CGSizeMake(new.width, 0);
  72. }
  73. }
  74. }
  75. - (void)prepareForReuse {
  76. [self.webView stopLoading];
  77. [super prepareForReuse];
  78. }
  79. @end