0

I am unable to call this swift file from a different app/framework as am getting the below error.

"Unknown class name 'WKScriptMessageHandler'"

This is the mySwift.swift which I have declared in a framework.

@objcMembers public class HSIWKJavascriptBridge : NSObject, WKScriptMessageHandler{
    public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "test", let messageBody = message.body as? String {
            print(messageBody)
        }
    }
}

swift compiler generated this code automatically while compiling.

@class WKUserContentController;
@class WKScriptMessage;

SWIFT_CLASS("_TtC5MyFwk21HSIWKJavascriptBridge")
@interface HSIWKJavascriptBridge : NSObject <WKScriptMessageHandler>
- (void)userContentController:(WKUserContentController * _Nonnull)userContentController didReceiveScriptMessage:(WKScriptMessage * _Nonnull)message;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

Now the error is WKScriptMessageHandler is not auto declared like

@class WKUserContentController;
@class WKScriptMessage;

due to which I am not able to use the framework.

Anand
  • 1,820
  • 2
  • 18
  • 25

1 Answers1

0

I tried with your same HSIWKJavascriptBridge swift class in my Project setup. I was able to use this class in my Obj-C files without any problem.

Since WKScriptMessageHandler is a protocol, it will not be forward declared as other classes (WKUserContentController, WKScriptMessage) in swift compiler generated code.

Please make sure import WebKit is added in your Swift file.

Anand
  • 1,820
  • 2
  • 18
  • 25