What is replacement of CFReadStreamCreateForStreamedHTTPRequest? - ios

In my objective C project i am using
Segment 1).
[(NSInputStream *)CFReadStreamCreateForStreamedHTTPRequest(kCFAllocatorDefault, request,(CFReadStreamRef)[self postBodyReadStream]) autorelease]
but CFReadStreamCreateForStreamedHTTPRequest is deprecated
what can i write alternatively ?
I tried
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
Error throws:-
Incompatible pointer types sending 'CFHTTPMessageRef' (aka 'struct >__CFHTTPMessage *') to parameter of type 'NSURLRequest * _Nonnull'
Segment 2).
(NSString *)kCFStreamPropertyHTTPProxyHost
'kCFStreamPropertyHTTPProxyHost' is deprecated:
Haven't found any replacement API.

To use dataTaskWithRequest:, you need to create an NSURLRequest, not a CFHTTPMessageRef.
As for kCFStreamPropertyHTTPProxyHost, this question asks “How to programmatically add a proxy to an NSURLSession” and has an answer that looks good to me.

Related

How to set several options in the setCategory method of AVAudioSession in Objective-C?

I am trying to use AVAudioSession setCategory method with several options and I have seen online that you can specify an array to the options argument in swift but cannot figure out how to do it in objective-C.
Here is the documentation where you can see the difference of the options argument expected in swift vs objective-C:
I have tried with objective-C arrays and it does store the options expected.
Thanks for your help!
Category options is bit mask. Here is an example of usage
AVAudioSession *session = AVAudioSession.sharedInstance;
NSError *error = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord
mode:AVAudioSessionModeVoiceChat
options:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionDuckOthers
error:&error];
if (nil == error)
{
// continue here
}

Firebase Performance crashes the app on the first call to NSURLSession

I have just added Firebase Performance to my which is mainly Obj-C and has Firebase (Core + Analytics + Messaging + Config) I read in the docs that:
Performance Monitoring does not support network requests made using
the NSURLConnection class.
However what's not expected is that the app crashes on the first call of NSURL*
e.g. I'm using a lib called "Harpy" which checks for new version of the app in the AppStore and it crashes here:
NSURLSession *session = [NSURLSession sharedSession]; // <--- Crashes here
NSURLSessionDataTask *task = [session
dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if ([data length] > 0 && !error) { // Success
[self parseResults:data];
}
}
];
I can't really see any useful exception however the thread starts with:
Thread 1 Queue : com.google.FPRNSURLSessionInstrumentation (serial)
So issue above was not directly related to Firebase itself, but actually to a conflict between Firebase Performance and Crittercism, the solution was to disable Crittercism's monitoring for NSURLSession as follows:
CrittercismConfig *config = [CrittercismConfig defaultConfig];
config.monitorNSURLSession = false;
[Crittercism enableWithAppID:settingsManager.crittericismKey andConfig:config];
[Crittercism setValue:[NSLocale preferredLanguages].firstObject forKey:#"deviceLanguage"];

Error in AFNetworking following upgrade to Xcode 7

Following upgrading to Xcode 7 (7A21B) an AFNetworking error is thrown when building my current project in AFURLSessionManager.m. I've linked AFNetworking as a submodule in Git so it's regularly up to date.
The error is
AFURLSessionManager.m:288:87: Null passed to a callee that requires a non-null argument
The line responsible:
NSURLSessionDataTask *dataTask = [[NSURLSession sessionWithConfiguration:nil] dataTaskWithURL:nil];
Obviously the nil arguments need to either be replaced with values or the method to instantiate the dataTask object needs to change but I'm not familiar enough with AFNetworking to make the change myself.
This is the entire method:
+ (void)initialize {
if ([NSURLSessionTask class]) {
NSURLSessionDataTask *dataTask = [[NSURLSession sessionWithConfiguration:nil] dataTaskWithURL:nil];
Class taskClass = [dataTask superclass];
af_addMethod(taskClass, #selector(af_resume), class_getInstanceMethod(self, #selector(af_resume)));
af_addMethod(taskClass, #selector(af_suspend), class_getInstanceMethod(self, #selector(af_suspend)));
af_swizzleSelector(taskClass, #selector(resume), #selector(af_resume));
af_swizzleSelector(taskClass, #selector(suspend), #selector(af_suspend));
[dataTask cancel];
}
}
Is there a way I can quench this error?
Tree solutions. Either use the shared session:
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:nil];
or decide on a configuration. Will you send files in background or only if your app is in foreground? Replace nil with either
[NSURLSessionConfiguration defaultSessionConfiguration]
or with
[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:#"My Identifier"]
or use NSURLSession directly instead of an obsolete third party framework.
This was a simple mistake. Thanks to Quentin for highlighting that the submodule in Git had not been updated. Doing this rectified the error.

"NSURLSession sharedSession" is not kind of NSURLSession

I created category for NSURLSession, and then I faced a problem. In iOS 7, [[NSURLSession sharedSession] isKindOfClass:[NSURLSession class]] returns NO.
I know that [NSURLSession sharedSession] is returning instance of __NSCFURLSession, but it is not a subclass of NSURLSession. So, it doesn't make any sense. At compile time everyting is ok, but in runtime I get unrecognized selector exception.
How can I get around with this? Is there is any runtime magic feature, that I can use? Because
[[NSURLSession sharedSession] respondsToSelector:#selector(dataTaskWithRequest:completionHandler:)]
returns YES, and that is the only method I use in my category.
As an alternative to creating an instance method in your category you could create a class method which accepts an instance of NSURLSession.
Assuming a category like this:
#interface NSURLSession (MyCategory)
- (void)doSomethingWithCompletion:(MyCompletionHandler)completionHandler;
#end
The replacement could be:
#interface NSURLSession (MyCategory)
+ (void)doSomethingWithSession:(NSURLSession *)session completion:(MyCompletionHandler)completionHandler;
#end
With usage as follows:
[NSURLSession doSomethingWithSession:[NSURLSession sharedSession]
completion:^{ ... }];

iOS NSURLSession Listen to Timeout

I created a simple NSURLSessionDownloadTask to download from a URL, with its class having the NSURLSession delegates:
#interface DownloadManager : NSObject <NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSURLSessionTaskDelegate>
//...
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
[sessionConfiguration setTimeoutIntervalForRequest:30.0];
[sessionConfiguration setTimeoutIntervalForResource:60.0];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:self.url];
[downloadTask resume];
However, I could not find a protocol method that listens to the download task timing out. Is there a way to listen to the timeout (ex. - I wanted to close a progress dialog box when 30.0 seconds have passed and no data is still received)
I've already scavenged Google but haven't found any information so far, so I'll leave this question here while I search for more info.
Thanks so much!
The timeout is one of the errors NSURLSession will give you in completionHandler block. It's NSURLErrorTimedOut = -1001.
in delegate method
- URLSession:task:didCompleteWithError:
check the NSError if it's NSURLErrorTimedOut do what you want
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/#//apple_ref/doc/constant_group/URL_Loading_System_Error_Codes

Resources