Import Swift framework to Obj-C

XenixDANA

I have framework that written in Swift like this.

import Foundation
import WebKit
import ObjectiveC

public extension WKWebView {

    public func someFunc(_ completionHandler:@escaping (_ capturedImage: UIImage?) -> Void) {
        //Some code
    }
}

When I build the frameworks and import to Objective-C code that use Cocoapods for Dependency Manager. I Can't call above someFunc function. The error said this:

No visible @interface for 'WKWebView' declares the selector 'someFunc'

This is how I implement Swift framework in Objective-C:

#import <Foundation/Foundation.h>
#import <ProjectName-umbrella.h>
@implementation CapturerDefault
- (void)captureWebViewScreenWith:(UIView *)containerView
            andCompletionHandler:(void (^)(UIImage *))completion {
    WKWebView *webView = [self findWebViewInViewController:containerView];
    
    [webView someFunc: resultImage] //The error show here.
    }
}

What is wrong? Did i miss something?

Arun

I tried the following steps and it works for me:

  1. Don't import the swift file in your objc file like: #import "ExampleFile.swift" but use #import "ProjectName-Swift.h"
  2. Make sure you use @objc statements in your swift code that you want to import to objc

Swift file:

import WebKit

extension WKWebView {
    @objc public func someFunc() {
        
    }
}

Objective C file:

#import "ViewController.h"
#import "Sample-Swift.h"
#import <WebKit/WebKit.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    WKWebView *wbView = [[WKWebView alloc]init];
    
    [wbView someFunc];
}

Credits to: "Expected ';' after top level declarator" under Swift

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related