2020년 7월 27일 월요일

Using Swift framework in an Objective-c project

reference
https://marcinczenko.github.io/post/using-swift-frameworks-from-objective-c-target/


===============================================
=============== Swift Framework ===============
===============================================
Make a ExampleSwiftFramework framework. not Static Library
Add ExampleSwiftFramework.swift and put the code below

@objc
public class ExampleSwiftFrameworkClass : NSObject {

    let message: String

    @objc(init:)
    public init(withHelloMessage message: String) {
        self.message = message
    }

    @objc(sayHello)
    public func sayHello() -> String {
        return self.message
    }
}

TARGETS->Build Settings
Mach-O : 'Dynamic Library'

Build Options->Always Embeded Swift Standard Libraries : No
Packaging->Defines Module : Yes
Apple Clang-Language-Modules->Enable Modules(C and Objective-C) : Yes

next to the upper left 'stop' icon. click framework and edit scheme->Run->Build Configuration=> Release

Click Product->Build
and you can see the built framework on the project navigato.
Right click at the file->Show in Finder

===============================================
============= Objective-C project =============
===============================================
Make an objective-C project

TARGETS->General
1. add the swift framework into the 'Frameworks, Libraries, and Embedded Content'
2. and then you can see 'Framework' folder in the project navigator.
3. remove the framework from the 'Frameworks, Libraries, and Embedded Content'
4. drag the framework.framework folder into the 'Framework' in the project navigator

TARGETS -> Build Settings(All, Combined taps)
Build Option -> Always Embed Swift Standard Libraries : No

TARGETS -> Build Phases
Click '+' and add New Copy Files Phase category
add the swift framework what you need and keep the default option(Code Sign On Copy : checked, Copy only when installing : unchecked)

put the code below in -(void)viewDidLoad

ExampleSwiftFrameworkClass *exam = [[ExampleSwiftFrameworkClass alloc] init:@"Test"];
NSLog(@"%@", [exam sayHello]);

댓글 없음:

댓글 쓰기