Created
September 16, 2017 08:24
-
-
Save userow/a3ca26bb989b4b9ff419d05fa530eb4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
static NSString * const adCountKey = @"adCountKey"; | |
static NSString * const highScoreKey = @"highScoreKey"; | |
@import WebKit; | |
@interface ViewController () <WKScriptMessageHandler> | |
@property (strong, nonatomic) WKWebView *webView; | |
@property (assign, nonatomic) NSInteger adCount; | |
@property (assign, nonatomic) NSInteger highScore; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
//TODO: read adCount / highScore from UserDef | |
self.adCount = [[NSUserDefaults standardUserDefaults] integerForKey:adCountKey]; | |
self.highScore = [[NSUserDefaults standardUserDefaults] integerForKey:highScoreKey]; | |
// First create a WKWebViewConfiguration object so we can add a controller | |
// pointing back to this ViewController. | |
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; | |
WKUserContentController *controller = [[WKUserContentController alloc] init]; | |
// Add a script handler for the "observe" call. This is added to every frame | |
// in the document (window.webkit.messageHandlers.NAME). | |
[controller addScriptMessageHandler:self name:@"observeHighScore"]; | |
[controller addScriptMessageHandler:self name:@"observeAd"]; | |
// [controller addScriptMessageHandler:self name:@"observeLocalStorage"]; | |
configuration.userContentController = controller; | |
NSString *gamePath = [NSBundle.mainBundle pathForResource:@"index" ofType:@"html" inDirectory:@"RunColorGame"]; | |
// This is the URL to be loaded into the WKWebView. | |
NSURL *gameUrl = [NSURL fileURLWithPath:gamePath]; | |
NSString *dir = [gamePath stringByDeletingLastPathComponent]; | |
NSURL *dirUrl = [NSURL fileURLWithPath:dir isDirectory:YES]; | |
// Initialize the WKWebView with the current frame and the configuration | |
// setup above | |
_webView = [[WKWebView alloc] initWithFrame:self.view.frame | |
configuration:configuration]; | |
// Load the jsbin URL into the WKWebView and then add it as a sub-view. | |
[self.view addSubview:_webView]; | |
NSError *error = nil; | |
BOOL isReachable = [gameUrl checkResourceIsReachableAndReturnError:&error]; | |
if (isReachable) { | |
[_webView loadFileURL:gameUrl | |
allowingReadAccessToURL:dirUrl]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment