I'm doing some experiment with the Foundation networking API and I'm trying to download files.
So I've created an NSURLRequest:
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.mysite.it/something.something"]];
Now I have a problem NSURLConnectionDownload.I have to implement this method
- (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long)expectedTotalBytes
At connection I created a NSURLConnection object but I don't know which methods I must implement... Any suggestion?
If you use
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; you'll probably want to implement 3 methods:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
and maybe
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
I recommend you to take a look here : https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html .Hope it helpes!
Related
I am using NSURLConnection's sendSynchronousRequest to communicate with server. I wanted to handle authentication which can be achieved using connection delegates but delegates are not called for a synchronous request.
NSData *data = [NSURLConnection sendSynchronousRequest:UrlRequest returningResponse:&response error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
if(!error)
{
// Do something
}
else
{
// Handle error
}
});
However i thought of sending all the request asynchronously using
[NSURLConnection connectionWithRequest:menuRequest delegate:self];
But i have multiple connection in the same class and each connection's success and error are used to perform different task.If i use async request error and success are heard by delegates which are same for all the request in that class, i cannot find out which request failed and which request succeeded. I have two question
If there is a way to implement https for synchronous request.
How to find which connection failed or succeeded among multiple connections in the same class for asynchronous request.
You can achieve this in different ways.
You can put creds in url like https://username:password#domain.tld/api/user.json
You can add add you creds to NSURLCredentialStorage before synchronous connection call.
You can use code below to achieve.
- (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse *__strong*)response error:(NSError *__strong*)error
{
_finishedLoading=NO;
_receivedData=[NSMutableData new];
_error=error;
_response=response;
NSURLConnection*con=[NSURLConnection initWithRequest:request
delegate:self
startImmediately:NO];
[con start];
return _receivedData;
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
//handle the challenge
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
*_response=response;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
*_error=error;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
When I'm using NSURLConnection to get data from my server, iOS still cache data. When requesting new data, iOS sending a new request to my server, but the files are still stored.
I'm using NSURLRequestReloadIgnoringCacheData and returning nil in connection:willCacheResponse:.
The problem is that I'm using my own caching object, and it's kind of dum to let iOS store a duplicate.
Should I ignore this or should I delete the cache somehow?
Update
Code for creating NSURLRequest:
request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
Code for creating NSURLConnection:
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
Code for NSURlConnectionDelegate:
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[recivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (_handler) _handler(response, error, nil);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (_handler) _handler(response, nil, recivedData);
recivedData = nil;
response = nil;
}
I have WSDL url with me. I have two methods in the WSDL, I need to access those method and get response using REST api.
What I have tried is below?
I used the below code snippets for request and response. But I didnt get any response.
- (void)viewDidLoad
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURLNSURL : [URLWithString"https://mywebsite.com/service.svc?wsdl"]];
[request setHTTPMethod"IBillingSubmissionService_RegisterDevice_InputMessage"];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[myConnection start];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[dataWebService setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(#"Response: %#",data);
[dataWebService appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
NSLog(#"Response: %#",responseString);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"Eror during connection: %#", [error description]);
}
What ever I am doing above is correct? Can some one provide a sample code snippet to call and get response using REST?
I want to know the below:
1) How to pass the value to WSDL method?
2) How to pass a reqiest and get response?
You can not directly use the wsdl webservices in objective c but you have to first construct the reference base classes.
You can use wsdl2objc code generator to construct the base classes to call the wsdl webservice
and Please refer the usage instructions for how to use it.
UPDATE: As a my advice you can see my answer here
The is my code
theDownloadConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
and it is calling these NSURLConnection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
But it is not calling
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;
This method of NSURLConnection delegate calls when I do this
[NSURLConnection connectionWithRequest:request delegate:self];
I dont understand why this is happening?
didSendBodyData is only called when the request has a body containing message data like in POST request. You are most likely executing a GET.
From the apple docs:
connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:
Sent as the body (message data) of a request is transmitted (such as
in an http POST request).
That's because its defined in NSURLConnectionDataDelegate and you are setting NSURLConnection
delegate.
Im implementing an app that shows some Twitter data, when is working on wifi is ok, but on 3g crashes, I have come to the conclusion that is because the NSURLRequest that fetchs the data that populates the table takes longer to load so the table ends up calling an object for key, when the keys havent even loaded,
So the question is,
- how can I know when the NSURLRequest finished loading? [I checked the Class Reference but didnt see it??]
Thanks a lot!
The delegate methods are invoked on the request's corresponding NSURLConnection, e.g.
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
You will then receive various callbacks included the finished callback as follows:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{}
Other delegate callbacks are:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {