錯(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é)
黑魔法固然好用,但是有很大的副作用,慎用?。?!