on master help sign in
Add run function for iOS using UIKit
commitd5ba5e77b68aa870a3fe27980658f0339b2ab510 authorHamish Mackenzie <Hamish.K.Mackenzie@googlemail.com> dateThu 29 Dec 2016, 11:15 (10 years ago) parentf3cdfa7
13 files changed+167−22
jsaddle-warp/jsaddle-warp.cabal+4−3 jsaddle-webkit2gtk/jsaddle-webkit2gtk.cabal+1−1 jsaddle-webkitgtk/jsaddle-webkitgtk.cabal+1−1 jsaddle-webkitgtk/src/Language/Javascript/JSaddle/WebKitGTK.hs+1−1 jsaddle-wkwebview/cbits/Main.m → jsaddle-wkwebview/cbits-cocoa/AppDelegate.m+1−1 jsaddle-wkwebview/cbits-uikit/AppDelegate.h+8 jsaddle-wkwebview/cbits-uikit/AppDelegate.m+57 jsaddle-wkwebview/cbits-uikit/ViewController.h+10 jsaddle-wkwebview/cbits-uikit/ViewController.m+40 jsaddle-wkwebview/jsaddle-wkwebview.cabal+16−4 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+19 jsaddle-wkwebview/src-ghcjs/Language/Javascript/JSaddle/WKWebView.hs+7 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView.hs → jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+2−11
jsaddle-warp/jsaddle-warp.cabal+4−3
@@ -1,5 +1,5 @@
11name: jsaddle-warp
2−version: 0.8.0.0
2+version: 0.8.2.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
@@ -46,9 +46,10 @@ library
4646 ghc-options: -ferror-spans -Wall
4747
4848test-suite test-tool
49+ if impl(ghcjs -any) || os(ios)
50+ buildable: False
4951
50− if !impl(ghcjs -any)
51− build-depends:
52+ build-depends:
5253 QuickCheck -any,
5354 bytestring -any,
5455 deepseq -any,
jsaddle-webkit2gtk/jsaddle-webkit2gtk.cabal+1−1
@@ -1,5 +1,5 @@
11name: jsaddle-webkit2gtk
2−version: 0.8.1.0
2+version: 0.8.2.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
jsaddle-webkitgtk/jsaddle-webkitgtk.cabal+1−1
@@ -1,5 +1,5 @@
11name: jsaddle-webkitgtk
2−version: 0.8.1.0
2+version: 0.8.2.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
jsaddle-webkitgtk/src/Language/Javascript/JSaddle/WebKitGTK.hs+1−1
@@ -24,7 +24,7 @@ module Language.Javascript.JSaddle.WebKitGTK (
2424
2525#ifdef ghcjs_HOST_OS
2626
27−run = IO () -> IO ()
27+run :: IO () -> IO ()
2828run = id
2929{-# INLINE run #-}
3030
jsaddle-wkwebview/cbits/Main.m → jsaddle-wkwebview/cbits-cocoa/AppDelegate.mrenamed+1−1
@@ -55,7 +55,7 @@ void runInWKWebView(HsStablePtr handler, const char * _Nonnull progName) {
5555 NSApplication *application = [NSApplication sharedApplication];
5656 AppDelegate *appDelegate = [[AppDelegate alloc] initApp:handler progName:[NSString stringWithCString:progName encoding:NSUTF8StringEncoding]];
5757 [application setDelegate:appDelegate];
58− char * argv [] = {"", 0};
58+ const char * _Nonnull argv [] = {"", 0};
5959 NSApplicationMain(0, argv);
6060 }
6161}
jsaddle-wkwebview/cbits-uikit/AppDelegate.hnew file+8
@@ -0,0 +1,8 @@
1+#include "HsFFI.h"
2+#import <UIKit/UIKit.h>
3+
4+@interface AppDelegate : UIResponder <UIApplicationDelegate>
5+
6+@property (nonatomic, assign) UIWindow *window;
7+
8+@end
jsaddle-wkwebview/cbits-uikit/AppDelegate.mnew file+57
@@ -0,0 +1,57 @@
1+#import "AppDelegate.h"
2+#import "ViewController.h"
3+
4+@interface AppDelegate ()
5+
6+
7+@end
8+
9+HsStablePtr globalHandler = 0;
10+
11+@implementation AppDelegate
12+
13+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
14+ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
15+ // Override point for customization after application launch.
16+ self.window.rootViewController = [[ViewController alloc] initWithHandler:globalHandler];
17+ self.window.backgroundColor = [UIColor whiteColor];
18+ [self.window makeKeyAndVisible];
19+ return YES;
20+}
21+
22+- (void)applicationWillResignActive:(UIApplication *)application {
23+ // 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.
24+ // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25+}
26+
27+
28+- (void)applicationDidEnterBackground:(UIApplication *)application {
29+ // 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.
30+ // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
31+}
32+
33+
34+- (void)applicationWillEnterForeground:(UIApplication *)application {
35+ // 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.
36+}
37+
38+
39+- (void)applicationDidBecomeActive:(UIApplication *)application {
40+ // 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.
41+}
42+
43+
44+- (void)applicationWillTerminate:(UIApplication *)application {
45+ // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
46+}
47+
48+@end
49+
50+void runInWKWebView(HsStablePtr handler, const char * _Nonnull progName) {
51+ @autoreleasepool {
52+ globalHandler = handler;
53+ const char * _Nonnull argv [] = {"", 0};
54+ UIApplicationMain(0, argv, nil, NSStringFromClass([AppDelegate class]));
55+ }
56+}
57+
jsaddle-wkwebview/cbits-uikit/ViewController.hnew file+10
@@ -0,0 +1,10 @@
1+#include "HsFFI.h"
2+#import <UIKit/UIKit.h>
3+#import <WebKit/WebKit.h>
4+
5+@interface ViewController : UIViewController
6+@property (nonatomic, assign) HsStablePtr handler;
7+@property (nonatomic, assign) WKWebView *webView;
8+
9+- (instancetype)initWithHandler:(HsStablePtr)handler;
10+@end
jsaddle-wkwebview/cbits-uikit/ViewController.mnew file+40
@@ -0,0 +1,40 @@
1+#import "ViewController.h"
2+
3+extern void withWebView(WKWebView *, HsStablePtr);
4+
5+@interface ViewController ()
6+
7+@end
8+
9+@implementation ViewController
10+
11+-(id)initWithHandler:(HsStablePtr)handler {
12+ self = [super init];
13+ if (self) {
14+ _handler = handler;
15+ }
16+ return self;
17+}
18+
19+- (void)loadView {
20+ [super loadView];
21+ // WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
22+ // [theConfiguration.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
23+ _webView = [[WKWebView alloc] init];
24+ _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
25+ self.view = _webView;
26+}
27+
28+- (void)viewDidLoad {
29+ [super viewDidLoad];
30+ withWebView(_webView, _handler);
31+}
32+
33+
34+- (void)didReceiveMemoryWarning {
35+ [super didReceiveMemoryWarning];
36+ // Dispose of any resources that can be recreated.
37+}
38+
39+
40+@end
jsaddle-wkwebview/jsaddle-wkwebview.cabal+16−4
@@ -1,5 +1,5 @@
11name: jsaddle-wkwebview
2−version: 0.8.1.0
2+version: 0.8.2.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
@@ -21,6 +21,7 @@ source-repository head
2121library
2222 exposed-modules:
2323 Language.Javascript.JSaddle.WKWebView
24+ Language.Javascript.JSaddle.WKWebView.Internal
2425 build-depends:
2526 aeson >=0.8.0.2 && <1.1,
2627 base <5,
@@ -28,9 +29,20 @@ library
2829 jsaddle >= 0.8.0.0 && <0.9
2930 default-language: Haskell2010
3031 hs-source-dirs: src
31− frameworks: Cocoa, WebKit
32+ frameworks: Foundation, WebKit
3233 ghc-options: -ferror-spans -Wall
3334 c-sources:
34− cbits/Main.m
3535 cbits/WKWebView.m
36−
36+ if impl(ghcjs)
37+ hs-source-dirs: src-ghcjs
38+ else
39+ hs-source-dirs: src-ghc
40+ if os(ios)
41+ frameworks: UIKit
42+ c-sources:
43+ cbits-uikit/AppDelegate.m
44+ cbits-uikit/ViewController.m
45+ else
46+ frameworks: Cocoa
47+ c-sources:
48+ cbits-cocoa/AppDelegate.m
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hsnew file+19
@@ -0,0 +1,19 @@
1+module Language.Javascript.JSaddle.WKWebView
2+ ( jsaddleMain
3+ , WKWebView(..)
4+ , run
5+ ) where
6+
7+import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, WKWebView(..))
8+import System.Environment (getProgName)
9+import Foreign.C.String (CString, withCString)
10+import Foreign.StablePtr (StablePtr, newStablePtr)
11+import Language.Javascript.JSaddle (JSM)
12+
13+foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ()) -> CString -> IO ()
14+
15+run :: JSM () -> IO ()
16+run f = do
17+ handler <- newStablePtr (jsaddleMain f)
18+ progName <- getProgName
19+ withCString progName $ runInWKWebView handler
jsaddle-wkwebview/src-ghcjs/Language/Javascript/JSaddle/WKWebView.hsnew file+7
@@ -0,0 +1,7 @@
1+module Language.Javascript.JSaddle.WKWebView
2+ ( run
3+ ) where
4+
5+run :: IO () -> IO ()
6+run = id
7+{-# INLINE run #-}
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView.hs → jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hsrenamed+2−11
@@ -1,11 +1,9 @@
11{-# LANGUAGE ForeignFunctionInterface, OverloadedStrings #-}
2−module Language.Javascript.JSaddle.WKWebView
2+module Language.Javascript.JSaddle.WKWebView.Internal
33 ( jsaddleMain
44 , WKWebView(..)
5− , run
65 ) where
76
8−import System.Environment (getProgName)
97import Control.Monad (void, join)
108import Control.Concurrent (forkIO)
119
@@ -14,7 +12,7 @@ import Data.ByteString (useAsCString, packCString)
1412import Data.ByteString.Lazy (ByteString, toStrict, fromStrict)
1513import Data.Aeson (encode, decode)
1614
17−import Foreign.C.String (CString, withCString)
15+import Foreign.C.String (CString)
1816import Foreign.Ptr (Ptr)
1917import Foreign.StablePtr (StablePtr, newStablePtr, deRefStablePtr)
2018
@@ -30,13 +28,6 @@ foreign export ccall withWebView :: WKWebView -> StablePtr (WKWebView -> IO ())
3028foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
3129foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
3230foreign import ccall evaluateJavaScript :: WKWebView -> CString -> IO ()
33−foreign import ccall runInWKWebView :: StablePtr (WKWebView -> IO ()) -> CString -> IO ()
34−
35−run :: JSM () -> IO ()
36−run f = do
37− handler <- newStablePtr (jsaddleMain f)
38− progName <- getProgName
39− withCString progName $ runInWKWebView handler
4031
4132jsaddleMain :: JSM () -> WKWebView -> IO ()
4233jsaddleMain f webView = do