on master help sign in
Add didFailToRegisterForRemoteNotificationsWithError to AppDelegateConfig
commit9dbfcb14c12563ded8c57dac11ed21e95e5a71aa authorAli Abrar <aliabrar@gmail.com> dateMon 8 May 2017, 13:34 (9 years ago) parent1af340d
2 files changed+34−20
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+6−2 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+28−18
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+6−2
@@ -16,6 +16,7 @@ BOOL global_requestAuthorizationOptionAlert = NO;
1616BOOL global_requestAuthorizationOptionCarPlay = NO;
1717BOOL global_registerForRemoteNotifications = NO;
1818HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
19+HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
1920
2021@implementation AppDelegate
2122
@@ -83,7 +84,8 @@ HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
8384}
8485
8586- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
86− // Called when remote notification registration failed
87+ NSString *errorString = [error localizedDescription];
88+ handlerCString([errorString UTF8String], global_didFailToRegisterForRemoteNotificationsWithError);
8789}
8890
8991@end
@@ -96,7 +98,8 @@ void runInWKWebView(HsStablePtr handler,
9698 const BOOL hs_requestAuthorizationOptionAlert,
9799 const BOOL hs_requestAuthorizationOptionCarPlay,
98100 const BOOL hs_registerForRemoteNotifications,
99− HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken) {
101+ HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken,
102+ HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError) {
100103 @autoreleasepool {
101104 globalHandler = handler;
102105 global_requestAuthorizationWithOptions = hs_requestAuthorizationWithOptions;
@@ -106,6 +109,7 @@ void runInWKWebView(HsStablePtr handler,
106109 global_requestAuthorizationOptionCarPlay = hs_requestAuthorizationOptionCarPlay;
107110 global_registerForRemoteNotifications = hs_registerForRemoteNotifications;
108111 global_didRegisterForRemoteNotificationsWithDeviceToken = hs_didRegisterForRemoteNotificationsWithDeviceToken;
112+ global_didFailToRegisterForRemoteNotificationsWithError = hs_didFailToRegisterForRemoteNotificationsWithError;
109113 const char * _Nonnull argv [] = {"", 0};
110114 UIApplicationMain(0, argv, nil, NSStringFromClass([AppDelegate class]));
111115 }
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+28−18
@@ -21,16 +21,18 @@ import Foreign.C.Types
2121import Foreign.StablePtr (StablePtr, newStablePtr)
2222import Language.Javascript.JSaddle (JSM)
2323
24−foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ())
25− -> CString
26− -> CChar -- whether to run requestAuthorizationWithOptions
27− -> CChar -- Ask for Badge authorization
28− -> CChar -- Ask for Sound authorization
29− -> CChar -- Ask for Alert authorization
30− -> CChar -- Ask for CarPlay authorization
31− -> CChar -- registerForRemoteNotifications
32− -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
33− -> IO ()
24+foreign import ccall runInWKWebView
25+ :: StablePtr (WKWebView -> IO ())
26+ -> CString
27+ -> CChar -- whether to run requestAuthorizationWithOptions
28+ -> CChar -- Ask for Badge authorization
29+ -> CChar -- Ask for Sound authorization
30+ -> CChar -- Ask for Alert authorization
31+ -> CChar -- Ask for CarPlay authorization
32+ -> CChar -- registerForRemoteNotifications
33+ -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
34+ -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
35+ -> IO ()
3436
3537
3638data AppDelegateConfig = AppDelegateConfig
@@ -52,6 +54,7 @@ data AppDelegateNotificationConfig = AppDelegateNotificationConfig
5254 { _appDelegateNotificationConfig_requestAuthorizationWithOptions :: Maybe (Set AuthorizationOption)
5355 , _appDelegateNotificationConfig_registerForRemoteNotifications :: Bool
5456 , _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken :: CString -> IO ()
57+ , _appDelegateNotificationConfig_didFailToRegisterForRemoteNotificationsWithError :: CString -> IO ()
5558 }
5659
5760instance Default AppDelegateNotificationConfig where
@@ -59,16 +62,13 @@ instance Default AppDelegateNotificationConfig where
5962 { _appDelegateNotificationConfig_requestAuthorizationWithOptions = Nothing
6063 , _appDelegateNotificationConfig_registerForRemoteNotifications = False
6164 , _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken = \_ -> return ()
65+ , _appDelegateNotificationConfig_didFailToRegisterForRemoteNotificationsWithError = \_ -> return ()
6266 }
6367
6468
6569-- | Run JSaddle in a WKWebView
66−run :: (CString -> IO ()) -> JSM () -> IO ()
67−run didRegisterForRemoteNotificationsWithDeviceToken f = do
68− handler <- newStablePtr (jsaddleMain f)
69− progName <- getProgName
70− deviceTokenHandler <- newStablePtr didRegisterForRemoteNotificationsWithDeviceToken
71− withCString progName $ \pn -> runInWKWebView handler pn 1 1 1 1 1 1 deviceTokenHandler
70+run :: AppDelegateConfig -> JSM () -> IO ()
71+run = run' Nothing
7272
7373-- | Run JSaddle in a WKWebView first loading the specified file
7474-- from the mainBundle (relative to the resourcePath).
@@ -77,8 +77,16 @@ runFile :: ByteString -- ^ The file to navigate to.
7777 -> AppDelegateConfig
7878 -> JSM ()
7979 -> IO ()
80−runFile url allowing cfg f = do
81− handler <- newStablePtr (jsaddleMainFile url allowing f)
80+runFile url allowing = run' $ Just (url, allowing)
81+
82+run' :: Maybe (ByteString, ByteString)
83+ -> AppDelegateConfig
84+ -> JSM ()
85+ -> IO ()
86+run' mUrl cfg f = do
87+ handler <- case mUrl of
88+ Just (url, allowing) -> newStablePtr (jsaddleMainFile url allowing f)
89+ Nothing -> newStablePtr (jsaddleMain f)
8290 progName <- getProgName
8391 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
8492 (requestAuthorizationWithOptions, authorizationOptions) =
@@ -87,6 +95,7 @@ runFile url allowing cfg f = do
8795 Just opts -> (True, opts)
8896 registerForRemoteNotifications = _appDelegateNotificationConfig_registerForRemoteNotifications ncfg
8997 didRegisterForRemoteNotificationsWithDeviceToken <- newStablePtr $ _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken ncfg
98+ didFailToRegisterForRemoteNotificationsWithError <- newStablePtr $ _appDelegateNotificationConfig_didFailToRegisterForRemoteNotificationsWithError ncfg
9099 withCString progName $ \pn -> runInWKWebView
91100 handler
92101 pn
@@ -97,3 +106,4 @@ runFile url allowing cfg f = do
97106 (boolToCChar $ Set.member AuthorizationOption_CarPlay authorizationOptions)
98107 (boolToCChar registerForRemoteNotifications)
99108 didRegisterForRemoteNotificationsWithDeviceToken
109+ didFailToRegisterForRemoteNotificationsWithError