Skip to content

Commit

Permalink
Add error page and reloading when server not available
Browse files Browse the repository at this point in the history
  • Loading branch information
stonesam92 committed May 22, 2015
1 parent a9cccb1 commit 6f73733
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
26 changes: 24 additions & 2 deletions WhatsMac/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
//Whatsapp web only works with specific user agents
_webView._customUserAgent = @"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://web.whatsapp.com"]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://web.whatsapp.com"]];
[_webView loadRequest:urlRequest];
[_window makeKeyAndOrderFront:self];

Expand Down Expand Up @@ -159,6 +159,11 @@ - (IBAction)showFAQ:(id)sender {
[self.faq makeKeyAndOrderFront:self];
}

- (IBAction)reloadPage:(id)sender {
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://web.whatsapp.com"]];
[self.webView loadRequest:urlRequest];
}

- (void)setActiveConversationAtIndex:(NSString*)index {
[self.webView evaluateJavaScript:
[NSString stringWithFormat:@"setActiveConversationAtIndex(%@)", index]
Expand All @@ -171,14 +176,25 @@ - (void)setActiveConversationAtIndex:(NSString*)index {
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL *url = navigationAction.request.URL;

if ([url.host hasSuffix:@"whatsapp.com"] || [url.host hasSuffix:@"whatsapp.net"]) {
if ([url.host hasSuffix:@"whatsapp.com"] || [url.host hasSuffix:@"whatsapp.net"] ||
[url.scheme isEqualToString:@"file"]) {
decisionHandler(WKNavigationActionPolicyAllow);
} else {
decisionHandler(WKNavigationActionPolicyCancel);
[[NSWorkspace sharedWorkspace] openURL:url];
}
}

- (void)webView:(WKWebView*)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"Failed navigation with error: %@", error);
[self showFailedConnectionPage];
}

- (void)webView:(WKWebView*)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"Failed navigation with error: %@", error);
[self showFailedConnectionPage];
}

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{

Expand Down Expand Up @@ -286,4 +302,10 @@ - (NSWindow*)createWindow:(NSString*)identifier title:(NSString*)title URL:(NSSt
CFBridgingRetain(window);
return window;
}

- (void)showFailedConnectionPage {
NSURL *failedPageURL = [[NSBundle mainBundle] URLForResource:@"noConnection" withExtension:@"html"];
NSURLRequest *failedPageRequest = [NSURLRequest requestWithURL:failedPageURL];
[self.webView loadRequest:failedPageRequest];
}
@end
6 changes: 6 additions & 0 deletions WhatsMac/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<action selector="checkForUpdates:" target="G8N-Ob-zSb" id="8Rw-VO-QT8"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="dmJ-G0-0Is"/>
<menuItem title="Reload From Server" keyEquivalent="r" id="365-gz-eEk">
<connections>
<action selector="reloadPage:" target="dKU-pJ-OAl" id="9jt-lm-BqN"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="VOq-y0-SEH"/>
<menuItem title="Services" id="NMo-om-nkz">
<modifierMask key="keyEquivalentModifierMask"/>
Expand Down
38 changes: 38 additions & 0 deletions WhatsMac/noConnection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<html>
<head>
<meta charset="utf-8">
<style>
.noConnectionMessage {
width:70%;
height:200px;
margin-left:15%;
margin-right:15%;
margin-top:25%;
margin-bottom:25%;
border-radius: 15.0px;
background-color: #F6F6F6;
display: table;
}
body {
background-color: #009688;
}
p {
text-align: center;
vertical-align: middle;
display: table-cell;
font-family: Helvetica, Arial, Sans-Serif;
line-height: 35px;
font-size: 14pt;
}

</style>
</head>
<body>
<div class="noConnectionMessage">
<p>
Sorry! Could not connect to WhatsApp Web. </br>
Press ⌘R to reload the page.
</p>
</div>
</body>
</html>

0 comments on commit 6f73733

Please sign in to comment.