<rant about Mac programming>
Programming for the Mac is just like using a Mac. Apple hides important details and tries to force you to do things their way. Apple keeps changing things underneath you so all the documentation online or in books is always vaguely out of date. It's also expensive. I bought the cheapest Mac mini with 4GB RAM and a hard disk for development, thinking I could do mostly command-line stuff, but that's not the case. You really need to work from Xcode, and Xcode is a pig of a program that takes up a lot of RAM and is sort of slow. I almost immediately had to switch to using an external SSD on USB to get any reasonable responsiveness from my system. Apple is really trying to stuff Swift down everyone's throats, but I opted to go with Objective-C because of my Smalltalk background. It's not bad except the syntax is somewhat awful. My main issue with it is that part of what Smalltalk so productive is that it comes with an advanced IDE that's super fast and makes it easy to browse around large application frameworks to figure out how to use an API. Objective-C comes with an overwhelmingly huge application framework as well, but Xcode is slow and pokey and doesn't come with good facilities for diving through the framework. Code completion is not good enough. There should be a quick way to find how other people call the same method, check out the documentation for a method, and to check out the inheritance tree. Xcode is more of a traditional IDE with some code completion. It would be nice if Xcode actually labelled all of its inscrutable icons too. No one knows what any of those buttons mean, but using those buttons isn't optional either. The latest MacOS/OSX versions do include a web view, but I always get the feeling that Safari developers don't really understand web apps and want to discourage people from making them. I find that they only implement just enough features to Safari support their own uses and then lose all interest in implementing things in a general way that can have multiple uses. For example, for the longest time, they refused to implement the download attribute on links because Apple didn't need it, so why should anyone else need it? Then, when they did implement it, it initially didn't work on data-urls and blobs because they didn't understand how important that was for web apps. Similarly, the new WKWebView initially could only show files from the Internet and not load up anything locally, making it useless for JavaScript downloadable apps. Then, even when they did fix it, things like web workers or XMLHttpRequest are still broken, really limiting its usefulness.
</rant about Mac programming>
Anyway, I found a great blog post that shows how to make a one window app with a web view in it. It lists every step, so it's easy to follow along even with no understanding of Mac programming. It worked for me, but Xcode has changed its default app layout to use storyboards so some of the instructions don't work any more, and it used the old WebView which is very limited. The new WKWebView is better because it allows for JIT compilation of the JavaScript, and it comes with proper facilities for letting the JavaScript send data to native code (the old web view required a bad hack to do that). So here are some updated instructions:
- Get Xcode and start it up
- Create a new Xcode project
- Make a MacOS Cocoa Application
- Fill in the appropriate application info, choose Objective-C for the language
- That should bring you to the screen where you can adjust the project settings
- If you want to run in a sandbox, I think you have to turn on signing. I think Xcode will take care of getting the appropriate certificates for you (I had already gotten them earlier).
- At the bottom of the General settings, under "Linked Frameworks and Libraries", you should add the WebKit.framework
- In the Capabilities tab, you can turn on the App Sandbox if you want (I think this is needed for the Mac App Store). Be careful, there seems to be a UI bug there. Once you turn on the app sandbox, you can't turn it off from the UI any more.
- If you do enable the App Sandbox, you also need to enable "Outgoing Connections (Client)" in the Network category. This is required even if you don't use the network. WKWebView seemed to have problems loading local files if the network entitlement wasn't enabled.
- Go to your ViewController.h, and change it to
- When using storyboards, the app delegate doesn't have direct access to the view, so you have to control the view from the view controller instead.
- Then go to your ViewController.m. Usually, you would draw a web view in the view of the storyboard and then hook it up to the view controller. Although this is possible with the WKWebView, all the documentation I've seen suggest manually creating the WKWebView instead. I think this might be necessary to pass in all the configuration you want for the WKWebView. To manually create the WKWebView, add these methods that show a basic web page:
- Now when you run the program, you should see the web page from example.com there.
- The next step is to create a directory with all your local web pages that you want to show. Create a folder named html in the Finder (i.e. just a normal folder somewhere outside of Xcode)
- Drag that folder onto your project in the file list. Enable "Destination: Copy if needed" and "Added folders: Create folder references"
- You should now have a html folder in your project. You can delete the original html folder that you created earlier in the Finder since you no longer need it any more. (You can confirm that the html folder will be included in your project properly by looking at your project file under the Build Phases tab, and the html folder should be listed in the Copy BundleResources section)
- Create an index.html file in your new html folder. Put some stuff in it.
- To show that page, go to your ViewController.m and change the awakeFromNib method to this:
#import <Cocoa/Cocoa.h> #import <WebKit/WebKit.h> @interface ViewController : NSViewController @property(strong,nonatomic) WKWebView* webView; @end
- (void)loadView { [super loadView]; _webView = [[WKWebView alloc] initWithFrame: [[self view] bounds] ]; [[self view] addSubview:_webView]; // Instead of adding the web view as a subview as // in above, you can also just replace the whole // view with the web view using // [self setView: _webView]; } - (void)awakeFromNib { [_webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:@"https://www.example.com"]]]; }
- (void)awakeFromNib { NSString *resourcePath = [[NSBundle mainBundle] resourcePath]; NSString *htmlPath = [resourcePath stringByAppendingString:@"/html/index.html"]; NSString *htmlDirPath = [resourcePath stringByAppendingString:@"/html/"]; [_webView loadFileURL:[NSURL fileURLWithPath:htmlPath] allowingReadAccessToURL: [NSURL fileURLWithPath:htmlDirPath isDirectory:true]]; }