踩坑 | swizzling 導(dǎo)致出現(xiàn) UIImagePickerController Error 4099

錯(cuò)誤描述

在實(shí)現(xiàn)從相冊(cè)獲取照片的功能時(shí),發(fā)現(xiàn)調(diào)用 UIImagePickerController 時(shí)候報(bào)如下錯(cuò)誤:

UIImagePickerController UIViewController create error: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.webank.wemoney.apple-extension-service was invalidated." 

解決辦法

常規(guī)Google一下,沒有找到類似的解答,就從代碼著手分析。

首先想到的是swizzling方法,項(xiàng)目中動(dòng)態(tài)改了一下bundle id:

- (void)modifyBundleId{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    Method originMethod = class_getInstanceMethod([NSBundle class], @selector(bundleIdentifier));
    Method swizzlingMethod = class_getInstanceMethod([NSBundle class], @selector(tk_bundleIdentifier));
    
    BOOL didAddMethod =  class_addMethod([NSBundle class], @selector(bundleIdentifier), method_getImplementation(swizzlingMethod), method_getTypeEncoding(swizzlingMethod));
    
    if (didAddMethod) {
        class_replaceMethod([NSBundle class], @selector(tk_bundleIdentifier), method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    }else{
        method_exchangeImplementations(originMethod, swizzlingMethod);
    }
}

tk_bundleIdentifier 方法實(shí)現(xiàn)如下:

#import "NSBundle+TKBundle.h"
#import <objc/runtime.h>

@implementation NSBundle (TKBundle)

- (NSString *)tk_bundleIdentifier{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    return @"com.tk.test";
}
@end

從項(xiàng)目中移除這個(gè) swizzling,重新run代碼,正常了!??!

總結(jié)

黑魔法固然好用,但是有很大的副作用,慎用?。?!

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

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

  • 如果你沒有朝思暮想的戀著她 如果你沒有灰頭土臉的為著她 如果你沒有尋尋覓覓的找過(guò)她 你斷然不會(huì)理解我對(duì)她的這份深情...
    我心向月閱讀 192評(píng)論 0 2
  • 【我從來(lái)都不信狗不咬人這句話】 第一,狗不是人,你說(shuō)你家狗不咬人,但是狗與狗之間呢? 是,你是用繩子拉著你家的狗。...
    葛大屁閱讀 332評(píng)論 0 0

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