iOS9 sendSynchronousRequest deprecated

后端 未结 7 1376
自闭症患者
自闭症患者 2020-12-24 13:15

Warning:\'sendSynchronousRequest(_:returningResponse:)\' was deprecated in iOS 9.0: Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSe

7条回答
  •  一生所求
    2020-12-24 14:05

    I have modified the code of Nilesh Patel a little bit, so you can use the old call, just by changing class name.

    + (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error {
    __block NSData *blockData = nil;
    @try {
    
            __block NSURLResponse *blockResponse = nil;
            __block NSError *blockError = nil;
    
            dispatch_group_t group = dispatch_group_create();
            dispatch_group_enter(group);
    
            NSURLSession *session = [NSURLSession sharedSession];
            [[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable subData, NSURLResponse * _Nullable subResponse, NSError * _Nullable subError) {
    
                blockData = subData;
                blockError = subError;
                blockResponse = subResponse;
    
                dispatch_group_leave(group);
            }] resume];
    
            dispatch_group_wait(group,  DISPATCH_TIME_FOREVER);
    
            *error = blockError;
            *response = blockResponse;
    
        } @catch (NSException *exception) {
    
            NSLog(@"%@", exception.description);
        } @finally {
            return blockData;
        }
    }
    

提交回复
热议问题