1

I saw there are many questions about Swift and Objective-C bridge including this one, this one, and this one.... But none of them helped me so far.

When adding WKNavigationDelegate and WKScriptMessageHandler, compiler fails with message "Cannot find protocol declaration".

Project-Brigding-Header.h

#import <WebKit/WebKit.h>
#include "xyz.h"

I have added WebKit.framework to Link Binary With Libraries under target > Build Phases. And made changes to build settings.

Defines Module : YES
Always Embed Swift Standard Libraries : YES
Install Objective-C Compatibility Header : YES

Do I have to do anything else to make it work?

s-t
  • 325
  • 4
  • 13

1 Answers1

2

Why you are using #import <WebKit/WebKit.h> in the bridging header?
Just add in a top of swift's file:

import WebKit 

The bridging header only for application's inner classes which written with Objective-C.
WebKit is a module (in swift's term).
P.S. Also remove #import from the bridging header.

Andrew Romanov
  • 4,058
  • 2
  • 21
  • 36
  • Trying to use WKWebkit-related variables etc from Objective-C/C++ code. But obviously importing WebKit.h isn't the solution.. – s-t Jul 02 '19 at 01:07
  • I've created example project and do not have problems with importing of WebKit. In Objective-C++ I use '#import ', in Swift file I use 'import WebKit'. All works fine. Can you add code of your imports and code where you are trying to initiate WKWebView? – Andrew Romanov Jul 02 '19 at 01:48
  • Perhaps there is circular reference or something. It's an old old project someone else wrote in ObjC and using C library. After looking at it since posting question, I think calling webview method through variable stored in AppDelegate is not a great idea, and that's where compiler can't find the protocol for WKWebView. – s-t Jul 02 '19 at 02:36
  • 1
    Thanks @Andrew I'll refactor so that I don't need to call webview method. – s-t Jul 02 '19 at 02:38