Fixes a crash caused by network swizzling in FlipperKit (#1810)
Summary: This PR fixes the crash mentioned [here](https://github.com/facebook/flipper/issues/1674#issuecomment-754402609) in the https://github.com/facebook/flipper/issues/1674 . The issue was that logging `NSLog(@"%@", [self valueForKey:@"_methodDescription"]);` in the swizzled network delegate class used to cause a crash. The cause of this was the nil `methodDescription.types` sent to `class_addMethod`. In the following change we do not add a method to the class which doesn't implement the selector, we will anyway listen for those events from the base NSURLSession class. Removing the `class_addMethod`, fixes both the issue mentioned [here](https://github.com/facebook/flipper/issues/1674#issuecomment-754402609) in https://github.com/facebook/flipper/issues/1674. This PR also fixes an issue where no network calls where logged in Flipper when the iOS version was 14, the reason was that the URLSessionResume task was not properly swizzled for 14. ## Changelog - Do not add a runtime method to the delegate which doesn't implement a responder - Fix swizzling logic of the URLSession task resume method for ios 14 Pull Request resolved: https://github.com/facebook/flipper/pull/1810 Test Plan: - I ran the sample app and checked that the network calls are logged. - I copy pasted the changes in RN project and verified that the issues mentioned in https://github.com/facebook/flipper/issues/1674 doesn't happen and also verified that the network calls being hit from JS and Native code gets logged in Flipper. Verified with IsolatedSwizzles.zip mentioned over [here](https://github.com/facebook/flipper/issues/1674#issuecomment-754726174). Also verified by copy pasting the code in the fresh RN project. Reviewed By: fabiomassimo Differential Revision: D25801510 Pulled By: mweststrate fbshipit-source-id: 53ef574696d2abbdbfc2b6d94769327cdfa8aa2b
This commit is contained in:
committed by
Facebook GitHub Bot
parent
1075a1bc18
commit
98b74c4f49
@@ -315,26 +315,23 @@ typedef void (^NSURLSessionAsyncCompletion)(
|
||||
+ (void)injectIntoNSURLSessionTaskResume {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
// In iOS 7 resume lives in __NSCFLocalSessionTask
|
||||
// In iOS 8 resume lives in NSURLSessionTask
|
||||
// In iOS 9 resume lives in __NSCFURLSessionTask
|
||||
// In iOS 14 resume lives in NSURLSessionTask
|
||||
Class className = Nil;
|
||||
if (![[NSProcessInfo processInfo]
|
||||
if (![NSProcessInfo.processInfo
|
||||
respondsToSelector:@selector(operatingSystemVersion)]) {
|
||||
className =
|
||||
NSClassFromString([@[ @"__", @"NSC", @"FLocalS", @"ession", @"Task" ]
|
||||
componentsJoinedByString:@""]);
|
||||
} else if (
|
||||
[[NSProcessInfo processInfo] operatingSystemVersion].majorVersion < 9 ||
|
||||
[[NSProcessInfo processInfo] operatingSystemVersion].majorVersion >=
|
||||
14) {
|
||||
className = [NSURLSessionTask class];
|
||||
// iOS ... 7
|
||||
className = NSClassFromString(@"__NSCFLocalSessionTask");
|
||||
} else {
|
||||
className =
|
||||
NSClassFromString([@[ @"__", @"NSC", @"FURLS", @"ession", @"Task" ]
|
||||
componentsJoinedByString:@""]);
|
||||
NSInteger majorVersion =
|
||||
NSProcessInfo.processInfo.operatingSystemVersion.majorVersion;
|
||||
if (majorVersion < 9 || majorVersion >= 14) {
|
||||
// iOS 8 or iOS 14+
|
||||
className = [NSURLSessionTask class];
|
||||
} else {
|
||||
// iOS 9 ... 13
|
||||
className = NSClassFromString(@"__NSCFURLSessionTask");
|
||||
}
|
||||
}
|
||||
|
||||
SEL selector = @selector(resume);
|
||||
SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
|
||||
|
||||
|
||||
@@ -87,14 +87,10 @@
|
||||
|
||||
Method oldMethod = class_getInstanceMethod(cls, selector);
|
||||
if (oldMethod) {
|
||||
class_addMethod(
|
||||
cls, swizzledSelector, implementation, methodDescription.types);
|
||||
|
||||
objc_method_description* description = method_getDescription(oldMethod);
|
||||
class_addMethod(cls, swizzledSelector, implementation, description->types);
|
||||
Method newMethod = class_getInstanceMethod(cls, swizzledSelector);
|
||||
|
||||
method_exchangeImplementations(oldMethod, newMethod);
|
||||
} else {
|
||||
class_addMethod(cls, selector, implementation, methodDescription.types);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user