iOS在UIWebView表单输入

时间:2016-03-16 14:33:38

标签: ios cordova uiwebview

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

enter image description here

我已经看到很多线程使用私有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

2 个答案:

答案 0 :(得分:1)

在你的html字符串中尝试这个

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

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

答案 1 :(得分:1)