@@ -1,11 +1,15 @@
1module Language.Javascript.JSaddle.WKWebView
1module Language.Javascript.JSaddle.WKWebView
2 ( jsaddleMain
2 ( jsaddleMain
3+ , jsaddleMainHTMLWithBaseURL
3 , jsaddleMainFile
4 , jsaddleMainFile
4 , WKWebView(..)
5 , WKWebView(..)
6 , runWithAppConfig
8 , runWithAppConfig
8 , mainBundleResourcePath
11 , mainBundleResourcePath
12+ , ApplicationState (..)
9 , AppDelegateConfig (..)
13 , AppDelegateConfig (..)
10 , AppDelegateNotificationConfig (..)
14 , AppDelegateNotificationConfig (..)
11 , AuthorizationOption (..)
15 , AuthorizationOption (..)
@@ -17,9 +21,11 @@ import Data.Set (Set)
17import qualified Data.Set as Set
21import qualified Data.Set as Set
18import Data.Word
22import Data.Word
19import Foreign.Marshal.Utils (fromBool)
23import Foreign.Marshal.Utils (fromBool)
20−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
24+import Language.Javascript.JSaddle.WKWebView.Internal
25+ (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
21import System.Environment (getProgName)
26import System.Environment (getProgName)
22import Foreign.C.String (CString, withCString)
27import Foreign.C.String (CString, withCString)
28+import Foreign.C.Types (CBool, CInt)
23import Foreign.StablePtr (StablePtr, newStablePtr)
29import Foreign.StablePtr (StablePtr, newStablePtr)
24import Language.Javascript.JSaddle (JSM)
30import Language.Javascript.JSaddle (JSM)
@@ -35,6 +41,7 @@ foreign import ccall runInWKWebView
35 -> StablePtr (IO ()) -- applicationWillTerminate
41 -> StablePtr (IO ()) -- applicationWillTerminate
36 -> StablePtr (IO ()) -- applicationSignificantTimeChange
42 -> StablePtr (IO ()) -- applicationSignificantTimeChange
37 -> StablePtr (CString -> IO ()) -- applicationUniversalLink
43 -> StablePtr (CString -> IO ()) -- applicationUniversalLink
44+ -> StablePtr (CInt -> CString -> IO ()) -- applicationDidReceiveRemoteNotification
38 -> Word64 -- whether to run requestAuthorizationWithOptions
45 -> Word64 -- whether to run requestAuthorizationWithOptions
39 -> Word64 -- Ask for Badge authorization
46 -> Word64 -- Ask for Badge authorization
40 -> Word64 -- Ask for Sound authorization
47 -> Word64 -- Ask for Sound authorization
@@ -43,6 +50,8 @@ foreign import ccall runInWKWebView
43 -> Word64 -- registerForRemoteNotifications
50 -> Word64 -- registerForRemoteNotifications
44 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
51 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
45 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
52 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
53+ -> Word64 -- developerExtrasEnabled
54+ -> StablePtr (CString -> IO CBool) -- applicationOpenFile
48data AppDelegateConfig = AppDelegateConfig
57data AppDelegateConfig = AppDelegateConfig
@@ -55,7 +64,25 @@ data AppDelegateConfig = AppDelegateConfig
55 , _appDelegateConfig_applicationWillTerminate :: IO ()
64 , _appDelegateConfig_applicationWillTerminate :: IO ()
56 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
65 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
57 , _appDelegateConfig_applicationUniversalLink :: CString -> IO ()
66 , _appDelegateConfig_applicationUniversalLink :: CString -> IO ()
67+ -- ^ Called when the app is launched from a URL. 'CString' contains the URL.
69+ -- The Core Foundation plist key CFBundleURLTypes controls which URL schemes
70+ -- the app should respond to.
71+ , _appDelegateConfig_applicationDidReceiveRemoteNotification :: ApplicationState -> CString -> IO ()
72+ -- ^ Called when the application receives a remote notification.
73+ -- 'ApplicationState' is the current state of the app.
74+ -- The 'CString' JSON-encoded user info contained in the notification.
75+ -- For more information see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623117-application?language=objc
58 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
76 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
77+ , _appDelegateConfig_developerExtrasEnabled :: Bool
78+ -- ^ Allow devtools in the app. Defaults to 'True'
79+ , _appDelegateConfig_applicationOpenFile :: CString -> IO Bool
80+ -- ^ Called when the app is launched by opening a file. 'CString' contains
81+ -- the file path. Return value is 'True' if the file was successfully
82+ -- opened. Defaults to ignoring the file and returning 'False'.
84+ -- The Core Foundation plist key CFBundleDocumentTypes controls which file
85+ -- types should be associated with the app.
61instance Default AppDelegateConfig where
88instance Default AppDelegateConfig where
@@ -69,7 +96,10 @@ instance Default AppDelegateConfig where
69 , _appDelegateConfig_applicationWillTerminate = return ()
96 , _appDelegateConfig_applicationWillTerminate = return ()
70 , _appDelegateConfig_applicationSignificantTimeChange = return ()
97 , _appDelegateConfig_applicationSignificantTimeChange = return ()
71 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
98 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
99+ , _appDelegateConfig_applicationDidReceiveRemoteNotification = \_ _ -> return ()
72 , _appDelegateConfig_appDelegateNotificationConfig = def
100 , _appDelegateConfig_appDelegateNotificationConfig = def
101+ , _appDelegateConfig_developerExtrasEnabled = True
102+ , _appDelegateConfig_applicationOpenFile = \_ -> return False
75data AuthorizationOption = AuthorizationOption_Badge
105data AuthorizationOption = AuthorizationOption_Badge
@@ -78,6 +108,12 @@ data AuthorizationOption = AuthorizationOption_Badge
78 | AuthorizationOption_CarPlay
108 | AuthorizationOption_CarPlay
79 deriving (Show, Read, Eq, Ord)
109 deriving (Show, Read, Eq, Ord)
111+data ApplicationState = ApplicationState_Active
112+ | ApplicationState_Inactive
113+ | ApplicationState_Background
114+ | ApplicationState_Unknown CInt
115+ deriving (Show, Read, Eq, Ord)
81data AppDelegateNotificationConfig = AppDelegateNotificationConfig
117data AppDelegateNotificationConfig = AppDelegateNotificationConfig
82 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
118 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
83 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
119 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
@@ -96,29 +132,38 @@ instance Default AppDelegateNotificationConfig where
97-- | Run JSaddle in a WKWebView
133-- | Run JSaddle in a WKWebView
98run :: JSM () -> IO ()
134run :: JSM () -> IO ()
99−run = run' Nothing def
135+run f = run' def (jsaddleMain f)
101-- | Run JSaddle in a WKWebView
137-- | Run JSaddle in a WKWebView
102runWithAppConfig :: AppDelegateConfig -> JSM () -> IO ()
138runWithAppConfig :: AppDelegateConfig -> JSM () -> IO ()
103−runWithAppConfig = run' Nothing
139+runWithAppConfig cfg = run' cfg . jsaddleMain
105-- | Run JSaddle in a WKWebView first loading the specified file
141-- | Run JSaddle in a WKWebView first loading the specified file
106-- from the mainBundle (relative to the resourcePath).
142-- from the mainBundle (relative to the resourcePath).
107−runFile :: ByteString -- ^ The file to navigate to.
144+ :: ByteString -- ^ The file to navigate to.
108− -> ByteString -- ^ The path to allow read access to.
145+ -> ByteString -- ^ The path to allow read access to.
109− -> AppDelegateConfig
146+ -> AppDelegateConfig
110− -> JSM ()
147+ -> JSM ()
111− -> IO ()
148+ -> IO ()
112−runFile url allowing = run' $ Just (url, allowing)
149+runFile url allowing cfg = run' cfg . jsaddleMainFile url allowing
151+-- | Run JSaddle in a WKWebView first loading the specified html
152+-- as though it came from the specified URL
153+-- (calls WKWebKit function loadHTMLString).
114−run' :: Maybe (ByteString, ByteString)
155+ :: ByteString -- ^ HTML to load.
156+ -> ByteString -- ^ pretend it came from this URL.
157+ -> AppDelegateConfig
160+runHTMLWithBaseURL url allowing cfg = run' cfg . jsaddleMainHTMLWithBaseURL url allowing
115− -> AppDelegateConfig
162+run' :: AppDelegateConfig
116− -> JSM ()
163+ -> (WKWebView -> IO ())
118−run' mUrl cfg f = do
165+run' cfg main = do
119− handler <- case mUrl of
166+ handler <- newStablePtr main
120− Just (url, allowing) -> newStablePtr (jsaddleMainFile url allowing f)
121− Nothing -> newStablePtr (jsaddleMain f)
122 progName <- getProgName
167 progName <- getProgName
124 -- AppDelegate callbacks
169 -- AppDelegate callbacks
@@ -131,6 +176,14 @@ run' mUrl cfg f = do
131 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
176 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
132 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
177 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
133 applicationUniversalLink <- newStablePtr $ _appDelegateConfig_applicationUniversalLink cfg
178 applicationUniversalLink <- newStablePtr $ _appDelegateConfig_applicationUniversalLink cfg
179+ applicationDidReceiveRemoteNotification <- newStablePtr $ \n s -> do
180+ let appState = case n of
181+ 0 -> ApplicationState_Active
182+ 1 -> ApplicationState_Inactive
183+ 2 -> ApplicationState_Background
184+ _ -> ApplicationState_Unknown n
185+ _appDelegateConfig_applicationDidReceiveRemoteNotification cfg appState s
186+ applicationOpenFile <- newStablePtr $ fmap fromBool . _appDelegateConfig_applicationOpenFile cfg
135 -- AppDelegate notification configuration
188 -- AppDelegate notification configuration
136 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
189 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
@@ -153,6 +206,7 @@ run' mUrl cfg f = do
153 applicationWillTerminate
206 applicationWillTerminate
154 applicationSignificantTimeChange
207 applicationSignificantTimeChange
155 applicationUniversalLink
208 applicationUniversalLink
209+ applicationDidReceiveRemoteNotification
156 (fromBool requestAuthorizationWithOptions)
210 (fromBool requestAuthorizationWithOptions)
157 (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
211 (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
158 (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)
212 (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)
@@ -161,3 +215,5 @@ run' mUrl cfg f = do
161 (fromBool registerForRemoteNotifications)
215 (fromBool registerForRemoteNotifications)
162 didRegisterForRemoteNotificationsWithDeviceToken
216 didRegisterForRemoteNotificationsWithDeviceToken
163 didFailToRegisterForRemoteNotificationsWithError
217 didFailToRegisterForRemoteNotificationsWithError
218+ (fromBool $ _appDelegateConfig_developerExtrasEnabled cfg)