iOS architecture: from development to cross-terminal architect, realize cross-terminal development of iOS development and Android, and use a set of codes for multiple terminals.

The architecture of this article is developed by the author after years of exploration and understanding of the architecture, from theoretical basis to source code implementation, and gradually improved in the actual application of development projects, forming a set of project componentization, module independence, and complete The architecture solution of plug-in (hot update solution) has been realized. Especially the plug-in (hot update) solution will make people's eyes shine, yes 使iOS开发迅速转变为跨端开发,对移动端进行统一.

YLBDesign Open Source Architecture

YLBDesign project: github.com/ProBobo/YLB…

Why open source

In the author's opinion, the iOS architecture greatly increases the cost of mastering development because of the lack of information, so the popularity rate is not high. However, some large-scale projects more or less need to solve some problems of code reuse and module independence. In order to improve development efficiency, architecture is very important in the later stages of some project development. Therefore, the open source architecture has its practical significance.

For the iOS architecture, the author also made his own thinking: because 架构不是一种单一的技术或者组件,而是一系列组件、模块、资源、理论的综合体. Because the business code needs to be treated as a complete management object, the architecture is more like 一个代码、代码文件和资源的管理系统. So many times some open source libraries only implement some of the functions in the architecture, not all of them.

foreword

Here the author provides the iOS architecture open source code. Let those who want to master the iOS architecture save a few years of struggle, master it quickly, and realize cross-terminal development.

I believe that many people will find that there is no ready-made complete open source architecture solution in the process of exploring the iOS architecture. Basically, some components that can realize the architecture are open sourced, and there is no real solution that can perfectly solve project modularization and hot update. . Therefore, in many cases, the perfect architecture scheme is still in the theoretical stage.

Since the iOS architecture is not very popular, many people have not even heard of what the iOS architecture is, and they know nothing about componentization and modularization. When encountering a project that needs to master basic componentization capabilities, it will appear passive. However, mastering componentization is not an easy process, although it is only the beginning stage of the iOS architecture.

The most troublesome thing may be that you want to start the iOS architecture, but find that the relevant information is not very rich, and most of it is the content of the discussion. So the author deliberately open sourced the code.

Architecture components of YLBDesign

架构组件是构成架构的具体代码基础。 架构包括:架构组件、业务模块组件、资源等。

这里需要说明的是,架构组件只是架构理论的一部分,可以是自研的组件,也可以是开源的组件。作为架构应该可以支持同类型功能的不同组件,实现架构组件的替换,所以架构还会一直发展和丰富。作者尽量使用开源组件,因为作者相信现在的App基本都会使用开源库,因此可以实现无缝连接。当没有合适的开源组件可以使用时,作者会使用自研的开源组件。

可以选择的组件

模块通信:YLBModule、BeeHive
路由跳转:JLRoutes、MGJRouter
插件化(热更新):小程序(nativesupport.dcloud.net.cn/README)、React Native、自研小程序语言、自研跨端语言

YLBDesign使用的架构组件

为了使作者的架构理论能完全实现,作者在参考BeeHive的基础上自研了YLBModule模块通信。 建议大家使用作者在YLBDesign中使用的架构组件,这样可以确保和作者所实现的效果一致,以免出现效果上的不一致而困惑。 作者所建立的iOS架构支持线上修复App页面问题,不需要每次修复问题都上线AppStore,小程序在这方面有天然优势(后期会对这方面做详细描述)。

模块通信:YLBModule(github.com/ProBobo/YLB…
路由跳转:JLRoutes(github.com/joeldev/JLR…)、YLBDRouter(github.com/YuliboTeam/…
插件化(热更新):小程序(nativesupport.dcloud.net.cn/README

说明:这里只有小程序没有进行组件化封装,而是使用官方文档进行集成,主要是为了便于理解,和官方文档保持一致。当然,要对小程序进行Pod组件化封装也是能做到的。由于uni-app提供的小程序依赖包太大,有1.6G,上传Github受到限制,当然下载也会很消耗时间。所以作者提供了小程序的集成链接:nativesupport.dcloud.net.cn/README

为什么使用uni-app的小程序

引用牛顿的话:站在巨人的肩膀上。

作者受微信小程序和支付宝小程序启发,发现小程序作为App中web页面的替代品,支持在线更新,可以在架构中充当插件的角色,实现跨端开发。这时候又发现uni-app支持App集成小程序,所以采用了uni-app的小程序。按照uni-app的说法,uni-app先于微信开发出了小程序。

uni-app官网原话:很多人以为小程序是微信先推出的,其实,DCloud才是这个行业的开创者。

App小程序可以快速转化为微信小程序或者支付宝小程序等:小程序作为一个独立开发的语言,应用也非常广泛,掌握以后不仅可以在iOS、Android端使用,也可以独立开发小程序。uni-app还支持发布为前端web项目。

这是关于uni-app的描述:开发者编写一套代码,可发布到iOS、Android、Web(响应式)、以及各种小程序(微信/支付宝/百度/头条/飞书/QQ/快手/钉钉/淘宝)、快应用等多个平台。

1、有完整的开发文档,容易掌握。
2、一套代码,多端运行
2.1、支持在iOS端、Android端、H5端运行。
2.2、使用的是前端开发语言,可以进行跨端开发。
2.3、可以实现App小程序快速转化为微信、支付宝等主流小程序。
2.4、有独立的开发工具,可以独立修改小程序,无需编译iOS项目进行发版。
2.5、有自己的插件市场。
3、替代H5,优化用户体验。

模块通信:YLBModule

相关文章:iOS开源:模块通信YLBModule

YLBModule实现了模块通信以及模块消息回传。 每个模块都可以独立为一个App,模块和模块之间没有依赖关系。 为了使模块具有App的能力,我们需要给模块设计生命周期,模块需要有自己的AppDelegate,并且模块间的通信需要通过通信接口协议(protocol)实现。

我们需要在模块的AppDelegate里面注册通信接口协议,同时需要指定模块接口类。即在AppDelegate中需要使通信接口协议模块接口类相对应,这样通过通信接口协议就可以找到对应模块。

通信接口协议将会放在YLBDServices组件中。

YLBModule使用说明

注意:YLBModule依赖库在YLBDBasePod组件中建立依赖关系,对YLBDBasePod进行依赖即可对YLBModule进行依赖。

1、主工程YLBDesign引入YLBModule依赖库(即对YLBDBasePod进行依赖)。
2、在主工程的AppDelegate中引入YLBAppDelegate.h文件,并继承YLBAppDelegate

@import UIKit;
#import <YLBModule/YLBAppDelegate.h>

NS_ASSUME_NONNULL_BEGIN

@interface AppDelegate : YLBAppDelegate <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

NS_ASSUME_NONNULL_END
复制代码

在didFinishLaunchingWithOptions里面调用super方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    
    //super的位置决定当前AppDelegate的优先级是否最高,放在最底部表示当前AppDelegate优先级最高
    [super application:application didFinishLaunchingWithOptions:launchOptions];
    
    return YES;
}
复制代码

3、创建首页模块YLBDHome,并引入YLBModule依赖库。
4、在YLBDHome中创建YLBDHomeAppDelegate,用于管理模块生命周期。
5、创建YLBDServices组件,并创建YLBDHomeProtocol协议文件。
6、注册模块:实现模块生命周期

[[YLBModuleManager sharedInstance] registerModuleClass:[self class]];
复制代码

7、注册服务:通过服务协议可以找到服务类

[[YLBServiceManager sharedInstance] registerService:@protocol(YLBDHomeProtocol) implClass:NSClassFromString(@"YLBDHomeController")];
复制代码

8、通过服务协议(YLBDHomeProtocol)获取服务类(YLBDHomeController)

//
//  YLBDesignTabBarController.m
//  YLBDesign
//
//  Created by 余礼钵 on 2022/10/2.
//

#import "YLBDesignTabBarController.h"
#import "YLBDesignNavigationController.h"

#import <YLBCommon/YLBCommon.h>
#import <YLBModule/YLBServiceManager.h>
#import <YLBDServices/YLBDHomeProtocol.h>
#import <YLBDServices/YLBDMineProtocol.h>

@interface YLBDesignTabBarController ()<UITabBarControllerDelegate>
@property(nonatomic, strong) NSMutableArray *childVCArray;
@end

@implementation YLBDesignTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    
    [self setUpChildViewController];
}

#pragma mark - 设置tab
- (void)setUpChildViewController {
    id<YLBDHomeProtocol> homeVC = [[YLBServiceManager sharedInstance] createService:@protocol(YLBDHomeProtocol)];
    UIImage *homeSelectImage = [UIImage imageNamed:@"icon_tabbar_uikit_selected"];
    UIImage *homeNormalImage= [UIImage imageNamed:@"icon_tabbar_uikit"];
    [self setUpChildController:(UIViewController *)homeVC image:homeSelectImage selectImage:homeNormalImage title:@"首页"];
    
    
    id<YLBDMineProtocol> mineVC = [[YLBServiceManager sharedInstance] createService:@protocol(YLBDMineProtocol)];
    UIImage *mineSelectImage = [UIImage imageNamed:@"icon_tabbar_lab_selected"];
    UIImage *mineNormalImage= [UIImage imageNamed:@"icon_tabbar_lab"];
    [self setUpChildController:(YLBDesignNavigationController *)mineVC image:mineSelectImage selectImage:mineNormalImage title:@"我的"];
    
    self.viewControllers = self.childVCArray;
}

- (void)setUpChildController:(UIViewController *)vc image:(UIImage *)image selectImage:(UIImage *)selectImage title:(NSString *)title {
    
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = image;
    vc.tabBarItem.badgeValue = nil;
    vc.tabBarItem.selectedImage = selectImage;
    
    YLBDesignNavigationController *navi = [[YLBDesignNavigationController alloc] initWithRootViewController:vc];
    [self.childVCArray addObject:navi];
    
}

- (NSMutableArray *)childVCArray {
    if (!_childVCArray) {
        _childVCArray = [@[] mutableCopy];
    }
    return _childVCArray;;
}

@end

复制代码

业务模块组件

首页模块 YLBDHome

pod 'YLBDHome', :git =>'https://github.com/YuliboTeam/YLBDHome.git'
复制代码

个人中心模块 YLBDMine

pod 'YLBDMine', :git => 'https://github.com/YuliboTeam/YLBDMine.git'
复制代码

架构服务组件

pod 'YLBDServices', :git => 'https://github.com/YuliboTeam/YLBDServices.git'
复制代码

业务基础组件

YLBDBasePod

pod 'YLBDBasePod', :git => 'https://github.com/YuliboTeam/YLBDBasePod.git'
复制代码

使用的基础开源库

pod 'YLBCommon', :git =>'https://github.com/ProBobo/YLBCommon.git'
pod 'YLBProUI', :git => 'https://github.com/YuliboTeam/YLBProUI.git'
pod 'QMUIKit'
复制代码

关于小程序的开发

1、uni小程序的应用资源集成方式

nativesupport.dcloud.net.cn/UniMPDocs/U…

生成的 uni小程序 wgt 资源包可以部署到远程服务器动态下发也可以直接内置到工程中。

2、uni小程序自带胶囊按钮,如果想让小程序页面和原生一致,可以在pages.json文件中设置titleNView为false,这样小程序中就不会出现胶囊按钮。

{
            "path": "pages/order/order",
            "style": {
                "navigationBarTitleText": "",
                "app-plus": {
                    "titleNView": false,//禁用原生导航栏
                    "bounce":"none"
                    }
            }
}
复制代码

3、为了避免小程序出现版本检查提示框,需要设置manifest.json文件中的ignoreVersion为true

"compatible": {
    "ignoreVersion": true //true表示忽略版本检查提示框,HBuilderX1.9.0及以上版本支持  
}, 
复制代码

4、uni小程序使用的也是路由跳转,可以和App的路由跳转进行统一。App端的路由配置文件ClassName.json也可以部署到远程服务器动态下发也可以直接内置到工程中。

{
    "scheme":"YLDesign202210",
    "home":"YLBDHomeController",
    "mine":"YLBDMineController",
    "detail":"YLBDDetailController",
    
}
复制代码

所以,小程序页面和原生页面可以实现互换,在修复线上bug时可以节约发版时间。

作者:钵_Right
链接:www.jianshu.com/p/aa35cb7b4…
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Guess you like

Origin juejin.im/post/7150914629718245407