Fix NSURLConnection is deprecated - Use NSURLSession (#4112)

This commit is contained in:
A Cœur 2022-11-12 11:29:48 +08:00 committed by GitHub
parent 2aa46f840c
commit 48e42ac71e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 63 deletions

View File

@ -4,6 +4,8 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(unsigned int, port_status_t) { // typedef NS_ENUM(unsigned int, port_status_t) { //
PORT_STATUS_CHECKING, PORT_STATUS_CHECKING,
PORT_STATUS_OPEN, PORT_STATUS_OPEN,
@ -27,3 +29,5 @@ typedef NS_ENUM(unsigned int, port_status_t) { //
- (void)portCheckerDidFinishProbing:(PortChecker*)portChecker; - (void)portCheckerDidFinishProbing:(PortChecker*)portChecker;
@end @end
NS_ASSUME_NONNULL_END

View File

@ -11,8 +11,8 @@ static NSTimeInterval const kCheckFireInterval = 3.0;
@property(nonatomic, weak) NSObject<PortCheckerDelegate>* fDelegate; @property(nonatomic, weak) NSObject<PortCheckerDelegate>* fDelegate;
@property(nonatomic) port_status_t fStatus; @property(nonatomic) port_status_t fStatus;
@property(nonatomic) NSURLConnection* fConnection; @property(nonatomic) NSURLSession* fSession;
@property(nonatomic) NSMutableData* fPortProbeData; @property(nonatomic) NSURLSessionDataTask* fTask;
@property(nonatomic) NSTimer* fTimer; @property(nonatomic) NSTimer* fTimer;
@ -24,6 +24,8 @@ static NSTimeInterval const kCheckFireInterval = 3.0;
{ {
if ((self = [super init])) if ((self = [super init]))
{ {
_fSession = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.ephemeralSessionConfiguration delegate:nil
delegateQueue:nil];
_fDelegate = delegate; _fDelegate = delegate;
_fStatus = PORT_STATUS_CHECKING; _fStatus = PORT_STATUS_CHECKING;
@ -42,7 +44,7 @@ static NSTimeInterval const kCheckFireInterval = 3.0;
- (void)dealloc - (void)dealloc
{ {
[_fTimer invalidate]; [self cancelProbe];
} }
- (port_status_t)status - (port_status_t)status
@ -55,51 +57,7 @@ static NSTimeInterval const kCheckFireInterval = 3.0;
[self.fTimer invalidate]; [self.fTimer invalidate];
self.fTimer = nil; self.fTimer = nil;
[self.fConnection cancel]; [self.fTask cancel];
}
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
self.fPortProbeData.length = 0;
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
[self.fPortProbeData appendData:data];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog(@"Unable to get port status: connection failed (%@)", error.localizedDescription);
[self callBackWithStatus:PORT_STATUS_ERROR];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSString* probeString = [[NSString alloc] initWithData:self.fPortProbeData encoding:NSUTF8StringEncoding];
self.fPortProbeData = nil;
if (probeString)
{
if ([probeString isEqualToString:@"1"])
{
[self callBackWithStatus:PORT_STATUS_OPEN];
}
else if ([probeString isEqualToString:@"0"])
{
[self callBackWithStatus:PORT_STATUS_CLOSED];
}
else
{
NSLog(@"Unable to get port status: invalid response (%@)", probeString);
[self callBackWithStatus:PORT_STATUS_ERROR];
}
}
else
{
NSLog(@"Unable to get port status: invalid data received");
[self callBackWithStatus:PORT_STATUS_ERROR];
}
} }
#pragma mark - Private #pragma mark - Private
@ -108,30 +66,48 @@ static NSTimeInterval const kCheckFireInterval = 3.0;
{ {
self.fTimer = nil; self.fTimer = nil;
NSString* urlString = [NSString stringWithFormat:@"https://portcheck.transmissionbt.com/%ld", [timer.userInfo integerValue]]; NSString* urlString = [NSString stringWithFormat:@"https://portcheck.transmissionbt.com/%ld", [(NSNumber*)timer.userInfo integerValue]];
NSURLRequest* portProbeRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] NSURLRequest* portProbeRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:15.0]; timeoutInterval:15.0];
if ((self.fConnection = [[NSURLConnection alloc] initWithRequest:portProbeRequest delegate:self])) _fTask = [_fSession dataTaskWithRequest:portProbeRequest
{ completionHandler:^(NSData* _Nullable data, NSURLResponse* _Nullable response, NSError* _Nullable error) {
self.fPortProbeData = [[NSMutableData alloc] init]; if (error)
} {
else NSLog(@"Unable to get port status: connection failed (%@)", error.localizedDescription);
{ [self callBackWithStatus:PORT_STATUS_ERROR];
NSLog(@"Unable to get port status: failed to initiate connection"); return;
[self callBackWithStatus:PORT_STATUS_ERROR]; }
} NSString* probeString = [[NSString alloc] initWithData:data ?: NSData.data encoding:NSUTF8StringEncoding];
if (!probeString)
{
NSLog(@"Unable to get port status: invalid data received");
[self callBackWithStatus:PORT_STATUS_ERROR];
}
else if ([probeString isEqualToString:@"1"])
{
[self callBackWithStatus:PORT_STATUS_OPEN];
}
else if ([probeString isEqualToString:@"0"])
{
[self callBackWithStatus:PORT_STATUS_CLOSED];
}
else
{
NSLog(@"Unable to get port status: invalid response (%@)", probeString);
[self callBackWithStatus:PORT_STATUS_ERROR];
}
}];
[_fTask resume];
} }
- (void)callBackWithStatus:(port_status_t)status - (void)callBackWithStatus:(port_status_t)status
{ {
self.fStatus = status; self.fStatus = status;
if (self.fDelegate && [self.fDelegate respondsToSelector:@selector(portCheckerDidFinishProbing:)]) NSObject<PortCheckerDelegate>* delegate = self.fDelegate;
{ [delegate performSelectorOnMainThread:@selector(portCheckerDidFinishProbing:) withObject:self waitUntilDone:NO];
[self.fDelegate performSelectorOnMainThread:@selector(portCheckerDidFinishProbing:) withObject:self waitUntilDone:NO];
}
} }
@end @end

View File

@ -495,7 +495,7 @@ static NSString* const kWebUIURLFormat = @"http://localhost:%ld/";
case PORT_STATUS_CHECKING: case PORT_STATUS_CHECKING:
break; break;
default: default:
NSAssert1(NO, @"Port checker returned invalid status: %d", self.fPortChecker.status); NSAssert(NO, @"Port checker returned invalid status: %d", self.fPortChecker.status);
break; break;
} }
self.fPortChecker = nil; self.fPortChecker = nil;