iOS組件化過程中遇到的問題及解決方案

一:資源包問題及組件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入?yún)⒓俺鰠?/h3>

入?yún)?、出參全是NSDictionary

/**
 創(chuàng)建登錄
 
 @param params 入?yún)? @param callBackBlock block回調(diào)
 
 @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 {
        // 這里處理異常場景,具體如何處理取決于產(chǎn)品
        NSLog(@"%@ 未能實例化頁面", NSStringFromSelector(_cmd));
        return [[UIViewController alloc] init];
    }
}


三:組件使用podspec配置層級目錄

組件內(nèi)部文件默認(rèn)是不在文件夾下的,即使在本地Development Pods中看到了文件夾,用戶通過pod引入時還是看不到。要想實現(xiàn)實體文件夾,需要修改subspec的文件目錄層次。

根目錄是s,使用s.subspec設(shè)置子目錄,這里設(shè)置子目錄為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版本統(tǒng)一

Podfile文件中 :

platform :ios, ‘9.0’

Podspec文件中:

s.ios.deployment_target = '9.0'

要始終保持這兩個版本一致,不然pod install會出錯。

六:include of non-modular header inside framework module

在封裝振動采集VibrationAcquistion框架時,更新到 Cocoapods 的時候出現(xiàn)一個錯誤,核心語句是 error: include of non-modular header inside framework module,在使用 Swift 的庫時會出現(xiàn)這個問題,把 Target 下 Build SettingsAllow Non-modular includes in Framework Modules 設(shè)為 Yes 就解決了。

七:Bitcode問題

如果組件內(nèi)直接或間接接入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是關(guān)閉Bitcode的,所以我們?nèi)绻眠@個Framework的話,就必須把項目的Bitcode關(guān)閉??梢栽赑odfile中添加以下語句,把所有Pod中的Bitcode都關(guān)閉。

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系統(tǒng)下點擊返回按鈕返回上級頁面的時候會造成crash.

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

因為 WKWebView 的 UIScrollView 代理惹的禍,因為我需要實時監(jiān)聽網(wǎng)頁的滾動區(qū)域來處理一些事情,所以我把 WKWebView.scrollView.delegate 設(shè)置為當(dāng)前控制器。
只要在VC的dealloc方法中把delegate置為nil就好了。

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

(2)如果引用WKWebView的VC中還有大標(biāo)題,那么在iOS12上會出現(xiàn)偏移量的問題。WKWebView會默認(rèn)有一個偏移量,最終展示為小標(biāo)題+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(), ^{
       //數(shù)據(jù)加載完成再設(shè)置代理,避免標(biāo)題閃一下
       webView.scrollView.delegate = self;
       //重新設(shè)置一下contentoffset,修復(fù)WKWebView在iOS12上導(dǎo)航欄偏移的問題
       CGFloat bigTitleHeight = self.dynamicNavView.bigTitleView.frame.size.height;
       [webView.scrollView setContentOffset:CGPointMake(0, - bigTitleHeight) animated:NO];
   });
}

webView.scrollView.delegate = self也要放到didFinishNavigation中,避免大標(biāo)題會閃動

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容