iOS use UIKeyboardTypeDecimalPad in UIWebView form input

ぐ巨炮叔叔 提交于 2019-12-10 16:14:20

问题


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

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:


Try this in your html string

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

or

<input type="text" pattern="[0-9]*"></input>



回答2:


There is a new plugin for this:
https://github.com/mnill/cordova-custom-keyboard



来源:https://stackoverflow.com/questions/36038710/ios-use-uikeyboardtypedecimalpad-in-uiwebview-form-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!