Skip to content

Commit

Permalink
Merge pull request #2 from stephenwoodford/error_event
Browse files Browse the repository at this point in the history
Feature Request: iOS "error" event
  • Loading branch information
Mads Møller committed Sep 5, 2014
2 parents 07b6850 + 81171c6 commit cab0ecc
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
9 changes: 8 additions & 1 deletion ios/Classes/DkNappJockeyWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@
UIWebView *webview;
NSURL *url;
NSString *lastValidLoad;
BOOL debug;

BOOL ignoreNextRequest;
id reloadData;
id reloadDataProperties;
SEL reloadMethod;

BOOL debug;
}

@property(nonatomic,readonly) id url;
@property(nonatomic,readwrite,retain) id reloadData;
@property(nonatomic,readwrite,retain) id reloadDataProperties;

-(void)render;
-(void)sendJockeyData:(id)args;
Expand Down
95 changes: 86 additions & 9 deletions ios/Classes/DkNappJockeyWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "DkNappJockeyWebView.h"

@implementation DkNappJockeyWebView
@synthesize reloadData, reloadDataProperties;

-(void)dealloc
{
Expand All @@ -25,6 +26,8 @@ -(void)dealloc
}
RELEASE_TO_NIL(webview);
RELEASE_TO_NIL(url);
RELEASE_TO_NIL(reloadData);
RELEASE_TO_NIL(reloadDataProperties);
RELEASE_TO_NIL(lastValidLoad);
[super dealloc];
}
Expand Down Expand Up @@ -70,8 +73,14 @@ -(void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds

- (void)setUrl_:(id)args
{
RELEASE_TO_NIL(url);
ENSURE_SINGLE_ARG(args,NSString);
ignoreNextRequest = YES;
[self setReloadData:args];
[self setReloadDataProperties:nil];
reloadMethod = @selector(setUrl_:);

RELEASE_TO_NIL(url);
RELEASE_TO_NIL(lastValidLoad);
ENSURE_SINGLE_ARG(args,NSString);

url = [[TiUtils toURL:args proxy:(TiProxy*)self.proxy] retain];

Expand All @@ -92,12 +101,16 @@ - (void)setDebug_:(id)args
- (void)reload
{
RELEASE_TO_NIL(lastValidLoad);
if (webview == nil)
{
return;
}

[webview reload];
if (webview == nil)
{
return;
}
if (reloadData != nil)
{
[self performSelector:reloadMethod withObject:reloadData withObject:reloadDataProperties];
return;
}
[webview reload];
}

- (void)stopLoading
Expand Down Expand Up @@ -190,6 +203,24 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
return NO;
}

NSString * scheme = [[newUrl scheme] lowercaseString];
if ([scheme hasPrefix:@"http"] || [scheme isEqualToString:@"ftp"]
|| [scheme isEqualToString:@"file"] || [scheme isEqualToString:@"app"]) {
DebugLog(@"[DEBUG] New scheme: %@",request);
BOOL valid = !ignoreNextRequest;
if ([scheme hasPrefix:@"http"]) {
//UIWebViewNavigationTypeOther means we are either in a META redirect
//or it is a js request from within the page
valid = valid && (navigationType != UIWebViewNavigationTypeOther);
}
if (valid) {
[self setReloadData:[newUrl absoluteString]];
[self setReloadDataProperties:nil];
reloadMethod = @selector(setUrl_:);
}
return YES;
}

if(debug){
NSLog(@"[NappJockey] No jockey event - fallback");
}
Expand Down Expand Up @@ -227,8 +258,54 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView
lastValidLoad = [urlAbs retain];
}
}

ignoreNextRequest = NO;
}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSString *offendingUrl = [self url];

if ([[error domain] isEqual:NSURLErrorDomain])
{
offendingUrl = [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey];

// this means the pending request has been cancelled and should be
// safely squashed
if ([error code]==NSURLErrorCancelled)
{
return;
}
}

NSLog(@"[ERROR] Error loading: %@, Error: %@",offendingUrl,error);

if ([self.proxy _hasListeners:@"error"])
{
NSString * message = [TiUtils messageFromError:error];
NSMutableDictionary *event = [NSMutableDictionary dictionaryWithObject:message forKey:@"message"];

// We combine some error codes into a single one which we share with Android.
NSInteger rawErrorCode = [error code];
NSInteger returnErrorCode = rawErrorCode;

if (rawErrorCode == NSURLErrorUserCancelledAuthentication)
{
returnErrorCode = NSURLErrorUserAuthenticationRequired; // URL_ERROR_AUTHENTICATION
}
else if (rawErrorCode == NSURLErrorNoPermissionsToReadFile || rawErrorCode == NSURLErrorCannotCreateFile || rawErrorCode == NSURLErrorFileIsDirectory || rawErrorCode == NSURLErrorCannotCloseFile || rawErrorCode == NSURLErrorCannotWriteToFile || rawErrorCode == NSURLErrorCannotRemoveFile || rawErrorCode == NSURLErrorCannotMoveFile)
{
returnErrorCode = NSURLErrorCannotOpenFile; // URL_ERROR_FILE
}
else if (rawErrorCode == NSURLErrorDNSLookupFailed)
{
returnErrorCode = NSURLErrorCannotFindHost; // URL_ERROR_HOST_LOOKUP
}

[event setObject:[NSNumber numberWithInteger:returnErrorCode] forKey:@"errorCode"];
[event setObject:offendingUrl forKey:@"url"];
[self.proxy fireEvent:@"error" withObject:event errorCode:returnErrorCode message:message];
}
}

@end

0 comments on commit cab0ecc

Please sign in to comment.