IMBeeHive is a kind of modular programming method

Overview

概述

IMBeeHive是用于iOS的App模块化编程的框架实现方案,本项目主要借鉴了阿里巴巴BeeHive,在此基础上通过逆向了一些大厂的APP使得功能更加强大完善。同时现在也在寻找一起开发这个框架的开发者,如果您对此感兴趣,请联系我的微信:alvinkk01.

背景

随着公司业务的不断发展,项目的功能越来越复杂,各个业务代码耦合也越来越多,解耦合和业务组件化(或者叫模块化)也迫在眉睫。 在网上组件化的文章很多,主要方案有三种:1.protocol-class 2.target-action 3.route,后来通过逆向一些大厂的app,发现这些组件化(或者叫模块化)的技术在大厂使用率挺高的,基本上也是基于这三种方案,但各家都是不同的实现方式,于是乎就萌生了开发一个组件化框架的想法,开发者可以根据自己的喜好选择自己喜欢的方案实现解耦和业务组件化,而不需要重复造轮子。

yy直播
image
抖音
2
网易云音乐
3

完整DEMO

https://github.com/DebugAlvin/IMAudioLive

项目特色

组件管理生命周期
组件间通信
使用protocol-class方案service无需注册
支持route(计划更新)

Installation

pod "IMBeeHive"

初始化

一.首先我们需要在主工程添加一个配置文件,这里我添加的是IMBeeHive.bundle/IMBeeHive.plist,通过配置plist我们可以在启动时注册所有的Moudle,实际开发的项目Moudle并不会很多,我目前开发的项目大概也就8个Moudle,IMLuanchMoudle、IMHomeMoudle、IMDynamicMoudle、IMChatMoudle、IMMineMoudle...,所以在推荐在plist里面统一注册Moudle image

二.在主主工程加入__attribute__((constructor)),通常app启动流程为:
1.所有Framework的+load方法
2.所有Framework的c++构造方法
3.主程序的+load方法
4.主程序的c++构造方法
我们在主程序Appdelegate之前做初始化,IMBeeHive才可以通过HOOK + NSInvocation方式让已经注册的Moudle管理Appdelegate的生命周期

__attribute__((constructor)) void loadBeeHiveMoudle () {  
    [IMBeeHive shareInstance].context.configName = @"IMBeeHive.bundle/IMBeeHive";//可选,默认为IMBeeHive.bundle/IMBeeHive  
    [[IMBeeHive shareInstance] registerAll]; //从配置里面注册所有moudle或者service  
    [[IMBeeHive shareInstance] setupAll]; //设置ioc容器里面所有的对象  
}  

创建Moudle

一.使用cocopods创建Moudle

首先cd到主程序目录,然后执行命令pod lib create xxxxx

pod lib create IMLuanchMoudle

将会询问以下内容:

1.What Language do you want to use?? [Swift / objC]
2.Would you like to include a demo application with your Library? [Yes / No]
3.Would you like to do view based testing? [Yes / No]
4.What is your class prefix?

创建成功后会打开Xcode
WeChat7ad9d59058808237942820048c7db9a3
Example可以作为我们的壳工程,平时对模块的开发和调试可以在这里进行。另外我们需要配置IMLuanchMoudle.podspec,具体方法请参考DEMO

新建一个IMLaunchModuleProtocol.h,值得注意的是我们必须遵循IMModuleProtocol协议

#import 
   
    
/**
 The services provided by Launch module to other modules
 */
@protocol IMLaunchModuleProtocol 
    
     

-(void)doSomeTings;

@end


    
   

新建一个IMLuanchMoudle.h和IMLuanchMoudle.m

@end NS_ASSUME_NONNULL_END ">
//IMLuanchMoudle.h
#import 
   
    

#import "IMLaunchModuleProtocol.h"

static UIWindow *mWindow = nil;

NS_ASSUME_NONNULL_BEGIN

@interface IMLuanchMoudle : NSObject
    
     

@end

NS_ASSUME_NONNULL_END  

    
   
//IMLuanchMoudle.m  
#import "IMLuanchMoudle.h"
#import "IMLuanchViewController.h"

@implementation IMLuanchMoudle

//每个Moudle必须实现shareInstance方法,编译器会提醒
+ (instancetype)shareInstance {
    static dispatch_once_t p;
    static id Instance = nil;
    dispatch_once(&p, ^{
        Instance = [[self alloc] init];
    });
    return Instance;
}

//如果我们当前的模块是主模块,我们可以实现didFinishLaunchingWithOptions设置rootViewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"[IMLuanchMoudle] --- [执行]");
    mWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    mWindow.backgroundColor = [UIColor whiteColor];
    [mWindow makeKeyAndVisible];
    [UIApplication sharedApplication].delegate.window = mWindow;
    mWindow.rootViewController = [[IMLuanchViewController alloc] init];
    return YES;
}

- (void)doSomeTings {
    NSLog(@"通过IMLaunchModuleProtocol协议调用IMLuanchMoudle的doSomeTings方法");
}


模块间的互相调用

首先我们需要在Protocol暴露方法

#import 
   
    
#import 
    
     

@protocol IMMineModuleProtocol 
     
      

- (UIViewController *)mainViewController;
-(void)doSomeTingsA;
-(void)doSomeTingsB;


@end

     
    
   

然后在Protocol的实现类实现这些方法

#import "IMMineModule.h"
#import "IMMIneViewController.h"

@implementation IMMineModule

+ (instancetype)shareInstance {
    static dispatch_once_t p;
    static id Instance = nil;
    dispatch_once(&p, ^{
        Instance = [[self alloc] init];
    });
    return Instance;
}

-(UIViewController *)mainViewController {
    IMMIneViewController *controller = [[IMMIneViewController alloc] init];
    return controller;
}

- (void)doSomeTingsA {
    NSLog(@"通过IMLaunchModuleProtocol协议调用IMLuanchMoudle的doSomeTingsA方法");
}

- (void)doSomeTingsB {
    NSLog(@"通过IMLaunchModuleProtocol协议调用IMLuanchMoudle的doSomeTingsB方法");
}


@end  

最后我们可以在任意模块通过Protocol调用实现类的方法

id
   
     pModule = IMGetBean(IMMineModuleProtocol);
[pModule doSomeTingsA];
[pModule doSomeTingsB];
UIViewController *controller = [pModule mainViewController];
[self.navigationController pushViewController:controller animated:YES];  

   
You might also like...
An unofficial logbook for bouldering at Mandala. Kind of a SwiftUI playground as well.
An unofficial logbook for bouldering at Mandala. Kind of a SwiftUI playground as well.

BoulderLogbook An unofficial boulder logbook for Dresden's boulder gym Mandala. Features When finished it should allow you to: log all your tops for a

RichEditorView is a simple, modular, drop-in UIView subclass for Rich Text Editing.
RichEditorView is a simple, modular, drop-in UIView subclass for Rich Text Editing.

RichEditorView RichEditorView is a simple, modular, drop-in UIView subclass for Rich Text Editing. Written in Swift 4 Supports iOS 8+ through Cocoapod

Modular and customizable Material Design UI components for iOS
Modular and customizable Material Design UI components for iOS

Material Components for iOS Material Components for iOS (MDC-iOS) helps developers execute Material Design. Developed by a core team of engineers and

Modular iOS with Uber needle & tuist example
Modular iOS with Uber needle & tuist example

Dodi Modular iOS with Uber needle & tuist example Setup brew install needle bash (curl -Ls https://install.tuist.io) and run make all Point of concer

Collection of Swift/iOS-related conference videos. A demo project for SuperArc framework - building modular iOS apps with a µComponent architecture.
Collection of Swift/iOS-related conference videos. A demo project for SuperArc framework - building modular iOS apps with a µComponent architecture.

SwiftCommunity Beta version is available at TestFlight Collection of Swift/iOS-related conference videos. This project serves as a showcase for the Su

Strucure: I used MVVM with Colusre binging modular architecture : Also I used openweathermap to get all information for current weather as it's easy t
Strucure: I used MVVM with Colusre binging modular architecture : Also I used openweathermap to get all information for current weather as it's easy t

Strucure: I used MVVM with Colusre binging modular architecture : Also I used openweathermap to get all information for current weather as it's easy t

Droar is a modular, single-line installation debugging window
Droar is a modular, single-line installation debugging window

Droar is a modular, single-line installation debugging window. Overview The idea behind Droar is simple: during app deployment stages, adding quick ap

A powerful, elegant, and modular animation library for Swift.
A powerful, elegant, and modular animation library for Swift.

MotionMachine provides a modular, powerful, and generic platform for manipulating values, whether that be animating UI elements or interpolating prope

ProductPage - An example project that shows how to build a product page in a modular way. SwiftUI practice

ProductPage An example project that shows how to build a product page in a modul

ESF modular ingestion tool for development and research.

ESFang This is a tool devised for modular consumption of EndpointSecurity Framework (ESF) events from the MacOs environment. This is my attempt to ove

Swift implementation of libp2p, a modular & extensible networking stack
Swift implementation of libp2p, a modular & extensible networking stack

Swift LibP2P The Swift implementation of the libp2p networking stack Table of Contents Overview Disclaimer Install Usage Example API Contributing Cred

Effortless modular dependency injection for Swift.

Inject Effortless modular dependency injection for Swift. Sometimes during the app development process we need to replace instances of classes or acto

Poi - You can use tinder UI like tableview method
Poi - You can use tinder UI like tableview method

Poi You can use tinder UI like tableview method Installation Manual Installation Use this command git clone [email protected]:HideakiTouhara/Poi.git Imp

Elegant Apply Style by Swift Method Chain.🌙
Elegant Apply Style by Swift Method Chain.🌙

ApplyStyleKit ApplyStyleKit is a library that applies styles to UIKit using Swifty Method Chain. Normally, when applying styles to UIView etc.,it is n

JSPatch bridge Objective-C and Javascript using the Objective-C runtime. You can call any Objective-C class and method in JavaScript by just including a small engine. JSPatch is generally used to hotfix iOS App.

JSPatch 中文介绍 | 文档 | JSPatch平台 请大家不要自行接入 JSPatch,统一接入 JSPatch 平台,让热修复在一个安全和可控的环境下使用。原因详见 这里 JSPatch bridges Objective-C and JavaScript using the Object

A handy collection of Swift method and Tools to build project faster and more efficient.

SwifterKnife is a collection of Swift extension method and some tools that often use in develop project, with them you might build project faster and

Trace Swift and Objective-C method invocations
Trace Swift and Objective-C method invocations

SwiftTrace Trace Swift and Objective-C method invocations of non-final classes in an app bundle or framework. Think Xtrace but for Swift and Objective

Varnam Input Method Engine for macOS. Easily type Indian languages on macOS

VarnamIME for macOS Easily type Indian languages on macOS using Varnam transliteration engine. Built at FOSSUnited's FOSSHack21. This project is a har

Elegant Apply Style by Swift Method Chain.🌙
Elegant Apply Style by Swift Method Chain.🌙

ApplyStyleKit ApplyStyleKit is a library that applies styles to UIKit using Swifty Method Chain. Normally, when applying styles to UIView etc.,it is n

Owner
null
Modular iOS with Uber needle & tuist example

Dodi Modular iOS with Uber needle & tuist example Setup brew install needle bash <(curl -Ls https://install.tuist.io) and run make all Point of concer

David Ha 30 Nov 16, 2022
Strucure: I used MVVM with Colusre binging modular architecture : Also I used openweathermap to get all information for current weather as it's easy t

Strucure: I used MVVM with Colusre binging modular architecture : Also I used openweathermap to get all information for current weather as it's easy t

Alaa Azab 0 Oct 7, 2021
An experimental functional programming language with dependent types, inspired by Swift and Idris.

Kara An experimental functional programming language with dependent types, inspired by Swift and Idris. Motivation Development of Kara is motivated by

null 40 Sep 17, 2022
Swift Programming Basics - Collections, Variables & Constants

Dicee What I learned in this module How to clone an existing Xcode project from GitHub. Create an app with behaviour and functionality. Create links b

null 0 Jan 9, 2022
Pexels API client library for the Swift programming language.

Pexels-Swift Pexels.com API client library for the Swift programming language. Overview This Swift Package is a wrapper for Pexels API to get access t

Lukas Pistrol 4 Sep 1, 2022
AREK is a clean and easy way to request any kind of iOS permission (with some nifty features 🤖)

AREK is a clean and easy to use wrapper over any kind of iOS permission written in Swift. Why AREK could help you building a better app is well descri

Ennio Masi 961 Dec 20, 2022
PrettyBorder is a SwiftUI package for managing an customized border and background at any kind of view.

PrettyBorder Description PrettyBorder is a SwiftUI package for managing an customized border and background at any kind of view. Preview of end result

Ahmet Giray Uçar 2 Oct 13, 2021
LBBottomSheet gives you the ability to present a controller in a kind of

LBBottomSheet Installation Swift Package Manager To install using Swift Package Manager, in Xcode, go to File > Add Packages..., and use this URL to f

Lunabee Studio 48 Dec 9, 2022
JustPeek is an iOS Library that adds support for Force Touch-like Peek and Pop interactions on devices that do not natively support this kind of interaction.

JustPeek Warning: This library is not supported anymore by Just Eat. JustPeek is an iOS Library that adds support for Force Touch-like Peek and Pop in

Just Eat 68 Apr 4, 2021
Kind of tired to need an Android Device on me, just to read manga, so here we are.

Dokusho Kind of tired to need an Android Device on me, just to read manga, so here we are. I am going to prioritize feature based on how I feel and no

Stephan Deumier 13 Oct 10, 2022