on master help sign in
Provide a file:// url as a base url for wkwebview

This should mean that users on Mac will no longer need to generate an empty index.html to run the app.

commitf118d01249a93ba0fdd3a3a5d0ee3cc20553a190 authorTim Emiola <adetokunbo@users.noreply.github.com> dateFri 13 Apr 2018, 13:57 (9 years ago) parent8f32ae4
2 files changed+16−13
jsaddle-wkwebview/cbits/WKWebView.m+8−10 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+8−3
jsaddle-wkwebview/cbits/WKWebView.m+8−10
@@ -61,9 +61,9 @@ extern void jsaddleSyncResult(HsStablePtr, JSaddleHandler *, const char * _Nonnu
6161
6262 if(!navigationAction.targetFrame && ![url.scheme isEqualToString:@"file"]) {
6363 if(openApp(url))
64− decisionHandler(WKNavigationActionPolicyCancel);
65− else
66− decisionHandler(WKNavigationActionPolicyAllow);
64+ decisionHandler(WKNavigationActionPolicyCancel);
65+ else
66+ decisionHandler(WKNavigationActionPolicyAllow);
6767 }
6868 else
6969 decisionHandler(WKNavigationActionPolicyAllow);
@@ -95,10 +95,11 @@ void completeSync(JSaddleHandler *handler, const char * _Nonnull s) {
9595 handler.completionHandler = NULL;
9696}
9797
98−void loadHTMLString(WKWebView *webView, const char * _Nonnull html) {
99− NSString *htmlString = [NSString stringWithCString:html encoding:NSUTF8StringEncoding];
100− dispatch_async(dispatch_get_main_queue(), ^{
101− [webView loadHTMLString:htmlString baseURL:NULL];
98+void loadHTMLStringWithBaseURL(WKWebView *webView, const char * _Nonnull html, const char * _Nonnull url) {
99+ NSString *htmlString = [NSString stringWithCString:html encoding:NSUTF8StringEncoding];
100+ NSURL *baseURL = [NSURL fileURLWithPath:[[NSString alloc] initWithCString:url encoding:NSUTF8StringEncoding]];
101+ dispatch_async(dispatch_get_main_queue(), ^{
102+ [webView loadHTMLString:htmlString baseURL:baseURL];
102103 });
103104}
104105
@@ -121,6 +122,3 @@ void loadBundleFile(WKWebView *webView, const char * _Nonnull file, const char *
121122 }
122123 }
123124}
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
3434foreign export ccall callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
3535foreign export ccall callIO :: StablePtr (IO ()) -> IO ()
3636foreign 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 ()
3838foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
3939foreign import ccall evaluateJavaScript :: WKWebView -> CString -> IO ()
4040foreign import ccall completeSync :: JSaddleHandler -> CString -> IO ()
@@ -44,7 +44,9 @@ foreign import ccall mainBundleResourcePathC :: IO CString
4444jsaddleMain :: JSM () -> WKWebView -> IO ()
4545jsaddleMain f webView =
4646 jsaddleMain' f webView $
47− useAsCString (toStrict indexHtml) $ loadHTMLString webView
47+ useAsCString (toStrict indexHtmlBaseURL) $ \url ->
48+ useAsCString (toStrict indexHtml) $ \html ->
49+ loadHTMLStringWithBaseURL webView html url
4850
4951-- | Run JSaddle in a WKWebView first loading the specified file
5052-- from the mainBundle (relative to the resourcePath).
@@ -123,6 +125,10 @@ jsaddleJs = ghcjsHelpers <> "\
123125 \})();\n\
124126 \"
125127
128+
129+indexHtmlBaseURL :: ByteString
130+indexHtmlBaseURL = "file:///tmp/index.html"
131+
126132indexHtml :: ByteString
127133indexHtml =
128134 "<!DOCTYPE html>\n\
@@ -140,4 +146,3 @@ mainBundleResourcePath = do
140146 if bs == nullPtr
141147 then return Nothing
142148 else Just <$> packCString bs
143−