I want to use the CFReadStream to implement downloading in background, via setting the kCFStreamNetworkServiceTypeBackground.
CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeBackground);
NSDictionary *settings = @{CFBridgingRelease(kCFStreamSSLPeerName) : _request.host};
CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, (__bridge CFDictionaryRef)settings);
CFReadStreamSetClient(readStream, kStreamEvents, requestCallback, &context);
static void requestCallback(CFReadStreamRef _Null_unspecified stream, CFStreamEventType type, void * _Null_unspecified clientCallBackInfo) {
CFReadStreamViewController *controller = (__bridge CFReadStreamViewController *)clientCallBackInfo;
switch (type) {
// ....
case kCFStreamEventHasBytesAvailable: {
NSUInteger bytesRead = 0;
uint8_t buffer[1024];
bytesRead = [controller.inputStream read:buffer maxLength:sizeof(buffer)];
if (bytesRead > 0) {
NSData *data = [[NSData alloc] initWithBytes:buffer length:bytesRead];
NSLog(@"%ld bytes read------", data.length);
}
}
break;
// .....
}
}
But when I press the home button of iPhone, the app enter the background, then no any logs in Xcode debug console. That means the download was suspended?
By the way, in order to support SNI, I use the CFReadStream instead of the NSURLSession.
Thanks for answering!