Skip to content
Browse files

added keyboard detection to ioswrapper

  • Loading branch information...
1 parent 830dfe1 commit a21c3ea3963a66b69de6c3f205d7f3ec32298d82 @jantuitman committed
View
3 ios_wrapper/ios_wrapper/CommandDispatcher.h
@@ -13,6 +13,9 @@
@interface CommandDispatcher : NSObject {
NSDictionary *commands;
}
+// called by webview if a command is coming in from the javascript side. The command is looked up and the execute method will be called.
- (void) processCommand: (NSString*) json forView: (UIWebView*) webview;
+// can be called by custom objective-c classes if an event must be passed through to javascript.
+- (void) postEvent: (NSString*) eventId withData: (NSDictionary*) data forView: (UIWebView*) webview;
@end
View
7 ios_wrapper/ios_wrapper/CommandDispatcher.m
@@ -74,5 +74,12 @@ - (void) processCommand: (NSString*) json forView: (UIWebView*) webview {
}
}
+- (void) postEvent:(NSString *)eventId withData:(NSDictionary *)data forView:(UIWebView *)webview {
+
+ NSString* javaScript=[NSString stringWithFormat: @"App5.ios_postBack('%@',%@)" , eventId,[JSONSerializer serialize: data]];
+ [webview stringByEvaluatingJavaScriptFromString: javaScript];
+
+}
+
@end
View
37 ios_wrapper/ios_wrapper/ios_wrapperViewController.m
@@ -39,7 +39,11 @@ - (void)viewDidLoad
webView.delegate = self;
dispatcher = [[CommandDispatcher alloc] init ];
-
+
+ NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
+ [nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
+ [nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
+
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
@@ -47,6 +51,37 @@ - (void)viewDidLoad
[webView loadRequest:requestObj];
}
+- (void) keyboardWillShow: (NSNotification*) item {
+ NSDictionary* info = [item userInfo];
+ CGRect kbRect = (CGRect) [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
+ CGRect screenCoords = [[webView window] convertRect:kbRect fromWindow: nil];
+ CGRect viewCoords= [webView convertRect:screenCoords fromView:nil];
+ NSLog(@"Keyboard will show at : %f,%f size %f,%f ",viewCoords.origin.x,viewCoords.origin.y,viewCoords.size.width,viewCoords.size.height);
+ NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSNumber numberWithFloat: viewCoords.origin.x], @"x",
+ [NSNumber numberWithFloat: viewCoords.origin.y], @"y",
+ [NSNumber numberWithFloat: viewCoords.size.width], @"width",
+ [NSNumber numberWithFloat: viewCoords.size.height], @"height",
+ nil];
+ [dispatcher postEvent:@"ios.keyboard.show" withData:d forView: webView];
+
+}
+- (void) keyboardWillHide: (NSNotification*) item {
+ NSDictionary* info = [item userInfo];
+ CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue ];
+ CGRect screenCoords = [[webView window] convertRect:kbRect fromWindow: nil];
+ CGRect viewCoords= [webView convertRect:screenCoords fromView:nil];
+ NSLog(@"Keyboard will hide from : %f,%f size %f,%f ",viewCoords.origin.x,viewCoords.origin.y,viewCoords.size.width,viewCoords.size.height);
+ NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSNumber numberWithFloat: viewCoords.origin.x], @"x",
+ [NSNumber numberWithFloat: viewCoords.origin.y], @"y",
+ [NSNumber numberWithFloat: viewCoords.size.width], @"width",
+ [NSNumber numberWithFloat: viewCoords.size.height], @"height",
+ nil];
+ [dispatcher postEvent:@"ios.keyboard.hide" withData:d forView: webView];
+
+}
+
- (void)viewDidUnload
{
[super viewDidUnload];
View
3 ios_wrapper/www/index.html
@@ -1,5 +1,6 @@
<html>
<body>
- placeholder for index.html
+ placeholder for index.html<br/>
+ <textarea rows="40" cols="80" />
</body>
</html>

0 comments on commit a21c3ea

Please sign in to comment.
Something went wrong with that request. Please try again.