@@ -9,6 +9,7 @@ module Language.Javascript.JSaddle.WKWebView
9 , runHTMLWithBaseURL
9 , runHTMLWithBaseURL
11 , mainBundleResourcePath
11 , mainBundleResourcePath
12+ , ApplicationState (..)
12 , AppDelegateConfig (..)
13 , AppDelegateConfig (..)
13 , AppDelegateNotificationConfig (..)
14 , AppDelegateNotificationConfig (..)
14 , AuthorizationOption (..)
15 , AuthorizationOption (..)
@@ -24,6 +25,7 @@ import Language.Javascript.JSaddle.WKWebView.Internal
24 (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
25 (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
25import System.Environment (getProgName)
26import System.Environment (getProgName)
26import Foreign.C.String (CString, withCString)
27import Foreign.C.String (CString, withCString)
28+import Foreign.C.Types (CBool, CInt)
27import Foreign.StablePtr (StablePtr, newStablePtr)
29import Foreign.StablePtr (StablePtr, newStablePtr)
28import Language.Javascript.JSaddle (JSM)
30import Language.Javascript.JSaddle (JSM)
@@ -39,6 +41,7 @@ foreign import ccall runInWKWebView
39 -> StablePtr (IO ()) -- applicationWillTerminate
41 -> StablePtr (IO ()) -- applicationWillTerminate
40 -> StablePtr (IO ()) -- applicationSignificantTimeChange
42 -> StablePtr (IO ()) -- applicationSignificantTimeChange
41 -> StablePtr (CString -> IO ()) -- applicationUniversalLink
43 -> StablePtr (CString -> IO ()) -- applicationUniversalLink
44+ -> StablePtr (CInt -> CString -> IO ()) -- applicationDidReceiveRemoteNotification
42 -> Word64 -- whether to run requestAuthorizationWithOptions
45 -> Word64 -- whether to run requestAuthorizationWithOptions
43 -> Word64 -- Ask for Badge authorization
46 -> Word64 -- Ask for Badge authorization
44 -> Word64 -- Ask for Sound authorization
47 -> Word64 -- Ask for Sound authorization
@@ -47,7 +50,8 @@ foreign import ccall runInWKWebView
47 -> Word64 -- registerForRemoteNotifications
50 -> Word64 -- registerForRemoteNotifications
48 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
51 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
49 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
52 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
50− -> Word64 -- Allow developer tools
53+ -> Word64 -- developerExtrasEnabled
54+ -> StablePtr (CString -> IO CBool) -- applicationOpenFile
53data AppDelegateConfig = AppDelegateConfig
57data AppDelegateConfig = AppDelegateConfig
@@ -60,8 +64,25 @@ data AppDelegateConfig = AppDelegateConfig
60 , _appDelegateConfig_applicationWillTerminate :: IO ()
64 , _appDelegateConfig_applicationWillTerminate :: IO ()
61 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
65 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
62 , _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
63 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
76 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
64 , _appDelegateConfig_developerExtrasEnabled :: Bool
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.
67instance Default AppDelegateConfig where
88instance Default AppDelegateConfig where
@@ -75,8 +96,10 @@ instance Default AppDelegateConfig where
75 , _appDelegateConfig_applicationWillTerminate = return ()
96 , _appDelegateConfig_applicationWillTerminate = return ()
76 , _appDelegateConfig_applicationSignificantTimeChange = return ()
97 , _appDelegateConfig_applicationSignificantTimeChange = return ()
77 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
98 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
99+ , _appDelegateConfig_applicationDidReceiveRemoteNotification = \_ _ -> return ()
78 , _appDelegateConfig_appDelegateNotificationConfig = def
100 , _appDelegateConfig_appDelegateNotificationConfig = def
79 , _appDelegateConfig_developerExtrasEnabled = True
101 , _appDelegateConfig_developerExtrasEnabled = True
102+ , _appDelegateConfig_applicationOpenFile = \_ -> return False
82data AuthorizationOption = AuthorizationOption_Badge
105data AuthorizationOption = AuthorizationOption_Badge
@@ -85,6 +108,12 @@ data AuthorizationOption = AuthorizationOption_Badge
85 | AuthorizationOption_CarPlay
108 | AuthorizationOption_CarPlay
86 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)
88data AppDelegateNotificationConfig = AppDelegateNotificationConfig
117data AppDelegateNotificationConfig = AppDelegateNotificationConfig
89 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
118 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
90 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
119 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
@@ -147,6 +176,14 @@ run' cfg main = do
147 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
176 applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
148 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
177 applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
149 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
151 -- AppDelegate notification configuration
188 -- AppDelegate notification configuration
152 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
189 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
@@ -169,6 +206,7 @@ run' cfg main = do
169 applicationWillTerminate
206 applicationWillTerminate
170 applicationSignificantTimeChange
207 applicationSignificantTimeChange
171 applicationUniversalLink
208 applicationUniversalLink
209+ applicationDidReceiveRemoteNotification
172 (fromBool requestAuthorizationWithOptions)
210 (fromBool requestAuthorizationWithOptions)
173 (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
211 (fromBool $ Set.member AuthorizationOption_Badge authorizationOptions)
174 (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)
212 (fromBool $ Set.member AuthorizationOption_Sound authorizationOptions)
@@ -178,3 +216,4 @@ run' cfg main = do
178 didRegisterForRemoteNotificationsWithDeviceToken
216 didRegisterForRemoteNotificationsWithDeviceToken
179 didFailToRegisterForRemoteNotificationsWithError
217 didFailToRegisterForRemoteNotificationsWithError
180 (fromBool $ _appDelegateConfig_developerExtrasEnabled cfg)
218 (fromBool $ _appDelegateConfig_developerExtrasEnabled cfg)