on master help sign in
Add requestAuthorizationWithOptions flag
commit837417c50560998a7e09d4c36c55b63cbd1bd281 authorAli Abrar <aliabrar@gmail.com> dateMon 8 May 2017, 01:30 (9 years ago) parent95b3de8
3 files changed+28−14
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+12−7 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+12−5 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+4−2
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+12−7
@@ -9,8 +9,9 @@ extern void handlerCString(const char * _Nonnull, HsStablePtr);
99@end
1010
1111HsStablePtr globalHandler = 0;
12−HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
12+BOOL global_requestAuthorizationWithOptions = NO;
1313BOOL global_registerForRemoteNotifications = NO;
14+HsStablePtr global_didRegisterForRemoteNotificationsWithDeviceToken = 0;
1415
1516@implementation AppDelegate
1617
@@ -21,11 +22,13 @@ BOOL global_registerForRemoteNotifications = NO;
2122 self.window.backgroundColor = [UIColor whiteColor];
2223 [self.window makeKeyAndVisible];
2324 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
24− [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
25− completionHandler:^(BOOL granted, NSError * _Nullable error) {
26− // Handler used to alter application behavior based on types of notifications authorized
27− // Perhaps we don't need to do anything here.
28− }];
25+ if (global_requestAuthorizationWithOptions) {
26+ [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
27+ completionHandler:^(BOOL granted, NSError * _Nullable error) {
28+ // Handler used to alter application behavior based on types of notifications authorized
29+ // Perhaps we don't need to do anything here.
30+ }];
31+ }
2932 if (global_registerForRemoteNotifications) {
3033 [application registerForRemoteNotifications];
3134 }
@@ -71,12 +74,14 @@ BOOL global_registerForRemoteNotifications = NO;
7174
7275void runInWKWebView(HsStablePtr handler,
7376 const char * _Nonnull progName,
77+ const BOOL hs_requestAuthorizationWithOptions,
7478 const BOOL hs_registerForRemoteNotifications,
7579 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken) {
7680 @autoreleasepool {
7781 globalHandler = handler;
78− global_didRegisterForRemoteNotificationsWithDeviceToken = hs_didRegisterForRemoteNotificationsWithDeviceToken;
82+ global_requestAuthorizationWithOptions = hs_requestAuthorizationWithOptions;
7983 global_registerForRemoteNotifications = hs_registerForRemoteNotifications;
84+ global_didRegisterForRemoteNotificationsWithDeviceToken = hs_didRegisterForRemoteNotificationsWithDeviceToken;
8085 const char * _Nonnull argv [] = {"", 0};
8186 UIApplicationMain(0, argv, nil, NSStringFromClass([AppDelegate class]));
8287 }
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+12−5
@@ -15,7 +15,12 @@ import Foreign.C.Types
1515import Foreign.StablePtr (StablePtr, newStablePtr)
1616import Language.Javascript.JSaddle (JSM)
1717
18−foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ()) -> CString -> CChar -> StablePtr (CString -> IO ()) -> IO ()
18+foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ())
19+ -> CString
20+ -> CChar -- requestAuthorizationWithOptions
21+ -> CChar -- registerForRemoteNotifications
22+ -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
23+ -> IO ()
1924
2025-- | Run JSaddle in a WKWebView
2126run :: (CString -> IO ()) -> JSM () -> IO ()
@@ -23,7 +28,7 @@ run didRegisterForRemoteNotificationsWithDeviceToken f = do
2328 handler <- newStablePtr (jsaddleMain f)
2429 progName <- getProgName
2530 deviceTokenHandler <- newStablePtr didRegisterForRemoteNotificationsWithDeviceToken
26− withCString progName $ \pn -> runInWKWebView handler pn 1 deviceTokenHandler
31+ withCString progName $ \pn -> runInWKWebView handler pn 1 1 deviceTokenHandler
2732
2833-- | Run JSaddle in a WKWebView first loading the specified file
2934-- from the mainBundle (relative to the resourcePath).
@@ -35,11 +40,13 @@ runFile :: ByteString -- ^ The file to navigate to.
3540runFile url allowing cfg f = do
3641 handler <- newStablePtr (jsaddleMainFile url allowing f)
3742 progName <- getProgName
38− deviceTokenHandler <- newStablePtr $ _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken cfg
39− let registerForRemoteNotifications = boolToCChar $ _appDelegateConfig_registerForRemoteNotifications cfg
43+ let requestAuthorizationWithOptions = boolToCChar $ _appDelegateConfig_requestAuthorizationWithOptions cfg
44+ registerForRemoteNotifications = boolToCChar $ _appDelegateConfig_registerForRemoteNotifications cfg
45+ didRegisterForRemoteNotificationsWithDeviceToken <- newStablePtr $ _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken cfg
4046 withCString progName $ \pn -> runInWKWebView
4147 handler
4248 pn
49+ requestAuthorizationWithOptions
4350 registerForRemoteNotifications
44− deviceTokenHandler
51+ didRegisterForRemoteNotificationsWithDeviceToken
4552
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+4−2
@@ -97,13 +97,15 @@ handlerCString c fptr = do
9797 f c
9898
9999data AppDelegateConfig = AppDelegateConfig
100− { _appDelegateConfig_registerForRemoteNotifications :: Bool
100+ { _appDelegateConfig_requestAuthorizationWithOptions :: Bool
101+ , _appDelegateConfig_registerForRemoteNotifications :: Bool
101102 , _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken :: CString -> IO ()
102103 }
103104
104105instance Default AppDelegateConfig where
105106 def = AppDelegateConfig
106− { _appDelegateConfig_registerForRemoteNotifications = False
107+ { _appDelegateConfig_requestAuthorizationWithOptions = False
108+ , _appDelegateConfig_registerForRemoteNotifications = False
107109 , _appDelegateConfig_didRegisterForRemoteNotificationsWithDeviceToken = \_ -> return ()
108110 }
109111