NSURL Request on Watch OS 2 - objective-c

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) {
}];

Related

NSURLSessionConfiguration and NSData always returning nil

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?

How to find the actual "Content_Type" of the NSURLRequest before downloading it

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.

NSURLSession not working on tvOS?

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];

How we call sync Webservice in iOS9

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.

AFHTTPSessionManager post request

I want to post a request to server. It is working fine if I am using NSURLSessionDataTask. But underneath I need to use AFNetworking as my whole application is using it. But when I am trying to hit the same service in AFHTTPSessionManager with POST method, It is giving me request time out.
Below are both the codes.
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
NSURL *url = [NSURL URLWithString:#"BASE URL"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request addValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setHTTPMethod:#"POST"];
NSString *postString = #"1";
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}];
[postDataTask resume];
AFNetworking kit version :-
AFHTTPSessionManager *client = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:#"BASE URL"]];
NSDictionary *request = #{#"Content-Type":#"application/json",
#"Accept":#"application/json",
};
NSString *postString = #"1";
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[client POST:#"" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithHeaders:request body:data];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(#"%#",responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"%#",error);
}];
Please help me in implementing it in right way.
I solved this problem by creating my own request and give its configuration to AFURLSessionManager and then holding my own NSURLSessionUploadTask object.
Thanks