on master help sign in
Add runFile (for loading files from app bundle)
commitf03fff87bc4a68f706c7eb93c12259ea0687b1bb authorHamish Mackenzie <Hamish.K.Mackenzie@googlemail.com> dateMon 13 Feb 2017, 00:49 (10 years ago) parentbc07af6
4 files changed+54−5
jsaddle-wkwebview/cbits/WKWebView.m+14 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+15−1 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+24−3 jsaddle/jsaddle.cabal+1−1
jsaddle-wkwebview/cbits/WKWebView.m+14
@@ -53,3 +53,17 @@ void loadHTMLString(WKWebView *webView, const char * _Nonnull html) {
5353 [webView loadHTMLString:[NSString stringWithCString:html encoding:NSUTF8StringEncoding] baseURL:NULL];
5454}
5555
56+void loadBundleFile(WKWebView *webView, const char * _Nonnull file, const char * _Nonnull allowing) {
57+ NSBundle *main = [NSBundle mainBundle];
58+ if(main) {
59+ NSString *path = main.resourcePath;
60+ if(path) {
61+ NSURL *fileUrl = [NSURL fileURLWithPath:[NSString stringWithFormat: @"%@/%@", path, [NSString stringWithCString:file encoding:NSUTF8StringEncoding]]];
62+ NSURL *allowingUrl = [NSURL fileURLWithPath:[NSString stringWithFormat: @"%@/%@", path, [NSString stringWithCString:allowing encoding:NSUTF8StringEncoding]]];
63+ [webView loadFileURL:fileUrl allowingReadAccessToURL:allowingUrl];
64+ }
65+ }
66+}
67+
68+
69+
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+15−1
@@ -1,10 +1,13 @@
11module Language.Javascript.JSaddle.WKWebView
22 ( jsaddleMain
3+ , jsaddleMainFile
34 , WKWebView(..)
45 , run
6+ , runFile
57 ) where
68
7−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, WKWebView(..))
9+import Data.ByteString (ByteString)
10+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..))
811import System.Environment (getProgName)
912import Foreign.C.String (CString, withCString)
1013import Foreign.StablePtr (StablePtr, newStablePtr)
@@ -12,8 +15,19 @@ import Language.Javascript.JSaddle (JSM)
1215
1316foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ()) -> CString -> IO ()
1417
18+-- | Run JSaddle in a WKWebView
1519run :: JSM () -> IO ()
1620run f = do
1721 handler <- newStablePtr (jsaddleMain f)
1822 progName <- getProgName
1923 withCString progName $ runInWKWebView handler
24+
25+-- | Run JSaddle in a WKWebView first loading the specified file
26+-- from the mainBundle (relative to the resourcePath).
27+runFile :: ByteString -- ^ The file to navigate to.
28+ -> ByteString -- ^ The path to allow read access to.
29+ -> JSM () -> IO ()
30+runFile url allowing f = do
31+ handler <- newStablePtr (jsaddleMainFile url allowing f)
32+ progName <- getProgName
33+ withCString progName $ runInWKWebView handler
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+24−3
@@ -1,6 +1,7 @@
11{-# LANGUAGE ForeignFunctionInterface, OverloadedStrings #-}
22module Language.Javascript.JSaddle.WKWebView.Internal
33 ( jsaddleMain
4+ , jsaddleMainFile
45 , WKWebView(..)
56 ) where
67
@@ -9,6 +10,7 @@ import Control.Concurrent (forkIO)
910
1011import Data.Monoid ((<>))
1112import Data.ByteString (useAsCString, packCString)
13+import qualified Data.ByteString as BS (ByteString)
1214import Data.ByteString.Lazy (ByteString, toStrict, fromStrict)
1315import Data.Aeson (encode, decode)
1416
@@ -27,10 +29,32 @@ foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString ->
2729foreign export ccall withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
2830foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
2931foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
32+foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
3033foreign import ccall evaluateJavaScript :: WKWebView -> CString -> IO ()
3134
35+-- | Run JSaddle in WKWebView
3236jsaddleMain :: JSM () -> WKWebView -> IO ()
3337jsaddleMain f webView = do
38+ jsaddleMain' f webView
39+
40+ useAsCString (toStrict indexHtml) $ loadHTMLString webView
41+ useAsCString (toStrict jsaddleJs) $ evaluateJavaScript webView
42+
43+-- | Run JSaddle in a WKWebView first loading the specified file
44+-- from the mainBundle (relative to the resourcePath).
45+jsaddleMainFile :: BS.ByteString -- ^ The file to navigate to.
46+ -> BS.ByteString -- ^ The path to allow read access to.
47+ -> JSM () -> WKWebView -> IO ()
48+jsaddleMainFile url allowing f webView = do
49+ jsaddleMain' f webView
50+
51+ useAsCString url $ \u ->
52+ useAsCString allowing $ \a ->
53+ loadBundleFile webView u a
54+ useAsCString (toStrict jsaddleJs) $ evaluateJavaScript webView
55+
56+jsaddleMain' :: JSM () -> WKWebView -> IO ()
57+jsaddleMain' f webView = do
3458 (processResult, start) <- runJavaScript (\batch ->
3559 useAsCString (toStrict $ "runJSaddleBatch(" <> encode batch <> ");") $
3660 evaluateJavaScript webView)
@@ -40,9 +64,6 @@ jsaddleMain f webView = do
4064 resultHandler <- newStablePtr processResult
4165 addJSaddleHandler webView startHandler resultHandler
4266
43− useAsCString (toStrict indexHtml) $ loadHTMLString webView
44− useAsCString (toStrict jsaddleJs) $ evaluateJavaScript webView
45−
4667jsaddleStart :: StablePtr (IO ()) -> IO ()
4768jsaddleStart ptrHandler = join (deRefStablePtr ptrHandler)
4869
jsaddle/jsaddle.cabal+1−1
@@ -1,5 +1,5 @@
11name: jsaddle
2−version: 0.8.0.1
2+version: 0.8.1.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT