2015-09-08 44 views

回答

3

挖成的Webkit源了幾個星期後,我設法通過混寫上WKContentView_startAssistingNode:userIsInteracting:blurPreviousNode:userObject並重寫userIsInteracting值來獲得在iOS 9這個工作:

僞代碼:

swizzle_intercept("WKContentView", "_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:", &hackAssist); 

void hackAssist (id self, SEL _cmd, void* arg0, BOOL arg1, BOOL arg2, id arg3) { 
    ((void (*)(id,SEL,void*,BOOL,BOOL,id))swizzle_interceptee(hackAssist))(self, _cmd, arg0, TRUE, arg2, arg3); 
} 

乾杯!

+0

那麼Swift相當於什麼呢? – Osvaldas

+0

在iOS 10上這是導致鍵盤出現,爲什麼? –

+0

@PauloCesar你還有最新的beta版嗎? –

0

我寫了一個擴展名(在斯威夫特3 * WKWebView類,增加了keyboardDisplayRequiresUserAction的計算性能,就像在一個UIWebView

你可以找到完整的解決方案in this blog,或者這裏是一個快速片段:

指的是蘋果的WebKit的官方開源文檔後,我想出了以下運行時混寫:

func setKeyboardRequiresUserInteraction() { 

    let sel: Selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode: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(block) (Any, UnsafeRawPointer, Bool, Bool, Any) -> Void = {(me, arg0, arg1, arg2, arg3) in 

    original(me, sel, arg0, true, arg2, arg3) 

} 

let imp: IMP = imp_implementationWithBlock(block) 

method_setImplementation(method, imp) 

} 
+0

至少在iOS 10和11中,這完全沒有任何作用。 – kevin

1

接受的答案不再是iOS的下工作11.3,因爲WebKit方法簽名已經改變。這是一個解決方法(在Obj-C中):

#import <objc/runtime.h> 

@implementation WebViewInjection 

+ (void)allowDisplayingKeyboardWithoutUserAction { 
    Class class = NSClassFromString(@"WKContentView"); 
    NSOperatingSystemVersion iOS_11_3_0 = (NSOperatingSystemVersion){11, 3, 0}; 

    if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion: iOS_11_3_0]) { 
     SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:changingActivityState:userObject:"); 
     Method method = class_getInstanceMethod(class, selector); 
     IMP original = method_getImplementation(method); 
     IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, BOOL arg3, id arg4) { 
      ((void (*)(id, SEL, void*, BOOL, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3, arg4); 
     }); 
     method_setImplementation(method, override); 
    } else { 
     SEL selector = sel_getUid("_startAssistingNode:userIsInteracting:blurPreviousNode:userObject:"); 
     Method method = class_getInstanceMethod(class, selector); 
     IMP original = method_getImplementation(method); 
     IMP override = imp_implementationWithBlock(^void(id me, void* arg0, BOOL arg1, BOOL arg2, id arg3) { 
      ((void (*)(id, SEL, void*, BOOL, BOOL, id))original)(me, selector, arg0, TRUE, arg2, arg3); 
     }); 
     method_setImplementation(method, override); 
    } 
} 

@end