InterfaceKit
`One line of code` to implement interfaces of `UIKit,AppKit,and WatchKit` in `SwiftUI` interface! InterfaceKit sufficiently encapsulates UIViewRepresentable, UIViewControllerRepresentable,NSViewRepresentable,NSViewControllerRepresentable,and WKInterfaceObjectRepresentable. `一行代码`实现在`SwiftUI`界面使用`UIKit、AppKit和WatchKit`的接口!InterfaceKit充分封装了UIViewRepresentable, UIViewControllerRepresentable,NSViewRepresentable,NSViewControllerRepresentable和 WKInterfaceObjectRepresentable.
Install / Use
/learn @adong666666/InterfaceKitREADME
InterfaceKit
========================
<a href="https://github.com/apple/swift-package-manager" alt="Swift Package Manager Compatible" title="InterfaceKit on Swift Package Manager"><img src="https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg" /></a>
<img src="https://img.shields.io/badge/platforms-iOS%20%7C%20macOS%20%7C%20tvOS%20%7C%20watchOS%20%7C%20Linux-333333.svg" alt="Supported Platforms: iOS, macOS, tvOS, watchOS & Linux" />
<a href="https://zsd.name" target="_blank"><img src="https://github.com/adong666666/InterfaceKit/workflows/InterfaceKit%20CI/badge.svg?branch=master" alt="Build Status" /></a>
One line of code to implement interfaces of UIKit,AppKit,and WatchKit in SwiftUI interface!
Chinese (Simplified): 中文文档
Code interpretation document: https://github.com/adong666666/InterfaceKitDoc(or this repository's Docs)
The basic idea of InterfaceKit is that we want some user interface abstraction layer that sufficiently encapsulates actually calling UIKit,AppKit,and WatchKit directly. It should be simple enough that common things are easy, but comprehensive enough that complicated things are also easy.
You can check out more about the project direction in the vision document.
┌──────────────┐
│ SwiftUI │
└──────▲───────┘
│
┌──────────────────────────┴───────────────────────────┐
│ InterfaceKit │
└───────▲──────────────────▲───────────────────▲───────┘
│ │ │
┌───────┴──────┐ ┌──────┴───────┐ ┌──────┴───────┐
│ UIKit │ │ AppKit │ │ WatchKit │
└──────────────┘ └──────────────┘ └──────────────┘
- Features
- Usage
- Requirements
- Communication
- Status
- Installation
- FAQ
- Credits
- Donations
- Contributing
- License
- History
Features
- [x] use interface of UIKit in SwiftUI interface
- [x] use interface of AppKit in SwiftUI interface
- [x] use interface of WatchKit in SwiftUI interface
Usage
Universal
For iOS or tvOS:
- To present UIView.
InterfaceView(MyUIView())
- To present UIViewController.
InterfaceViewController(MyUIViewController())
For macOS:
- To present NSView.
NSInterfaceView(MyNSView())
- To present NSViewController.
NSInterfaceViewController(MyNSViewController())
For watchOS:
- To present WKInterfaceObject.
WKInterfaceView(MyWKInterfaceObject())
eg.
import SwiftUI
import InterfaceKit
struct MyInterfaceView: View {
var body: some View {
ZStack {
InterfaceViewController(MyViewController())
InterfaceView(MyView())
SwiftUIView()
}
}
}
#if DEBUG
struct MyInterfaceView_Previews: PreviewProvider {
static var previews: some View {
MyInterfaceView()
}
}
#endif
With Closure
You can do something while presenting SwiftUI view.
- For Example
InterfaceViewController(MyUIViewController.shared, {
print("Hello World")
MyUIViewController.shared.delegate = SomeViewControler.shared
MyUIViewController.shared.view.alpha = 0.5
MyUIViewController.shared.view.backgroundColor = .white
MyUIViewController.someFunction()
networkRequest()
JSONParsing()
downloadFile()
showProgress()
makeToast()
databaseOperation()
//do something
...
})
.navigationBarBackButtonHidden(false)
.navigationBarHidden(false)
.navigationBarTitle(I18n.localizedString("Title"), displayMode: .large)
Multiple platforms
InterfaceKit makes it clearer for multiple platforms programming.
- For Example
import SwiftUI
import MapKit
import InterfaceKit
let kStr = "Hello World"
#if os(iOS) || os(tvOS)
typealias OSViewController = UIViewController
typealias OSView = UILabel
typealias OSInterfaceView = InterfaceView
typealias OSInterfaceVC = InterfaceViewController
let kBounds = UIScreen.main.bounds as CGRect?
#elseif os(macOS)
typealias OSViewController = NSViewController
typealias OSView = NSTextField
typealias OSInterfaceView = NSInterfaceView
typealias OSInterfaceVC = NSInterfaceViewController
let kBounds = NSScreen.main?.frame
#endif
@main
struct EApp: App {
var body: some Scene {
WindowGroup {
ZStack {
#if !os(watchOS)
OSInterfaceView(MKMapView())
OSInterfaceView(MyView(), { print(kStr) })
OSInterfaceVC(MyVC())
#else
WKInterfaceView(WKInterfaceMap(), { print(kStr) })
#endif
Text(kStr).foregroundColor(.purple)
}
}
}
}
#if !os(watchOS)
class MyVC: OSViewController {
#if os(iOS) || os(tvOS)
override func viewDidLoad() {
let lbl = MyView()
lbl.textAlignment = .right
view.addSubview(lbl)
}
#elseif os(macOS)
override func loadView() { view = MyView() }
#endif
}
class MyView: OSView {
override init(frame: CGRect) {
super.init(frame: CGRect(x: 0, y: kBounds!.height / 2 - 60, width: kBounds!.width, height: 40))
#if os(iOS) || os(tvOS)
text = kStr
#elseif os(macOS)
stringValue = kStr
#endif
}
required init?(coder: NSCoder) { fatalError() }
}
#endif
Requirements
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
- Xcode 11+
- Swift 5.1+
Communication
- QQ Group: 1027277979
- If you'd like to contact me, use mail:3440217568@qq.com or QQ:3440217568 or WeChat:adongenjoylife or telephone:15674119605.
- If you found a bug, and can provide steps to reliably reproduce it, please open an issue.
- If you have a feature request, please open an issue.
- If you want to contribute, please submit a pull request.
Status
This project is actively under development. We consider it ready for production use.
Installation
Below is a table that shows which version of InterfaceKit you should use for your Swift version.
| Swift | InterfaceKit | | ----- | -------------- | | 5.X | >= 5.4.0 |
InterfaceKit supports multiple methods for installing the library in a project.
Copy to your project
Clone the repository by running the following command:
git clone https://github.com/adong666666/InterfaceKit.git --depth=1
Copy the Swift files in InterfaceKit folder to your project.
- If for iOS or tvOS project, you can copy the file
UIInterface.swift. - If for macOS project, you can copy the file
NSInterface.swift. - If for watchOS project, you can copy the file
WKInterface.swift.
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. If you have not installed CocoaPods, just install it with the following command:
$ gem install cocoapods
<br />
You need a `Podfile` to Integrate InterfaceKit into your Xcode project with CocoaPods. If you do not have a Podfile, just create one or use the Podfile provided in `PodfileExample` folder by this repository. Podfile is as follows.
# Podfile
source 'https://github.com/CocoaPods/Specs.git'
# platform:ios, '13.0'
use_frameworks!
inhibit_all_warnings!
target 'YOUR_TARGET_NAME' do
pod 'InterfaceKit'
end
# post_install do |installer_representation|
# installer_representation.pods_project.targets.each do |target|
# target.build_configurations.each do |config|
# config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
# end
# end
# end
Replace YOUR_TARGET_NAME with your project name.
To integrate InterfaceKit into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'InterfaceKit'
Maybe you have not update CocoaPods, then InterfaceKit may not be found, you can run pod update to update CocoaPods, or just run the following command.
pod 'InterfaceKit', :git => 'https://github.com/adong666666/InterfaceKit.git'
- If just for iOS or tvOS project, you can specify it in your
Podfile:
pod 'InterfaceKit/UIKit'
- If just for mac project, you can specify it in your
Podfile:
pod 'InterfaceKit/AppKit'
- If just for watchOS project, you can specify it in your
Podfile:
pod 'InterfaceKit/WatchKit'
- If you want to use
