WebViewController.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // WebViewController.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 14-3-13.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "WebViewController.h"
  9. @interface WebViewController ()
  10. @end
  11. @implementation WebViewController
  12. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  13. {
  14. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  15. if (self) {
  16. // Custom initialization
  17. }
  18. return self;
  19. }
  20. -(void)ReloadData
  21. {
  22. // Count ++ ;
  23. // [mytabelview reloadData];
  24. [self loadcontent];
  25. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  26. [reF endRefreshing];
  27. }
  28. -(void) loadcontent
  29. {
  30. self.mum.hidden=false;
  31. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  32. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
  33. //设置请求方式为get
  34. [request setHTTPMethod:@"GET"];
  35. //添加用户会话id
  36. [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
  37. //连接发送请求
  38. NSHTTPURLResponse* urlResponse = nil;
  39. NSError *error = [[NSError alloc] init];
  40. self.content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
  41. self.filename = [urlResponse suggestedFilename];
  42. dispatch_async(dispatch_get_main_queue(), ^{
  43. self.navigationItem.title = self.filename;
  44. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  45. if(self.content!=nil)
  46. [reF removeFromSuperview];
  47. //
  48. [self.webview loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
  49. });
  50. });
  51. }
  52. -(void)changeCell
  53. {
  54. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  55. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  56. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  57. DebugLog(@"refresh!!!!!!!!");
  58. }
  59. - (void)viewDidLoad
  60. {
  61. [super viewDidLoad];
  62. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  63. ref.tag = 200 ;
  64. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"drag to refresh"];
  65. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  66. // ref.hidden = true;
  67. [ref addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  68. [self.webview.scrollView addSubview:ref];
  69. [self loadcontent];
  70. // Do any additional setup after loading the view.
  71. }
  72. - (void)didReceiveMemoryWarning
  73. {
  74. [super didReceiveMemoryWarning];
  75. // Dispose of any resources that can be recreated.
  76. }
  77. - (IBAction)onSaveClick:(UIBarButtonItem *)sender {
  78. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  79. NSString *path = [paths objectAtIndex:0];
  80. NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  81. // NSError *error = [[NSError alloc] init];
  82. // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  83. // NSAssert(bo,@"创建目录失败");
  84. [self.content writeToFile:filePath atomically:YES];
  85. sender.enabled = false;
  86. // self.navigationItem.rightBarButtonItem = nil;
  87. }
  88. #pragma mark - WKNavigationDelegate
  89. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  90. {
  91. NSLog(@"webview didFinishNavigation");
  92. // [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  93. self.mum.hidden=YES;
  94. }
  95. /*
  96. #pragma mark - Navigation
  97. // In a storyboard-based application, you will often want to do a little preparation before navigation
  98. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  99. {
  100. // Get the new view controller using [segue destinationViewController].
  101. // Pass the selected object to the new view controller.
  102. }
  103. */
  104. @end