WebViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // WebViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 1/21/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "WebViewController.h"
  9. #import "RAUtils.h"
  10. #import "const.h"
  11. @interface WebViewController ()
  12. @end
  13. @implementation WebViewController
  14. - (void)viewWillAppear:(BOOL)animated
  15. {
  16. [super viewWillAppear:animated];
  17. [[self navigationController] setNavigationBarHidden:NO animated:NO];
  18. }
  19. - (void)viewDidLoad
  20. {
  21. [super viewDidLoad];
  22. // self.edgesForExtendedLayout = UIRectEdgeNone;
  23. // Do any additional setup after loading the view.
  24. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  25. ref.tag = 200 ;
  26. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"drag to refresh"];
  27. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  28. // ref.hidden = true;
  29. [ref addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  30. [self.wkWebview.scrollView addSubview:ref];
  31. self.wkWebview.navigationDelegate = self;
  32. // self.wkWebview.dataDetectorTypes=UIDataDetectorTypeAll;
  33. // self.wkWebview datade
  34. if(!_defaultBarbutton)
  35. {
  36. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  37. style:UIBarButtonItemStylePlain
  38. target:self
  39. action:@selector( onBackClick:)];
  40. //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  41. // closeButton.title = @"Close";
  42. // closeButton.tintColor = UIColorFromRGB(0x996633);
  43. self.navigationItem.leftBarButtonItem = closeButton;
  44. }
  45. // self.url=@"http://192.168.1.3:8080/npd/test.pdf";
  46. // NSURL* url = [NSURL URLWithString:self.url];
  47. //
  48. // NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:2000];
  49. //
  50. //
  51. // [self.webView loadRequest:request];
  52. [self loadcontent];
  53. }
  54. - (void)onBackClick:(UIButton *)sender {
  55. /*
  56. if (self.backItemDismiss)
  57. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  58. [self.navigationController popViewControllerAnimated:FALSE];*/
  59. //如果以下关闭有问题则回滚上面注释代码
  60. if (self.presentingViewController) {
  61. [self dismissViewControllerAnimated:YES completion:nil];
  62. } else {
  63. [self.navigationController popViewControllerAnimated:YES];
  64. }
  65. }
  66. -(void)ReloadData
  67. {
  68. // Count ++ ;
  69. // [mytabelview reloadData];
  70. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  71. [reF endRefreshing];
  72. [self loadcontent];
  73. }
  74. -(void) loadcontent
  75. {
  76. self.mum.hidden=false;
  77. NSURL *url =[NSURL URLWithString:self.url];
  78. DebugLog(self.url);
  79. NSURLRequest *request =[NSURLRequest requestWithURL:url];
  80. [self.wkWebview loadRequest:request];
  81. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  82. //
  83. // NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60*5];
  84. //
  85. // //设置请求方式为get
  86. //
  87. // [request setHTTPMethod:@"GET"];
  88. //
  89. // //添加用户会话id
  90. //
  91. // [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
  92. //
  93. // //连接发送请求
  94. //
  95. //
  96. // NSHTTPURLResponse* urlResponse = nil;
  97. //
  98. // NSError *error = [[NSError alloc] init];
  99. //
  100. // self.content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
  101. //
  102. //
  103. //
  104. // dispatch_async(dispatch_get_main_queue(), ^{
  105. //
  106. // self.navigationItem.title = self.title;
  107. // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  108. // // self.content=nil;
  109. // if(self.content!=nil&&self.content.length>0)
  110. // {
  111. // [reF removeFromSuperview];
  112. //
  113. //
  114. //// // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  115. //// NSString *path = NSTemporaryDirectory();
  116. //// NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  117. //// // NSError *error = [[NSError alloc] init];
  118. //// // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  119. //// // NSAssert(bo,@"创建目录失败");
  120. //// [self.content writeToFile:filePath atomically:YES];
  121. ////
  122. ////
  123. //// self.btnshare.enabled =true;
  124. ////
  125. ////
  126. //// self.btnemail.enabled =true;
  127. //// self.btnsave.enabled =true;
  128. ////
  129. //// if(self.onLoadSuccess)
  130. //// self.onLoadSuccess(self.url);
  131. // // int a=0;
  132. // }
  133. // else
  134. // {
  135. // [RAUtils message_alert:nil title:@"Open URL Failed" controller:self];
  136. // // self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding];
  137. // }
  138. //
  139. // ;
  140. // // DebugLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]);
  141. // //
  142. //
  143. //
  144. // [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
  145. //
  146. //
  147. //
  148. //
  149. // });
  150. // });
  151. }
  152. -(void)changeCell
  153. {
  154. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  155. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  156. if ([self respondsToSelector:@selector(ReloadData)])
  157. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  158. // DebugLog(@"refresh!!!!!!!!");
  159. }
  160. - (void)didReceiveMemoryWarning
  161. {
  162. [super didReceiveMemoryWarning];
  163. // Dispose of any resources that can be recreated.
  164. }
  165. #pragma mark - WKNavigationDelegate
  166. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  167. {
  168. self.mum.hidden=true;
  169. }
  170. /*
  171. #pragma mark - Navigation
  172. // In a storyboard-based application, you will often want to do a little preparation before navigation
  173. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  174. // Get the new view controller using [segue destinationViewController].
  175. // Pass the selected object to the new view controller.
  176. }
  177. */
  178. @end