Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Swift framework to Obj-C

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?

like image 251
XenixDANA Avatar asked Jun 28 '26 03:06

XenixDANA


1 Answers

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

like image 185
Arun Avatar answered Jun 29 '26 20:06

Arun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!