Wkwebview Auto Eject keyboard and hide Input satellite view (Inputaccessoryview)

Source: Internet
Author: User

Auto Eject keyboard

In UIWebView, there is a property keyboarddisplayrequiresuseraction, when set to No, you can eject the keyboard directly when the page is just loaded; in Wkwebview, there is no such property, if you want to implement similar function, You must replace the corresponding method in Wkwebview, the code is as follows:

static void (*ORIGINALIMP) (id self, SEL _cmd, void* arg0, bool arg1, BOOL arg2, id arg3) = NULL;

void Interceptimp (id self, SEL _cmd, void* arg0, bool arg1, BOOL arg2, id arg3) {
    originalimp (self, _cmd, arg0, TRUE, Arg2, ARG3);

+ (void) Wkwebviewshowkeybord {
    static dispatch_once_t Oncetoken;
    Dispatch_once (&oncetoken, ^{
        Class cls = nsclassfromstring (@ "Wkcontentview");
        SEL originalselector = nsselectorfromstring (@ "_startassistingnode:userisinteracting:blurpreviousnode:userobject:" );
        Method Originalmethod = Class_getinstancemethod (CLS, originalselector);
        Imp impovverride = (imp) interceptimp;
        Originalimp = (void *) method_getimplementation (originalmethod);
        Method_setimplementation (Originalmethod, impovverride);}
    ); 

Then call the function Wkwebviewshowkeybord, you can load the page directly display the keyboard, you need to pay special attention to this function can only call once, otherwise it will cause a loop call, the program crashes. Alternatively, you can add extended properties and methods to Wkwebview, and then set the properties directly, as follows: original content look here.

Typealias Closuretype = @convention (c) (Any, Selector, unsaferawpointer, BOOL, bool, any)-> Void extension {var Keyboardrequiresuserinteraction:bool {get {return false} set {if NE Wvalue = = true {setkeyboardrequiresuserinteraction ()}}} func Setkeybo Ardrequiresuserinteraction () {Let sel:selector = Sel_getuid ("_startassistingnode:userisinteracting:blurpreviousn
        Ode:userobject: "Let Wkcontentview:anyclass = nsclassfromstring (" Wkcontentview ")! 
        Let method = Class_getinstancemethod (Wkcontentview, sel) Let Originalimp:imp = Method_getimplementation (method!) Let Original:closuretype = Unsafebitcast (Originalimp, to:ClosureType.self) let block: @convention (bloc k) (Any, unsaferawpointer, BOOL, BOOL, NO)-> Void = {(Me, arg0, arg1, Arg2, Arg3) in original (Me, SEL, AR G0, True, Arg2, ARG3)} Let Imp: IMP = Imp_implementationwithblock (block) method_setimplementation (method!, IMP)}} 

Although the keyboard appears, but there is another problem, that is the keyboard inputaccessoryview, the following figure:





Click Done, you can hide the keyboard, but now I do not want to show the Inputaccessoryview, then how to hide, the code is as follows:

Final class Fauxbarhelper:nsobject {@objc var inputaccessoryview:anyobject?
        
        {return nil} func Removeinputaccessoryview (Webview:wkwebview) {var targetview:uiview? = Nil For view in WebView.scrollView.subviews {if String (Describing:type (Of:view)). Hasprefix ("Wkcontent"  {TargetView = view}} Guard Let target = TargetView else {return Let Noinputaccessoryviewclassname = "\ (target.superclass!) _noinputaccessoryview "var newclass:anyclass? = Nsclassfromstring (noinputaccessoryviewclassname) if Newclass = = Nil {let Targetclass:anyclass = ob
            Ject_getclass (target)! Newclass = Objc_allocateclasspair (Targetclass, noinputaccessoryviewclassname.cstring (USING:STRING.ENCODING.ASCII) !, 0)} Let Originalmethod = Class_getinstancemethod (fauxbarhelper.self, #selector (getter:fauxba Rhelper.inputaccessoryvieW)) Class_addmethod (newclass!. Self, #selector (Getter:FauxBarHelper.inputAccessoryView), Method_getimplementation (originalmethod!), Method_
        Gettypeencoding (originalmethod!))
    Object_setclass (target, newclass!)
 }
}
Now just create the Fauxbarhelper class instance object, and then call the Removeinputaccessoryview method, pass in the corresponding WebView, the code original address, if you need OC code, you can see here and here.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.