按照VIPER架构开发iOS应用程序。但却以非常简单的方式。
简单而优雅的Viper
我们都知道Viper很酷,但我们也知道它设置起来很困难。这个库旨在简化所有那些样板的过程。如果您还不知道Viper是什么,请查看: 用VIPER架构设计iOS应用程序 (objc.io)
安装
所需条件
- iOS 11.0+
- Swift 5.1+
- Xcode 11.0+
Swift包管理器(SPM)
您可以使用Xcode上的SPM轻松安装此框架。在Xcode中转到 文件 | Swift Packages | 添加包依赖...
并搜索 "http://github.com/ferranabello/Viperit"
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。
在项目 Podfile 中指定 Viperit
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!
pod 'Viperit'
Carthage
Carthage 是一个去中心化的依赖管理器,它为您构建依赖并提供二进制框架。
要使用 Carthage 将 Viperit 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "ferranabello/Viperit"
运行 carthage update
来构建框架,并将构建的 Viperit.framework
拖入到您的 Xcode 项目中。
特性
使用 Xcode 模板轻松创建模块
Viperit Xcode 模板可以从此 最新发布 页面下载。下载 Templates.zip 文件。
要安装它们,解压缩文件,打开终端并运行
cd PATH/TO/UNZIPPED/FOLDER
mkdir -p ~/Library/Developer/Xcode/Templates/
cp -R Viperit ~/Library/Developer/Xcode/Templates/
如果您想自动为您创建 storyboard 文件,则可以勾选“Also create a Storyboard file for module”。选择“Universal”以使用一个视图同时适用于手机和平板,或者如果您想为平板设备使用一个独立的视图,则选择“Dedicated Tablet View”。
使用 storyboard、xib、程序视图或 SwiftUI
任何 Viperit 模块都会假设其视图默认从 storyboard 加载。但是,您可以使用 storyboards、nibs、代码 或甚至 SwiftUI 视图!您需要做的只是覆盖您模块枚举中的变量 viewType。
enum MySuperCoolModules: String, ViperitModule {
case theStoryboardThing
case oldSchool
case xibModule
case whatTheSwift
var viewType: ViperitViewType {
switch self {
case .theStoryboardThing: return .storyboard
case .oldSchool: return .code
case .xibModule: return .nib
case .whatTheSwift: return .swiftUI
}
}
}
内置路由函数
这个框架非常灵活,旨在以任何您希望的方式使用,但它路由器中自带一些实用的功能
//Show your module as the root view controller of the given window
func show(inWindow window: UIWindow?, embedInNavController: Bool, setupData: Any?, makeKeyAndVisible: Bool)
//Show your module from any given view controller
func show(from: UIViewController, embedInNavController: Bool, setupData: Any?)
//Show your module inside another view
func show(from containerView: UIViewController, insideView targetView: UIView, setupData: Any?)
//Present your module modally
func present(from: UIViewController, embedInNavController: Bool, presentationStyle: UIModalPresentationStyle, transitionStyle: UIModalTransitionStyle, setupData: Any?, completion: (() -> Void)?)
易于测试
您可以通过注入模拟层来测试您的模块,如下所示
var module = AppModules.home.build()
view = HomeMockView()
view.expectation = expectation(description: "Test expectation")
module.injectMock(view: view)
...
用法
现在,让我们创建第一个名为 "myFirstModule" 的 Viperit 模块!
0. 创建模块文件
让我们使用提供的 Xcode 模板来轻松地创建模块所需的所有类。只需在文档面板中点击 新建文件,然后在 "Viperit" 下的 协议导向模块、面向对象模块 或 SwiftUI 模块 之间进行选择。
1. 创建模块枚举
您至少需要一个(您可以使用尽可能多的枚举,也许还可以按照功能来分组模块)实现 ViperitModule 协议的枚举来枚举您的应用程序模块。
import Viperit
//MARK: - Application modules
enum AppModules: String, ViperitModule {
case myFirstModule
}
2. 构建模块并执行导航
想象这是一个新应用,我们想要加载 "myFirstModule" 模块作为应用的启动模块
import Viperit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let module = AppModules.myFirstModule.build()
let router = module.router as! MyFirstModuleRouter
router.show(inWindow: window)
return true
}
}
这只是一个示例,当然你可以使用自己的路由函数代替提供的 show(inWindow:)
window = UIWindow(frame: UIScreen.main.bounds)
let module = AppModules.myFirstModule.build()
let router = module.router as! MyFirstModuleRouter
router.mySuperCoolShowFunction(inWindow: window)
2.1. 你在使用 SwiftUI 吗?
假设你创建了一个基于 SwiftUI 的名为 'Cool' 的模块。你所要做的是使用新的 Viperit SwiftUI 模块构建器
import SwiftUI
import Viperit
//A sample function that could be implemented in some Router to show your Cool SwiftUI module
//You can even inject an @EnvironmentObject view model to your SwiftUI view.
func showTheCoolModule() {
let module = AppModules.cool.build { presenter -> (CoolView, UserSettings) in
let p = presenter as! CoolPresenterApi
let settings = p.settings()
return (CoolView(presenter: p), settings)
}
let router = module.router as! CoolRouter
router.show(from: viewController)
}
查看示例应用以便更好地理解其工作原理以及如何在 SwiftUI 中使用 presenter 管理数据流。
3. 遵循 Viper 流程
使用 Viper 方式,一切准备就绪来制作伟大的事情!克隆仓库并运行 'Example' 目标来查看实际效果!或者用 Cocoapods 尝试它
pod try Viperit
作者
许可证
Viperit 在 MIT 许可证 下发布并由 Ferran Abelló 版权所有。