UIWebView在iOS 8中显示空白屏幕

前端之家收集整理的这篇文章主要介绍了UIWebView在iOS 8中显示空白屏幕前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序,我可以下载一个pdf并存储在iPhone.它在iOS 7(我目前在iOS 8上运行)运行正常,而在iOS 8中运行应用程序时,webView只是保持空白.我不知道有什么问题.
  1. #import "ISJMMisalViewController.h"
  2. #import "SWRevealViewController.h"
  3.  
  4. @implementation ISJMMisalViewController
  5. @synthesize activityImageView;
  6.  
  7. - (void)viewWillAppear:(BOOL)animated {
  8. [super viewWillAppear:YES];
  9.  
  10. [self.webView addSubview:activityImageView];
  11. [activityImageView startAnimating];
  12.  
  13. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
  14.  
  15. NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
  16. NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"misalLocal.pdf"];
  17.  
  18. //Now create Request for the file that was saved in your documents folder
  19. NSURL *url = [NSURL fileURLWithPath:filePath];
  20. NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  21.  
  22. dispatch_async(dispatch_get_main_queue(),^{
  23.  
  24. [self.webView setUserInteractionEnabled:YES];
  25. [self.webView setDelegate:self];
  26. [self.webView loadRequest:requestObj];
  27. [self.webView setScalesPageToFit:YES];
  28. [activityImageView stopAnimating];
  29. [activityImageView removeFromSuperview];
  30.  
  31. });
  32. });
  33.  
  34. }
  35.  
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38.  
  39. [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
  40.  
  41. UIImage *image = [UIImage imageNamed: @"NavBarImage.png"];
  42. UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
  43.  
  44. self.navigationItem.titleView = imageView;
  45.  
  46. _sidebarButton.target = self.revealViewController;
  47. _sidebarButton.action = @selector(revealToggle:);
  48.  
  49. //Create the first status image and the indicator view
  50. UIImage *statusImage = [UIImage imageNamed:@"1.png"];
  51. activityImageView = [[UIImageView alloc] initWithImage:statusImage];
  52.  
  53.  
  54. //Add more images which will be used for the animation
  55. activityImageView.animationImages = [NSArray arrayWithObjects:
  56. [UIImage imageNamed:@"1.png"],[UIImage imageNamed:@"2.png"],[UIImage imageNamed:@"3.png"],[UIImage imageNamed:@"4.png"],[UIImage imageNamed:@"5.png"],[UIImage imageNamed:@"6.png"],[UIImage imageNamed:@"7.png"],[UIImage imageNamed:@"8.png"],[UIImage imageNamed:@"9.png"],nil];
  57.  
  58.  
  59. //Set the duration of the animation (play with it
  60. //until it looks nice for you)
  61. activityImageView.animationDuration = 0.7;
  62.  
  63.  
  64. //Position the activity image view somewhere in
  65. //the middle of your current view
  66. activityImageView.frame = CGRectMake(self.view.frame.size.width/2 - 50,self.view.frame.size.height/2 - 100,100,100);
  67.  
  68. UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleNavBar:)];
  69. [self. webView addGestureRecognizer:gesture];
  70.  
  71. if (!self.webView) {
  72.  
  73. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sin conexión"
  74. message:@"No fue posible la descarga de datos debido a que no se pudo conectar con el servidor"
  75. delegate: nil
  76. cancelButtonTitle: @"OK"
  77. otherButtonTitles: nil];
  78.  
  79. [alert show];
  80.  
  81. }
  82.  
  83.  
  84. }
  85.  
  86. - (IBAction)actualizar:(id)sender {
  87.  
  88. [self.webView addSubview:activityImageView];
  89. [activityImageView startAnimating];
  90.  
  91. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,^{
  92.  
  93. NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://isjm.weebly.com/uploads/2/5/3/6/25368636/misal.pdf"]];
  94. NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath]stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
  95. NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"misalLocal.pdf"];
  96. [pdfData writeToFile:filePath atomically:YES];
  97.  
  98. NSURL *url = [NSURL fileURLWithPath:filePath];
  99. NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  100.  
  101. dispatch_async(dispatch_get_main_queue(),^{
  102.  
  103. [self.webView setUserInteractionEnabled:YES];
  104. [self.webView setDelegate:self];
  105. [self.webView loadRequest:requestObj];
  106. [self.webView setScalesPageToFit:YES];
  107. [activityImageView stopAnimating];
  108. [activityImageView removeFromSuperview];
  109.  
  110. if (!self.webView) {
  111.  
  112. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sin conexión"
  113. message:@"No fue posible la descarga de datos debido a que no se pudo conectar con el servidor"
  114. delegate: nil
  115. cancelButtonTitle: @"OK"
  116. otherButtonTitles: nil];
  117.  
  118. [alert show];
  119. }
  120. });
  121. });
  122. }
  123.  
  124. - (void)toggleNavBar:(UITapGestureRecognizer *)gesture {
  125.  
  126. BOOL barsHidden = self.navigationController.navigationBar.hidden;
  127. [self.navigationController setNavigationBarHidden:!barsHidden animated:YES];
  128.  
  129. }
  130.  
  131. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  132. {
  133. return YES;
  134. }
  135.  
  136. @end

解决方法

这是一个iOS 8的错误!您可以使用以下方式解决
  1. NSString *string = [[NSBundle mainBundle] pathForResource:@"Low Cost Perimeter Build Guide" ofType:@"pdf"];
  2. NSURL *url = [NSURL fileURLWithPath:string];
  3. NSData *pdfData = [[NSData alloc] initWithContentsOfURL:url];
  4. [self.webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];

猜你在找的iOS相关文章