2014-12-01 98 views
10

我測試與本地文件WKWebView,這是在模擬器上工作,但它不是在設備工作WKWebView與本地文件

@interface EDPresentationViewController()<WKNavigationDelegate,WKScriptMessageHandler> 


    @property(nonatomic,strong)WKWebView *webView; 

    @property(nonatomic,strong)EDPresentationController *presentationController; 

@end 


@implementation EDPresentationViewController 

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.presentationController = [[EDPresentationController alloc]init]; 

    WKWebViewConfiguration *webConfiguration = [[WKWebViewConfiguration alloc]init]; 
    self.webView = [[WKWebView alloc]initWithFrame:self.view.frame configuration:webConfiguration]; 

    NSURL *presentationFolder = [self.presentationController url]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:presentationFolder]; 

    [self.webView loadRequest:request]; 
} 

我授予的URL來源:

NSURL *presentationFolder = [self.presentationController url]; 

是好的,因爲我用UIWebview測試了相同的代碼並且工作正常!

我總是得到同樣的錯誤:

Could not create a sandbox extension for '/' 

這是不行的,我想這將在Objective-C的工作在迅速

iOS Webkit not working on device, but works on simulator at swift

任何想法可以理解的,謝謝


更新2-12-2014

我發現這可能是一個錯誤的的iOS 8.1,它可以固定在8.2

https://devforums.apple.com/thread/247777?start=25&tstart=0

我測試過的文件移動到臨時文件夾,我沒有得到任何錯誤,但webView只是空的。

我用UIWebView測試了相同的代碼(臨時文件夾),並且工作正常!

而且,我已經試過這樣:

https://stackoverflow.com/a/26054170/426180

我能找出來,這個工程因爲CSS和JavaScript的HTML被embebed。

+0

我不認爲這是一個錯誤,這是工作的測試版,並停止工作的最後8.0發佈,蘋果知道它,但還沒有修復它 – jcesarmobile 2014-12-02 11:13:40

+0

那麼,這比錯誤:),雖然也許它是在測試版中工作,因爲CSS和JavaScript代碼被嵌入。感謝您的評論 – xarly 2014-12-02 11:35:31

+0

順便說一句,似乎8.2測試版本無法加載本地文件 – jcesarmobile 2014-12-02 11:53:29

回答

1

這工作就像一個魅力...

@interface ViewController() <WKScriptMessageHandler, WKNavigationDelegate> 

...

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; 
WKUserContentController *controller = [[WKUserContentController alloc] init]; 

configuration.userContentController = controller; 
[configuration.preferences setValue:@"TRUE" forKey:@"allowFileAccessFromFileURLs"]; 
self.webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds configuration:configuration]; 
self.webView.navigationDelegate = self; 
// Also if you'd have bouncing problem 
self.webView.scrollView.bounces = false; 
self.webView.scrollView.alwaysBounceVertical = false; 
[self.view addSubview:self.webView]; 

NSString* productURL = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"htmlapp/home.html"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:productURL]]; 
[self.webView loadRequest:request];