Skip to content

Commit

Permalink
Removed commented code
Browse files Browse the repository at this point in the history
  • Loading branch information
possan committed Feb 22, 2012
1 parent b6d3ac6 commit 8492bc2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 90 deletions.
17 changes: 1 addition & 16 deletions IOSMQ.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,11 @@

@end

/*
@interface IOSMQQuery : NSObject
+ (IOSMQQuery *)query;
+ (IOSMQQuery *)queryWithMessage:(NSString *)message;
@property (retain) NSString *message;
@property (retain) NSString *messageid;
@end
*/

@class IOSMQ;

@protocol IOSMQDelegate <NSObject>

- (void) queue:(IOSMQ *)queue message:(IOSMQMessage*)message;
// - (void) queue:(IOSMQ *)queue query:(IOSMQQuery *)query;
// - (void) queue:(IOSMQ *)queue response:(NSString*)message withID:(NSString *)messageid;

@end

Expand All @@ -47,15 +36,11 @@
@property (strong,retain) id<UIWebViewDelegate> nextDelegate;
@property (strong,retain) id<IOSMQDelegate> delegate;
@property (strong,retain) UIWebView *webview;
@property (nonatomic,assign) float pollInterval;

// + (NSString *) generateID;
+ (id) queueWithWebView:(UIWebView *)webview;

// - (id) initWithWebView:(UIWebView *)webview;
- (void) send:(IOSMQMessage *)message;
- (void) start;
// - (void) query:(IOSMQQuery *)query;
// - (void) postMessage:(NSString *)string;
// - (void) sendResponse:(NSString *)message toQuery:(NSString *)messageid;

@end
86 changes: 12 additions & 74 deletions IOSMQ.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ + (IOSMQMessage *)messageWithMessage:(NSString *)message withID:(NSString *)id {
return [ret autorelease];
}


- (id) init {
self = [super init];
if( self != nil ) {
Expand All @@ -44,45 +43,18 @@ - (id) init {
}

@end


/*
@implementation IOSMQQuery
@synthesize message;
@synthesize messageid;
+ (IOSMQQuery *)query {
IOSMQQuery *ret = [[IOSMQQuery alloc] init];
return ret;
}
+ (IOSMQQuery *)queryWithMessage:(NSString *)message {
IOSMQQuery *ret = [[IOSMQQuery alloc] init];
return ret;
}
- (id) init {
self = [super init];
if( self != nil ) {
self.message = @"";
self.messageid = @"";
}
return self;
}
@end
*/


@implementation IOSMQ

@synthesize nextDelegate;
@synthesize delegate;
@synthesize webview;
@synthesize pollInterval;

- (id) initWithWebView:(UIWebView *)_webview {
self = [super init];
if( self != nil ) {
self.pollInterval = 0.5;
self.webview = _webview;
self.nextDelegate = _webview.delegate;
_webview.delegate = nil;
Expand All @@ -104,38 +76,27 @@ - (void) send:(IOSMQMessage *)message {
NSString *js = [NSString stringWithFormat:@"IOSMQ._backendPush({id:'%@',message:'%@'})",message.id,message.message];
NSLog(@"pushing json: %@",js);
[self.webview stringByEvaluatingJavaScriptFromString:js];
}

/*
- (void) query:(NSString *)string withID:(NSString *)messageid { }
- (void) query:(IOSMQQuery *)query { }
- (void) postMessage:(NSString *)string { }
- (void) sendResponse:(NSString *)message toQuery:(NSString *)messageid { }
*/
}

- (void) _poll {
// NSLog(@"IOSMQ: poll" );
NSString *polled = [self.webview stringByEvaluatingJavaScriptFromString:@"IOSMQ._backendPollString()"];
// NSLog(@"IOSMQ: polled %@", polled );
if( [polled hasPrefix:@"{"] ) {
NSLog(@"IOSMQ: polled json: %@", polled );
// NSLog(@"IOSMQ: polled json: %@", polled );
// probably json...
id jsonroot = [polled JSONValue];
if( jsonroot != nil) {
NSLog(@"jsonroot=%@",jsonroot);
// NSLog(@"jsonroot=%@",jsonroot);
IOSMQMessage *msg = [IOSMQMessage message];
id _id = [[jsonroot objectForKey:@"id"] retain];
id _msg = [[jsonroot objectForKey:@"message"] retain];
NSLog(@"IOSMQ: json id = %@", _id );
NSLog(@"IOSMQ: json message = %@", _msg );
// NSLog(@"IOSMQ: json id = %@", _id );
// NSLog(@"IOSMQ: json message = %@", _msg );
msg.id = _id;
msg.message = _msg;
if( delegate != nil )
[delegate queue:self message:msg];
[_id release];
[_msg release];
// [msg release];
// [jsonroot release];
}
}
}
Expand All @@ -144,7 +105,7 @@ - (void) start {
NSLog(@"IOSMQ: start" );
// hook events
[self.webview setDelegate:self];
self->timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(_poll) userInfo:nil repeats:YES];
self->timer = [NSTimer scheduledTimerWithTimeInterval:self.pollInterval target:self selector:@selector(_poll) userInfo:nil repeats:YES];
}

- (void) dealloc {
Expand All @@ -164,8 +125,7 @@ - (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"IOSMQ: did start load");

NSLog(@"Injecting IOSMQ...");
if( nextDelegate != nil )
if( nextDelegate != nil )
[nextDelegate webViewDidStartLoad:webView];
}

Expand All @@ -175,10 +135,9 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"IOSMQ" ofType:@"js"];
if( filepath != nil ){
// NSLog(@"resource path: %@", filepath);
NSString *js = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
if( js != nil ){
// NSLog(@"javascript: %@", js);
NSLog(@"Injecting IOSMQ JavaScript..."); // too late for onload to work though :/
[webView stringByEvaluatingJavaScriptFromString:js];
}
}
Expand All @@ -190,28 +149,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString* urlString = [[request URL] absoluteString];
NSLog(@"IOSMQ: request: %@", urlString);

/*
NSRange r = [urlString rangeOfString:@"/dummyapi/"];
if( r.location != NSNotFound ){
NSString *apicall = [urlString substringFromIndex:r.location+r.length];
NSLog(@"hijacked api call: %@", apicall);
NSString *json = [NSString stringWithFormat:@"{some:'json %@ data'}", urlString];
NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
[webView loadData:data MIMEType:@"text/plain" textEncodingName:@"utf-8" baseURL:nil];
return NO;
}
if ([urlString hasPrefix:@"http://internal.flyessence.com/api/"])
{
NSString *json = [NSString stringWithFormat:@"{some:'json %@ data'}", urlString];
NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
// [webView stringByEvaluatingJavaScriptFromString:@"alert('javascript called')"];
[webView loadData:data MIMEType:@"text/plain" textEncodingName:@"utf-8" baseURL:nil];
return NO;
}
*/


if( nextDelegate != nil )
return [nextDelegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];

Expand Down

0 comments on commit 8492bc2

Please sign in to comment.