How we call sync call But the give response asynch.here is my code.
NSURL *URL = [NSURL URLWithString:#"http://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
// ...
}];
[task resume];
We can't and we never could.
The previous approach was halting the execution of the tread, continuing after getting the data or after a timeout.
Related
I am new to Mac application development and i am working on fixing the deprecation issues present in the application. I am using XCode 11.3 in Mojave MacOS 10.14 and Our application contains the following line of code
[NSURLConnection sendSynchronousRequest:request returningResonse:response error:Error]
A warning message getting displayed as sendSynchronousRequest is deprecated and it is suggesting to use [NSURLSession dataTaskWithRequest:completionHandler:] .
so i have implemented the following code
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:#"MyURLHere"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"Response:%# %#\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
}
}];
[dataTask resume];
i found that data is being returned as nil always. When i debugged the code, i found that even [NSURLSessionConfiguration defaultSessionConfiguration] is being returned as nil. Can you please suggest what needs to be done in order to fix these issues?
I am attempting to upload a PNG file to a device, using NSURLDataTask and a "POST" request.
However my attempts always conclude with the device returning a 400 HTTP error code in the response.
My Upload code looks like this:
-(void)uploadMedia:(NSData*)mediaData withName:(NSString*)name andID:(NSString*)mediaID
{
NSString *urlString;
NSString *appToken [self retrieveAppToken];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
appToken, #"km-device-auth",
#"application/octet-stream", #"Content-Type",
nil];
sessionConfiguration.HTTPAdditionalHeaders = dict;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
urlString = [NSString stringWithFormat:#"https://%#:7788/Media?name=%#&id=%#",_strIP,name,mediaID];
NSLog(#"Attemptive Upload URL:%#",urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = #"POST";
request.HTTPBody = mediaData;
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"My respone:%#:%#",response,data);
}];
[postDataTask resume];
}
SOLVED
My headers also required a value for "Content-MD5" which was missing.
I want to know the content_type of the NSURLRequest before it is downloaded.
As much as I searched I found the below code as solution :
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *httpRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
[httpRequest setHTTPMethod:#"HEAD"];
[httpRequest addValue:#"identity" forHTTPHeaderField:#"Accept-Encoding"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:httpRequest
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPResponse *httpResponse = (NSHTTPResponse*)response;
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog([dictionary[#"Content_Type"] description]);
}];
[task resume];
But i am always getting the content_type as text/HTML, even if the URL points to an image.
So it might be the content_type of the header which send because of HEAD set at setHTTPMethod:#"HEAD".
Please help me to find the actual Content_Type of the NSURLRequest before downloading it.
I have this method to get the contents of an URL on TV:
-(void)placeGetRequestForDeveloperID:(NSString *)developerID andRunOnCompletion:(void (^)(NSData *data, NSURLResponse *response, NSError *error))ourBlock {
NSString *urlString = [NSString stringWithFormat:#"https://itunes.apple.com/lookup?id=%#&entity=software", developerID];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
[session dataTaskWithRequest:request completionHandler:ourBlock];
}
The completion block is never called.
NSURLSession is allowed on tvOS. Is there something I have to set on the project, like entitlements or something on the Info.plist to make this work? Is this code OK?
Thanks!
By default, when you create the task, it's suspended. You have to start the task by calling resume.
[[session dataTaskWithRequest:request completionHandler:ourBlock] resume]
You have to resume the task to start the API call.
You can try the API call as like below too.
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask *apiCall = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
}];
[apiCall resume];
I just updated Xcode to the latest watch beta and a project that I was working on before now has errors which hadn't been there.
Here is my code:
NSString *urlToSet = [[NSString alloc] initWithFormat:#"http://www.example.com/"];
self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:urlToSet]];
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
The error is on the last line and it says:
'initWithRequest:delegate:' is unavailable: not available on watchOS
Since NSURLConnection is deprecated in iOS9, Apple is forcing everyone to switch to NSURLSession and Watch developers are not an exception. So just use the following code instead of yours:
NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:urlToSet]];
[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];