Saturday, March 26, 2016

iOS use UIKeyboardTypeDecimalPad in UIWebView form input

Leave a Comment

I want to show the decimal pad with a '.' in the left corner used by native apps in a cordova based app.

enter image description here

I have seen a lot of threads using private APIs etc. but I want a solution that would go for app store submission. Any help is appreciated.

I have already tried something from this thread.

Below is my modification,

@protocol TSPingPong <NSObject> - (void) ts_pong: (id) sender; @end  @interface NSObject (TSPingPong) @end @implementation NSObject (TSPingPong) - (void) ts_ping: (id) sender {     if ( [sender respondsToSelector: @selector(ts_pong:)] )     {         [sender performSelector: @selector( ts_pong: ) withObject: self ];     } } @end   @interface ViewController () @property (weak, nonatomic) IBOutlet UIWebView *webView;  @end  @implementation ViewController  - (void)viewDidLoad {     [super viewDidLoad];      [[NSNotificationCenter defaultCenter] addObserver: self                                              selector: @selector(keyboardWillAppear:)                                                  name: UIKeyboardWillShowNotification                                                object: nil];      NSString* html = @"<br><br><br><form action=''> " \     "First name: <input type='text'><br> " \     "Last name: <input type='text'><br>" \     "<input type='submit' value='Submit'> " \     "</form>";      [_webView loadHTMLString: html                      baseURL: nil]; }  - (void) keyboardWillAppear: (NSNotification*) n {     // the keyboard is about to appear!     // play pingpong with the first responder so we can ensure it has a keyboardAppearance method:      [[UIApplication sharedApplication] sendAction: @selector( ts_ping:) // added via category extension                                                to: nil                  // to: the first responder                                              from: self                 // "sender"                                          forEvent: nil]; }  - (void) ts_pong: (id) sender {     // sender is the first responder.  Happens to be undocumented "UIWebBrowserView" in this case.      // if it doesn't have it's own keyboardAppearance method then let's add one:     if ( ![sender respondsToSelector: @selector(keyboardAppearance)] )     {         Method m = class_getInstanceMethod( [self class], @selector( keyboardAppearanceTemplateMethod ) );          IMP imp = method_getImplementation( m );          const char* typeEncoding = method_getTypeEncoding( m );          class_addMethod( [sender class], @selector(keyboardAppearance), imp, typeEncoding );      }      if ( ![sender respondsToSelector: @selector(keyboardType)] )     {         Method m1 = class_getInstanceMethod( [self class], @selector( keyboardTypeTemplateMethod ) );          IMP imp1= method_getImplementation( m1 );          const char* typeEncoding1 = method_getTypeEncoding( m1 );          class_addMethod( [sender class], @selector(keyboardType), imp1, typeEncoding1 );      } }  - (UIKeyboardAppearance) keyboardAppearanceTemplateMethod {     return UIKeyboardAppearanceDark; }   - (UIKeyboardType)keyboardTypeTemplateMethod {     return UIKeyboardTypeDecimalPad; } 

I am able to get the keyboard appearance to be dark but not the keyboard type which I desire.

Other threads and answers that I have seen so far but no results,

How the default keyboard comes up when user taps in UIWebView?

https://www.aerych.com/blog/2012/04/29/uiwebviews-uikeyboards-and-uitoolbars-oh-my/

How to set a custom keyboard for UIWebView

Add custom UIButton to UIKeyboard's accessory view for a UIWebView

https://github.com/dcorbatta/CKWebView

1 Answers

Answers 1

Try this in your html string

<input type="tel"></input> 

or

<input type="text" pattern="[0-9]*"></input> 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment