2014-12-01 17 views
10

Tôi đang thử nghiệm WKWebView với tập tin địa phương, được làm việc trong mô phỏng nhưng nó không được làm việc trong thiết bịWKWebView với tập tin địa phương

@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]; 
} 

Tôi cho phép tất url từ:

NSURL *presentationFolder = [self.presentationController url]; 

là ok, bởi vì tôi đã thử nghiệm cùng một mã với UIWebview và hoạt động!

tôi luôn luôn nhận được lỗi tương tự:

Could not create a sandbox extension for '/' 

này đã không làm việc, tôi đoán nó sẽ làm việc trong Objective-C như trong nhanh chóng

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

Bất cứ ý tưởng sẽ được đánh giá cao , cảm ơn


Cập nhật 2-12-2014

tôi đã phát hiện này có thể là một lỗi trong iOS 8.1 và nó có thể được cố định trong 8,2

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

Tôi đã thử nghiệm di chuyển các tập tin vào thư mục tạm thời và tôi đã không nhận được bất kỳ lỗi nhưng webView chỉ trống.

Tôi đã thử nghiệm cùng một mã (thư mục tạm thời) với UIWebView và hoạt động tốt!

Ngoài ra, tôi đã thử điều này:

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

Như tôi có thể tìm ra, công trình này vì css và javascript được nhúng trong html.

+0

Tôi không nghĩ rằng đó là một lỗi, nó đã làm việc trên beta và ngừng làm việc trên trận chung kết 8.0 phát hành, táo biết nó và đã không sửa chữa nó chưa – jcesarmobile

+0

Vâng, đó tồi tệ hơn một lỗi :), mặc dù có thể nó đã được làm việc trong các bản beta vì mã css và javascript được embebed. Cảm ơn bạn đã bình luận – xarly

+0

BTW, có vẻ như bản beta 8.2 không thể tải các tệp cục bộ hoặc – jcesarmobile

Trả lời

4

Hãy thử XWebView có một máy chủ http được nhúng rất nhỏ. Nó nhỏ hơn nhiều so với GCDWebServer. Phương thức loadFileURL:allowingReadAccessToURL được thêm vào thông qua phần mở rộng, do đó bạn không biết máy chủ.

1

này làm việc như một nét duyên dáng ...

@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]; 
Các vấn đề liên quan