on master help sign in
Add basic AppDelegate callbacks to AppDelegateConfig
commita5bfacf8f9eacc7dccb30c38d00d841056d02917 authorAli Abrar <aliabrar@gmail.com> dateMon 8 May 2017, 14:08 (9 years ago) parent9dbfcb1
3 files changed+106−12
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+47−2 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+51−8 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+8−2
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+47−2
@@ -2,6 +2,7 @@
22#import "ViewController.h"
33#import <UserNotifications/UserNotifications.h>
44
5+extern void handlerIO(HsStablePtr);
56extern void handlerCString(const char * _Nonnull, HsStablePtr);
67
78@interface AppDelegate ()
@@ -9,6 +10,14 @@ extern void handlerCString(const char * _Nonnull, HsStablePtr);
910@end
1011
1112HsStablePtr globalHandler = 0;
13+HsStablePtr global_willFinishLaunchingWithOptions = 0;
14+HsStablePtr global_didFinishLaunchingWithOptions = 0;
15+HsStablePtr global_applicationDidBecomeActive = 0;
16+HsStablePtr global_applicationWillResignActive = 0;
17+HsStablePtr global_applicationDidEnterBackground = 0;
18+HsStablePtr global_applicationWillEnterForeground = 0;
19+HsStablePtr global_applicationWillTerminate = 0;
20+HsStablePtr global_applicationSignificantTimeChange = 0;
1221BOOL global_requestAuthorizationWithOptions = NO;
1322BOOL global_requestAuthorizationOptionBadge = NO;
1423BOOL global_requestAuthorizationOptionSound = NO;
@@ -20,7 +29,14 @@ HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
2029
2130@implementation AppDelegate
2231
32+- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
33+ // Tells the delegate that the launch process has begun but that state restoration has not yet occurred.
34+ handlerIO(global_willFinishLaunchingWithOptions);
35+ return YES;
36+}
37+
2338- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
39+ // Tells the delegate that the launch process is almost done and the app is almost ready to run.
2440 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
2541 // Override point for customization after application launch.
2642 self.window.rootViewController = [[ViewController alloc] initWithHandler:globalHandler];
@@ -49,49 +65,70 @@ HsStablePtr global_didFailToRegisterForRemoteNotificationsWithError = 0;
4965 if (global_registerForRemoteNotifications) {
5066 [application registerForRemoteNotifications];
5167 }
68+ handlerIO(global_didFinishLaunchingWithOptions);
5269 return YES;
5370}
5471
5572- (void)applicationWillResignActive:(UIApplication *)application {
5673 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
5774 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
75+ handlerIO(global_applicationWillResignActive);
5876}
5977
6078
6179- (void)applicationDidEnterBackground:(UIApplication *)application {
6280 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
6381 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
82+ handlerIO(global_applicationDidEnterBackground);
6483}
6584
6685
6786- (void)applicationWillEnterForeground:(UIApplication *)application {
6887 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
88+ handlerIO(global_applicationWillEnterForeground);
6989}
7090
7191
7292- (void)applicationDidBecomeActive:(UIApplication *)application {
7393 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
94+ handlerIO(global_applicationDidBecomeActive);
7495}
7596
7697
7798- (void)applicationWillTerminate:(UIApplication *)application {
7899 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
100+ handlerIO(global_applicationWillTerminate);
79101}
80102
81103- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
104+ // Tells the delegate that the app successfully registered with Apple Push Notification service (APNs).
82105 NSString *deviceTokenString = [deviceToken base64EncodedStringWithOptions: 0];
83106 handlerCString([deviceTokenString UTF8String], global_didRegisterForRemoteNotificationsWithDeviceToken);
84107}
85108
86109- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
87− NSString *errorString = [error localizedDescription];
88− handlerCString([errorString UTF8String], global_didFailToRegisterForRemoteNotificationsWithError);
110+ // Sent to the delegate when Apple Push Notification service cannot successfully complete the registration process.
111+ NSString *errorString = [error localizedDescription];
112+ handlerCString([errorString UTF8String], global_didFailToRegisterForRemoteNotificationsWithError);
113+}
114+
115+- (void)applicationSignificantTimeChange:(UIApplication *)application {
116+ // Tells the delegate when there is a significant change in the time.
117+ handlerIO(global_applicationSignificantTimeChange);
89118}
90119
91120@end
92121
93122void runInWKWebView(HsStablePtr handler,
94123 const char * _Nonnull progName,
124+ HsStablePtr hs_willFinishLaunchingWithOptions,
125+ HsStablePtr hs_didFinishLaunchingWithOptions,
126+ HsStablePtr hs_applicationDidBecomeActive,
127+ HsStablePtr hs_applicationWillResignActive,
128+ HsStablePtr hs_applicationDidEnterBackground,
129+ HsStablePtr hs_applicationWillEnterForeground,
130+ HsStablePtr hs_applicationWillTerminate,
131+ HsStablePtr hs_applicationSignificantTimeChange,
95132 const BOOL hs_requestAuthorizationWithOptions,
96133 const BOOL hs_requestAuthorizationOptionBadge,
97134 const BOOL hs_requestAuthorizationOptionSound,
@@ -102,6 +139,14 @@ void runInWKWebView(HsStablePtr handler,
102139 HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError) {
103140 @autoreleasepool {
104141 globalHandler = handler;
142+ global_willFinishLaunchingWithOptions = hs_willFinishLaunchingWithOptions;
143+ global_didFinishLaunchingWithOptions = hs_didFinishLaunchingWithOptions;
144+ global_applicationDidBecomeActive = hs_applicationDidBecomeActive;
145+ global_applicationWillResignActive = hs_applicationWillResignActive;
146+ global_applicationDidEnterBackground = hs_applicationDidEnterBackground;
147+ global_applicationWillEnterForeground = hs_applicationWillEnterForeground;
148+ global_applicationWillTerminate = hs_applicationWillTerminate;
149+ global_applicationSignificantTimeChange = hs_applicationSignificantTimeChange;
105150 global_requestAuthorizationWithOptions = hs_requestAuthorizationWithOptions;
106151 global_requestAuthorizationOptionBadge = hs_requestAuthorizationOptionBadge;
107152 global_requestAuthorizationOptionSound = hs_requestAuthorizationOptionSound;
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+51−8
@@ -24,6 +24,14 @@ import Language.Javascript.JSaddle (JSM)
2424foreign import ccall runInWKWebView
2525 :: StablePtr (WKWebView -> IO ())
2626 -> CString
27+ -> StablePtr (IO ()) -- willFinishLaunchingWithOptions
28+ -> StablePtr (IO ()) -- didFinishLaunchingWithOptions
29+ -> StablePtr (IO ()) -- applicationDidBecomeActive
30+ -> StablePtr (IO ()) -- applicationWillResignActive
31+ -> StablePtr (IO ()) -- applicationDidEnterBackground
32+ -> StablePtr (IO ()) -- applicationWillEnterForeground
33+ -> StablePtr (IO ()) -- applicationWillTerminate
34+ -> StablePtr (IO ()) -- applicationSignificantTimeChange
2735 -> CChar -- whether to run requestAuthorizationWithOptions
2836 -> CChar -- Ask for Badge authorization
2937 -> CChar -- Ask for Sound authorization
@@ -34,14 +42,29 @@ foreign import ccall runInWKWebView
3442 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
3543 -> IO ()
3644
37−
3845data AppDelegateConfig = AppDelegateConfig
39− { _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
46+ { _appDelegateConfig_willFinishLaunchingWithOptions :: IO ()
47+ , _appDelegateConfig_didFinishLaunchingWithOptions :: IO ()
48+ , _appDelegateConfig_applicationDidBecomeActive :: IO ()
49+ , _appDelegateConfig_applicationWillResignActive :: IO ()
50+ , _appDelegateConfig_applicationDidEnterBackground :: IO ()
51+ , _appDelegateConfig_applicationWillEnterForeground :: IO ()
52+ , _appDelegateConfig_applicationWillTerminate :: IO ()
53+ , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
54+ , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
4055 }
4156
4257instance Default AppDelegateConfig where
4358 def = AppDelegateConfig
44− { _appDelegateConfig_appDelegateNotificationConfig = def
59+ { _appDelegateConfig_willFinishLaunchingWithOptions = return ()
60+ , _appDelegateConfig_didFinishLaunchingWithOptions = return ()
61+ , _appDelegateConfig_applicationDidBecomeActive = return ()
62+ , _appDelegateConfig_applicationWillResignActive = return ()
63+ , _appDelegateConfig_applicationDidEnterBackground = return ()
64+ , _appDelegateConfig_applicationWillEnterForeground = return ()
65+ , _appDelegateConfig_applicationWillTerminate = return ()
66+ , _appDelegateConfig_applicationSignificantTimeChange = return ()
67+ , _appDelegateConfig_appDelegateNotificationConfig = def
4568 }
4669
4770data AuthorizationOption = AuthorizationOption_Badge
@@ -59,11 +82,11 @@ data AppDelegateNotificationConfig = AppDelegateNotificationConfig
5982
6083instance Default AppDelegateNotificationConfig where
6184 def = AppDelegateNotificationConfig
62− { _appDelegateNotificationConfig_requestAuthorizationWithOptions = Nothing
63− , _appDelegateNotificationConfig_registerForRemoteNotifications = False
64− , _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken = \_ -> return ()
65− , _appDelegateNotificationConfig_didFailToRegisterForRemoteNotificationsWithError = \_ -> return ()
66− }
85+ { _appDelegateNotificationConfig_requestAuthorizationWithOptions = Nothing
86+ , _appDelegateNotificationConfig_registerForRemoteNotifications = False
87+ , _appDelegateNotificationConfig_didRegisterForRemoteNotificationsWithDeviceToken = \_ -> return ()
88+ , _appDelegateNotificationConfig_didFailToRegisterForRemoteNotificationsWithError = \_ -> return ()
89+ }
6790
6891
6992-- | Run JSaddle in a WKWebView
@@ -88,6 +111,18 @@ run' mUrl cfg f = do
88111 Just (url, allowing) -> newStablePtr (jsaddleMainFile url allowing f)
89112 Nothing -> newStablePtr (jsaddleMain f)
90113 progName <- getProgName
114+
115+ -- AppDelegate callbacks
116+ willFinishLaunchingWithOptions <- newStablePtr $ _appDelegateConfig_willFinishLaunchingWithOptions cfg
117+ didFinishLaunchingWithOptions <- newStablePtr $ _appDelegateConfig_didFinishLaunchingWithOptions cfg
118+ applicationDidBecomeActive <- newStablePtr $ _appDelegateConfig_applicationDidBecomeActive cfg
119+ applicationWillResignActive <- newStablePtr $ _appDelegateConfig_applicationWillResignActive cfg
120+ applicationDidEnterBackground <- newStablePtr $ _appDelegateConfig_applicationDidEnterBackground cfg
121+ applicationWillEnterForeground <- newStablePtr $ _appDelegateConfig_applicationWillEnterForeground cfg
122+ applicationWillTerminate <- newStablePtr $ _appDelegateConfig_applicationWillTerminate cfg
123+ applicationSignificantTimeChange <- newStablePtr $ _appDelegateConfig_applicationSignificantTimeChange cfg
124+
125+ -- AppDelegate notification configuration
91126 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
92127 (requestAuthorizationWithOptions, authorizationOptions) =
93128 case _appDelegateNotificationConfig_requestAuthorizationWithOptions ncfg of
@@ -99,6 +134,14 @@ run' mUrl cfg f = do
99134 withCString progName $ \pn -> runInWKWebView
100135 handler
101136 pn
137+ willFinishLaunchingWithOptions
138+ didFinishLaunchingWithOptions
139+ applicationDidBecomeActive
140+ applicationWillResignActive
141+ applicationDidEnterBackground
142+ applicationWillEnterForeground
143+ applicationWillTerminate
144+ applicationSignificantTimeChange
102145 (boolToCChar requestAuthorizationWithOptions)
103146 (boolToCChar $ Set.member AuthorizationOption_Badge authorizationOptions)
104147 (boolToCChar $ Set.member AuthorizationOption_Sound authorizationOptions)
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+8−2
@@ -33,6 +33,7 @@ foreign export ccall jsaddleStart :: StablePtr (IO ()) -> IO ()
3333foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString -> IO ()
3434foreign export ccall withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
3535foreign export ccall handlerCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
36+foreign export ccall handlerIO :: StablePtr (IO ()) -> IO ()
3637foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
3738foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
3839foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
@@ -91,8 +92,13 @@ withWebView webView ptrF = do
9192
9293handlerCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
9394handlerCString c fptr = do
94− f <- deRefStablePtr fptr
95− f c
95+ f <- deRefStablePtr fptr
96+ f c
97+
98+handlerIO :: StablePtr (IO ()) -> IO ()
99+handlerIO ptr = do
100+ f <- deRefStablePtr ptr
101+ f
96102
97103boolToCChar :: Bool -> CChar
98104boolToCChar a = if a then 1 else 0