Our game Onslaught! Arena was only available on the Chrome Webstore when we first launched it. We recently announced that we had also launched the game on the Mac App Store, using the exact same source code. Isn't HTML5 awesome?
Many of you expressed interest in how to embed a WebView into a Mac app, and as promised, here's a detailed tutorial.
/Developer/Applications/Xcode.app
.
File / New Project... / Mac OS X / Application / Cocoa Application / Choose ...
then select the folder to save your project into. I'll use WebViewExample as the project name in this tutorial (but you can use whatever you want).
Overview / Groups & Files
, expand WebViewExample / Frameworks
by clicking the arrow icon.
Add / Existing Frameworks... / Webkit.framework / Add
.
.html
files, etc.) and drag it into this Resources folder in Xcode. Make sure to choose Create Folder References for any added folders.
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface WebViewExampleAppDelegate : NSObject {
NSWindow *window;
IBOutlet WebView *webView;
}
@property (assign) IBOutlet NSWindow *window;
@property (nonatomic, retain) IBOutlet WebView *webView;
@end
#import "WebViewExampleAppDelegate.h"
@implementation WebViewExampleAppDelegate
@synthesize window;
@synthesize webView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}
- (void)awakeFromNib {
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [resourcesPath stringByAppendingString:@"/htdocs/index.html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
}
@end
Note: in the line NSString *htmlPath = [resourcesPath stringByAppendingString:@"/htdocs/index.html"];
, change the string "/htdocs/index.html" to your game's .html
file.
/Developer/Applications/Interface Builder.app
).
File / Save
or command+s).
This example project is available on our GitHub page (I've included my old game Spacius! as an example). You can also download the generated WebViewExample app directly.
I'll post another tutorial soon on how to make a more complete app (EDIT : now available here!), including adding icons, enabling localStorage
and building a bridge from JavaScript to Objective-C.
Follow us on Twitter to get notified when we publish that tutorial.
Could you get it to work? Any steps I could change to make things easier to understand? Please let me know in the comments.
LDG © 2022 • Blog • Terms of Service • Video Policy • v2.1.2