目录
App间的唤起和通信
App跳转
通过Scheme唤起其他App
Universal Link
组件化
App间的唤起和通信
App跳转
·使用URL Scheme支持App启动、跳转及参数传递
·分享 / 登陆 / 拉起App Store等
设置URL Type
在UIApplication中处理参数和业务逻辑
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{//业务处理}
通过Scheme唤起其他App
通过Scheme判断App是否安装
使用UIApplication唤起App
//判断目标App是否安装
-(BOOL)canOpenURL:(NSURL *)url;// 通过点击拉起TestApp
-(void)viewClicked{NSURL *openURL = [NSURL URLWithString:@"TestApp://"];BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:openURL];[[UIApplication sharedApplication] openURL:openURL] options:nil completionHandler:^(BOOL success) {}];
}
Universal Link
·使用HTTP / HTTPS协议启动客户端
·Scheme重复 / Web和Native统一
·需要配合Web端进行注册
·在Safari / 微信 / 其他App中使用Universal Link
组件化
·组件化意义:
·适用于基础功能稳定、项目规模较大
·项目变大,编译时间长,基础模块的产品间复用
·多团队发布、集成、测试不便,协同开发相互依赖和冲突
·组件化拆分
·工具类 / 基础功能 / 基础UI控件 / 业务线 ...
·组件化通信选型
·常用的三种方案
·Target-Action
·抽离业务逻辑
·通过中间层进行调用
·中间层使用runtime反射
·中间层代码优化
·URL Scheme
·使用URL处理本地的跳转
·通过中间层进行注册&调用
·注册无需使用反射
·Protocol - Class
·增加Protocol Wrapper层
·中间件返回Protocol对应的Class
·解决硬编码问题
//
// GSCMediator.h
// GSCApp1
//
// Created by gsc on 2024/6/17.
//#import <UIKit/UIKit.h>
#import "Foundation/Foundation.h"NS_ASSUME_NONNULL_BEGIN@protocol GSCDetailViewControllerProtocol <NSObject>- (__kindof UIViewController *)detailViewControllerWithUrl:(NSString *)detailUrl;@end@interface GSCMediator : NSObject//target-action
+(__kindof UIViewController *)detailViewControllerWithUrl:(NSString *)detailUrl;//urlScheme
typedef void(^GSCMediatorProcessBlock)(NSDictionary *params);
+(void)registerScheme:(NSString *)scheme processBlock:(GSCMediatorProcessBlock)processBlock;
+(void)openUrl:(NSString *)url params:(NSDictionary *)params;//protocol class
+(void)registerProtocol:(Protocol *)protocol class:(Class)class;
+(Class)classForProtocol:(Protocol *)protocol;@endNS_ASSUME_NONNULL_END
//
// GSCMediator.m
// GSCApp1
//
// Created by gsc on 2024/6/17.
//#import "GSCMediator.h"@implementation GSCMediator#pragma mark target-action
+(__kindof UIViewController *)detailViewControllerWithUrl:(NSString *)detailUrl{Class detailClass = NSClassFromString(@"GSCDetailViewController");UIViewController *detailController = [[detailClass alloc] performSelector:NSSelectorFromString(@"initWithUrlString:") withObject:detailUrl];return detailController;
}#pragma mark url scheme
+(NSMutableDictionary *)mediatorCache{static NSMutableDictionary *cache;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{cache = @{}.mutableCopy;});return cache;
}+(void)registerScheme:(NSString *)scheme processBlock:(GSCMediatorProcessBlock)processBlock{if(scheme && processBlock){[[[self class] mediatorCache] setObject:processBlock forKey:scheme];}
}+(void)openUrl:(NSString *)url params:(NSDictionary *)params{GSCMediatorProcessBlock block = [[[self class] mediatorCache] objectForKey:url];if(block){block(params);}
}#pragma mark protocol class
+(void)registerProtocol:(Protocol *)protocol class:(Class)class{if(protocol && class){[[[self class] mediatorCache] setObject:class forKey:NSStringFromProtocol(protocol)];}
}
+(Class)classForProtocol:(Protocol *)protocol{return [[[self class] mediatorCache] objectForKey:NSStringFromProtocol(protocol)];
}@end