踩坑 | swizzling 导致出现 UIImagePickerController Error 4099

错误描述

在实现从相册获取照片的功能时,发现调用 UIImagePickerController 时候报如下错误:

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

解决办法

常规Google一下,没有找到类似的解答,就从代码着手分析。

首先想到的是swizzling方法,项目中动态改了一下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 方法实现如下:

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

@implementation NSBundle (TKBundle)

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

从项目中移除这个 swizzling,重新run代码,正常了!!!

总结

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 如果你没有朝思暮想的恋着她 如果你没有灰头土脸的为着她 如果你没有寻寻觅觅的找过她 你断然不会理解我对她的这份深情...
    我心向月阅读 156评论 0 2
  • 【我从来都不信狗不咬人这句话】 第一,狗不是人,你说你家狗不咬人,但是狗与狗之间呢? 是,你是用绳子拉着你家的狗。...
    葛大屁阅读 310评论 0 0