2016-01-05 43 views
0

我無法在wkwebview中加載本地epub文件(來自文檔目錄)。它在模擬器中工作,但不在設備中。我知道這是來自here的iOS 8的一個bug。它已經解決了iOS 8?請幫我,我該怎麼做。我的設備得到了錯誤的 -在WKWebView中加載本地文件在設備中不起作用

The operation couldn't be completed. (KCFErrorDomainCFNetwork error 1.) 

這裏是代碼片段 -

-(void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 

    [self.webView removeObserver:self forKeyPath:@"loading"]; 
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"]; 
} 


-(void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    [self.webView addObserver:self forKeyPath:@"loading" options:NSKeyValueObservingOptionNew context:nil]; 
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil]; 
    [self.progressView setProgress:0.0f animated:NO]; 

    if ([self.documentDirectory checkEpubFileIfExistsAtPath:self.epubFolder]) { 

     NSString *filePath = [NSString stringWithFormat:@"%@/%@/%@/OEBPS/%@", [self.documentDirectory getDocumentsDirectory], self.epubFolder, self.epubName, [_html_link substringToIndex:[_html_link rangeOfString:@"#"].location]]; 

     //Loading webview with progress bar action 
     if ([_html_link rangeOfString:@"#"].location != NSNotFound) { 

      self.tag = [_html_link substringFromIndex:[_html_link rangeOfString:@"#"].location]; 
      NSURL *URL = [NSURL fileURLWithPath:filePath]; 
      NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
      [self.webView loadRequest:request]; 
     } 

    } else { 

     NSDictionary *object = [self.alertMessages getMessageObj:@"translationNotAvailable"]; 
     [self presentViewController:[self.alertController alertWithCustomOkayAction:[object objectForKey:@"title"] message:[object objectForKey:@"msg"] callback:^(void) { 

      [self dismissViewControllerAnimated:YES completion:nil]; 

     }] animated:YES completion:nil]; 
    } 
} 

//Constraints for Web View 
- (void) setConstraintsForWebView { 
    [self.webView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    WKWebView *webView = self.webView; 
    UIProgressView *progressView = self.progressView; 
    UIToolbar *toolBar = self.toolBar; 

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[webView]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(webView)]]; 

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[progressView]-0-[webView]-[toolBar]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(progressView, webView, toolBar)]]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //Do any additional setup after loading the view. 
    self.alertController = [AlertController new]; 
    self.alertMessages = [AlertMessages new]; 
    self.documentDirectory = [DocumentDirectory new]; 
    self.languageController = [LanguageController new]; 

    //Set observer for webview load 
    self.webView = [[WKWebView alloc] initWithFrame:CGRectZero]; 
    self.webView.navigationDelegate = self; 
    [self.view insertSubview:self.webView belowSubview:self.progressView]; 
    [self setConstraintsForWebView]; 

} 

#pragma mark KVO 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    if ([keyPath isEqualToString:@"loading"]) { 
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:self.webView.loading]; 
    } else if ([keyPath isEqualToString:@"estimatedProgress"]) { 
     self.progressView.hidden = self.webView.estimatedProgress == 1; 
     self.progressView.progress = self.webView.estimatedProgress; 
    } 
} 


#pragma mark Web view navigation delegate 

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { 
    [self.progressView setProgress:0.0f animated:NO]; 
    if (self.tag) { 
     [self.webView evaluateJavaScript:[NSString stringWithFormat:@"window.location.hash='%@'", self.tag] completionHandler:nil]; 
    } 
} 

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error { 

    [self presentViewController:[self.alertController alertWithAction:@"Error!" message:error.localizedDescription] animated:YES completion:nil]; 
} 

回答

相關問題