iOS在UIWebView表单输入中使用UIKeyboardTypeDecimalPad

我想用’.’显示小数点.在基于cordova的应用程序中的本机应用程序使用的左角.

我已经看到很多线程使用私有API等,但我想要一个可以提交应用程序商店的解决方案.任何帮助表示赞赏.

我已经尝试过 this 线程的东西.

以下是我的修改,

@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;
}

我能够让键盘外观变暗但不是我想要的键盘类型.

到目前为止我见过的其他主题和答案但没有结果,

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

最佳答案 在你的html字符串中尝试这个

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

要么

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