on master help sign in
Provide a file:// url as a base url for wkwebview
f118d01Tim Emiola
jsaddle-wkwebview/cbits/WKWebView.m+8−10
@@ -61,9 +61,9 @@ extern void jsaddleSyncResult(HsStablePtr, JSaddleHandler *, const char * _Nonnu
61
61
62 if(!navigationAction.targetFrame && ![url.scheme isEqualToString:@"file"]) {
62 if(!navigationAction.targetFrame && ![url.scheme isEqualToString:@"file"]) {
63 if(openApp(url))
63 if(openApp(url))
64− decisionHandler(WKNavigationActionPolicyCancel);
64+ decisionHandler(WKNavigationActionPolicyCancel);
65− else
65+ else
66− decisionHandler(WKNavigationActionPolicyAllow);
66+ decisionHandler(WKNavigationActionPolicyAllow);
67 }
67 }
68 else
68 else
69 decisionHandler(WKNavigationActionPolicyAllow);
69 decisionHandler(WKNavigationActionPolicyAllow);
@@ -95,10 +95,11 @@ void completeSync(JSaddleHandler *handler, const char * _Nonnull s) {
95 handler.completionHandler = NULL;
95 handler.completionHandler = NULL;
96}
96}
97
97
98−void loadHTMLString(WKWebView *webView, const char * _Nonnull html) {
98+void loadHTMLStringWithBaseURL(WKWebView *webView, const char * _Nonnull html, const char * _Nonnull url) {
99− NSString *htmlString = [NSString stringWithCString:html encoding:NSUTF8StringEncoding];
99+ NSString *htmlString = [NSString stringWithCString:html encoding:NSUTF8StringEncoding];
100+ NSURL *baseURL = [NSURL fileURLWithPath:[[NSString alloc] initWithCString:url encoding:NSUTF8StringEncoding]];
100− dispatch_async(dispatch_get_main_queue(), ^{
101+ dispatch_async(dispatch_get_main_queue(), ^{
101− [webView loadHTMLString:htmlString baseURL:NULL];
102+ [webView loadHTMLString:htmlString baseURL:baseURL];
102 });
103 });
103}
104}
104
105
@@ -121,6 +122,3 @@ void loadBundleFile(WKWebView *webView, const char * _Nonnull file, const char *
121 }
122 }
122 }
123 }
123}
124}
124−
125−
126−
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+8−3
@@ -34,7 +34,7 @@ foreign export ccall callWithWebView :: WKWebView -> StablePtr (WKWebView -> IO
34foreign export ccall callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
34foreign export ccall callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
35foreign export ccall callIO :: StablePtr (IO ()) -> IO ()
35foreign export ccall callIO :: StablePtr (IO ()) -> IO ()
36foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> StablePtr (Results -> IO Batch) -> IO ()
36foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> StablePtr (Results -> IO Batch) -> IO ()
37−foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
37+foreign import ccall loadHTMLStringWithBaseURL :: WKWebView -> CString -> CString -> IO ()
38foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
38foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
39foreign import ccall evaluateJavaScript :: WKWebView -> CString -> IO ()
39foreign import ccall evaluateJavaScript :: WKWebView -> CString -> IO ()
40foreign import ccall completeSync :: JSaddleHandler -> CString -> IO ()
40foreign import ccall completeSync :: JSaddleHandler -> CString -> IO ()
@@ -44,7 +44,9 @@ foreign import ccall mainBundleResourcePathC :: IO CString
44jsaddleMain :: JSM () -> WKWebView -> IO ()
44jsaddleMain :: JSM () -> WKWebView -> IO ()
45jsaddleMain f webView =
45jsaddleMain f webView =
46 jsaddleMain' f webView $
46 jsaddleMain' f webView $
47− useAsCString (toStrict indexHtml) $ loadHTMLString webView
47+ useAsCString (toStrict indexHtmlBaseURL) $ \url ->
48+ useAsCString (toStrict indexHtml) $ \html ->
49+ loadHTMLStringWithBaseURL webView html url
48
50
49-- | Run JSaddle in a WKWebView first loading the specified file
51-- | Run JSaddle in a WKWebView first loading the specified file
50-- from the mainBundle (relative to the resourcePath).
52-- from the mainBundle (relative to the resourcePath).
@@ -123,6 +125,10 @@ jsaddleJs = ghcjsHelpers <> "\
123 \})();\n\
125 \})();\n\
124 \"
126 \"
125
127
128+
129+indexHtmlBaseURL :: ByteString
130+indexHtmlBaseURL = "file:///tmp/index.html"
131+
126indexHtml :: ByteString
132indexHtml :: ByteString
127indexHtml =
133indexHtml =
128 "<!DOCTYPE html>\n\
134 "<!DOCTYPE html>\n\
@@ -140,4 +146,3 @@ mainBundleResourcePath = do
140 if bs == nullPtr
146 if bs == nullPtr
141 then return Nothing
147 then return Nothing
142 else Just <$> packCString bs
148 else Just <$> packCString bs
143−