美文网首页组件化
iOS组件化过程中遇到的问题及解决方案

iOS组件化过程中遇到的问题及解决方案

作者: zgsddzwj | 来源:发表于2019-04-22 16:26 被阅读83次

    一:资源包问题及组件iconfont使用

    1、第一种podspec资源的配置

    s.resources = 'FN_FNYongNeng_Login/Assets/*'
    

    资源读取方式

    NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"FN_FNYongNeng_Login" ofType:@"ttf"];
    UIImage *login_user = [UIImage imageWithIconFontComponent:FNIconFontComponentTTFMake(@"\U0000e60e", 24, HexRGB(0x333333), path)];
    
    

    2、第二种podspec资源的配置

    s.resource_bundles = {
    'FN_FNYongNeng_Login' => ['FN_FNYongNeng_Login/Assets/*']
    }
    

    资源读取方式

    NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"FN_FNYongNeng_Login" ofType:@"ttf" inDirectory:@"FN_FNYongNeng_Login.bundle"];
    
    UIImage *login_user = [UIImage imageWithIconFontComponent:FNIconFontComponentTTFMake(@"\U0000e60e", 24, HexRGB(0x333333), path)];
    
    

    二:组件的CTMediator入参及出参

    入参、出参全是NSDictionary

    /**
     创建登录
     
     @param params 入参
     @param callBackBlock block回调
     
     @return 日历控制器
     */
    - (UIViewController *)CTMediator_FNYongNengLoginVC:(NSDictionary *)params  callBackBlock:(void (^)(NSDictionary *dic))callBackB
    
    
    
    
    - (UIViewController *)CTMediator_FNYongNengLoginVC:(NSDictionary *)params  callBackBlock:(void (^)(NSDictionary *dic))callBackBlock {
        
        
        NSMutableDictionary *tmpParams = [NSMutableDictionary dictionary];
        tmpParams[kFNLoginVC_LoginSuccessBlock] = callBackBlock;
        
        UIViewController *viewController = [self performTarget:kCTMediatorTarget_FNLoginViewController
                                                        action:kCTMediatorAction_nativeFetchFNLoginViewController
                                                        params:tmpParams
                                             shouldCacheTarget:NO];
        
        if ([viewController isKindOfClass:[UIViewController class]]) {
            // view controller 交付出去之后,可以由外界选择是push还是present
            return viewController;
        } else {
            // 这里处理异常场景,具体如何处理取决于产品
            NSLog(@"%@ 未能实例化页面", NSStringFromSelector(_cmd));
            return [[UIViewController alloc] init];
        }
    }
    
    
    

    三:组件使用podspec配置层级目录

    组件内部文件默认是不在文件夹下的,即使在本地Development Pods中看到了文件夹,用户通过pod引入时还是看不到。要想实现实体文件夹,需要修改subspec的文件目录层次。

    根目录是s,使用s.subspec设置子目录,这里设置子目录为ss

    s.source_files = 'FN_FNFanNeng_DataSummary/Classes/*.*'
    s.subspec 'Mediator' do |ss|
        ss.source_files = 'FN_FNFanNeng_DataSummary/Classes/Mediator/*.*'
        end
    s.subspec 'MonthlyData' do |ss|
        ss.source_files = 'FN_FNFanNeng_DataSummary/Classes/MonthlyData/*.*'
        end
    s.subspec 'Views' do |ss|
        ss.source_files = 'FN_FNFanNeng_DataSummary/Classes/Views/*.*'
        end
    s.resource = 'FN_FNFanNeng_DataSummary/Assets/*'
    

    注意:需要把s.source_files最后改为 /*.*

    四:Bundle中的图片命名

    Bundle中的图片命名:如果图片命名为640x1136,则真机无法加载,模拟器无影响,需改为640*1136

    五:保持Podfile中的iOS版本和podspec版本统一

    Podfile文件中 :

    platform :ios, ‘9.0’

    Podspec文件中:

    s.ios.deployment_target = '9.0'

    要始终保持这两个版本一致,不然pod install会出错。

    六:include of non-modular header inside framework module

    在封装振动采集VibrationAcquistion框架时,更新到 Cocoapods 的时候出现一个错误,核心语句是 error: include of non-modular header inside framework module,在使用 Swift 的库时会出现这个问题,把 Target 下 Build SettingsAllow Non-modular includes in Framework Modules 设为 Yes 就解决了。

    七:Bitcode问题

    如果组件内直接或间接接入EZOpenSDKFramework,会引起以下报错:

    ld: '~/Pods/EZOpenSDK/dist/EZOpenSDK/dynamicSDK/EZOpenSDKFramework.framework/EZOpenSDKFramework' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. file '~/Pods/EZOpenSDK/dist/EZOpenSDK/dynamicSDK/EZOpenSDKFramework.framework/EZOpenSDKFramework' for architecture arm64

    这是因为EZOpenSDKFramework是关闭Bitcode的,所以我们如果要用这个Framework的话,就必须把项目的Bitcode关闭。可以在Podfile中添加以下语句,把所有Pod中的Bitcode都关闭。

    post_install do |installer|
        installer.pods_project.targets.each do |target|
            target.build_configurations.each do |config|
                config.build_settings['ENABLE_BITCODE'] = 'NO'
            end
        end
    end 
    

    八:组件中引用WKWebView的问题

    (1)如果某个VC中引用了WKWebView,在iOS9系统下点击返回按钮返回上级页面的时候会造成crash.

    -[FNWebViewController retain]: message sent to deallocated instance 0x7fa1974a33f0
    

    因为 WKWebView 的 UIScrollView 代理惹的祸,因为我需要实时监听网页的滚动区域来处理一些事情,所以我把 WKWebView.scrollView.delegate 设置为当前控制器。
    只要在VC的dealloc方法中把delegate置为nil就好了。

    - (void)dealloc {
        self.webView.scrollView.delegate = nil; // Fix iOS9 crash.
    }
    

    (2)如果引用WKWebView的VC中还有大标题,那么在iOS12上会出现偏移量的问题。WKWebView会默认有一个偏移量,最终展示为小标题+Webview。
    解决方式:需要在webView加载完毕后重置一下偏移量

      -(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
    {
       [FNProgressHUD dismissForView:self.view];
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           //数据加载完成再设置代理,避免标题闪一下
           webView.scrollView.delegate = self;
           //重新设置一下contentoffset,修复WKWebView在iOS12上导航栏偏移的问题
           CGFloat bigTitleHeight = self.dynamicNavView.bigTitleView.frame.size.height;
           [webView.scrollView setContentOffset:CGPointMake(0, - bigTitleHeight) animated:NO];
       });
    }
    

    webView.scrollView.delegate = self也要放到didFinishNavigation中,避免大标题会闪动

    相关文章

      网友评论

        本文标题:iOS组件化过程中遇到的问题及解决方案

        本文链接:https://www.haomeiwen.com/subject/mrcugqtx.html