Where to put Loading - objective-c

In my app I have connections with a web service and I would like to create a loading view to show each time the connection starts until it returns a response.
Where is the best place to put this view in my application and what is the best way to implement it?
Thanks,
Fermin

NSURL *url = [NSURL URLWithString:#"http://.....?WSDL"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[theConnection start];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webData appendData:data];
}

Related

How to pass 2 parameters in a method in WCF Service using Objective c?

My Parameters are
Request1 = <Request> <name>2</name> <pwd>1</pwd> </Request>
Request2 = value1
My Code is
-(void)webRequestCommnunication
{
//Web Service Call
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <soap:Body><Method1 xmlns=\"http://tempuri.org/\"><Request1><Request> <name>2</name> <pwd>1</pwd> </Request></Request1><Request2>value1</Request2></Method1></soap:Body></soap:Envelope>"];
NSURL *url = [NSURL URLWithString:#"http://localIPaddress/servicename1/servicename2/Method1.svc"];
theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/service1/Method1" forHTTPHeaderField:#"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
webData = [[NSMutableData alloc]init];
if(theConnection)
{
NSLog(#"Connected....");
}
else
{
NSLog(#"theConnection is NULL");
}
}
I want to pass Request1, Request2 inside Method1,but it throws exception as The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Method1'. End element 'Request1' from namespace 'http://tempuri.org/' expected. Found element 'Request' from namespace 'http://tempuri.org/'. Line 1, position 276..
Can anyone suggest what i am doing wrong?
Finally Got solution!
While sending Request1 parameter the <,> symbol is not parsed in Soap Format,only the parent tag the symbol is validated and the modified code is
-(void)webRequestCommnunication
{
//Web Service Call
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <soap:Body><Method1 xmlns=\"http://tempuri.org/\"><Request1><Request1><Request><name>2</name><pwd>1</pwd></Request></Request1><Request2>value1</Request2></Method1></soap:Body></soap:Envelope>"];
NSURL *url = [NSURL URLWithString:#"http://localIPaddress/servicename1/servicename2/Method1.svc"];
theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/service1/Method1" forHTTPHeaderField:#"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
webData = [[NSMutableData alloc]init];
if(theConnection)
{
NSLog(#"Connected....");
}
else
{
NSLog(#"theConnection is NULL");
}
}

Can't obtain a response from WCF service

i have a trouble when i try to call a web service made in WCF, this service is calling by Mac os x app.
this is the code
soapMessage = #"{name:\"Sergio\"}";
NSURL *url = [NSURL URLWithString:#"http://localhost/GSBonoServicios/GSBonoService.svc/HelloWorld"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest setHTTPMethod: #"POST"];
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSData *requestData = [NSData dataWithBytes:[soapMessage UTF8String] length:[soapMessage length]];
[theRequest setHTTPBody:requestData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
The connection is successful but didn't a response, i try implementing some tutorials and I'm looking in Stackoverflow and i find many answers but nothing works, i hope you can help me.
ps. I do not speak much English, but I hope I explained, Thanks
Finally I found the solution the problem was in the service, and the code that works is
NSString *name=#"Sergio";
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys: name, #"name",
nil];
NSURL *url = [NSURL URLWithString:#"http://www.myserver.com/Servicio/ServicioEjemplo.svc/HelloWorld"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod: #"POST"];
[theRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"applicat ion/json" forHTTPHeaderField:#"Content-Type"];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:kNilOptions error:&error];
[theRequest setHTTPBody:postdata];
NSLog(#"JSON summary: %#", [[NSString alloc] initWithData:postdata
encoding:NSUTF8StringEncoding]);
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
Thanks

How to send a POST request via WebKit

To navigate a WebKit WebView to a URL is quite easy:
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[m_webView mainFrame] loadRequest:requestObj];
I can't see where/how to add POST data to any of these objects. How do one perform a Post, using a hosted WebView in Cocoa?
Sample code for the answer:
NSData* postData = postString;
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSString* contentLength = [NSString stringWithFormat:#"%d", [data length]];
[urlRequest setValue:contentLength forHTTPHeaderField:#"Content-Length"];
[urlRequest setHTTPBody:postData];
[[m_webView mainFrame] loadRequest:urlRequest];
Use an NSMutableURLRequest object. It has a setHTTPMethod: method.

how to write xml for soap message

I am new to web services in iphone.
I need to get data from .net server.
I fallow this tutorial
In this soap message is
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<Hello xmlns=\"http://viium.com/\">\n"
"<name>%#</name>\n"
"</Hello>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", nameInput.text
];
But i did n't have knowledge on this,
I am from java background.
In android my xml string is
xml="<MortgageGetLoanOfficerInfo><PhoneNumber>"+getDeviceTelNo()+"</PhoneNumber></MortgageGetLoanOfficerInfo>";
how can i write this in iphone.
can any pls post solution.
Thank u in advance.
You are correctly assigning your soap request in NSString. Now you have to create NSMutableURLRequest object and send the request as follow
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:webServiceUrl];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:yourSoapAction forHTTPHeaderField:#"SOAPAction"];
[req addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[[NSError alloc] init] autorelease];
NSData *webData = [NSURLConnection sendSynchronousRequest:req returningResponse:&urlResponse error:&error];
Now parse your webData using any of xml parser. I generally prefer NSXMLParser
Hope this helped..
try with following sample code;
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<getLogin xmlns=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"
"<email>%#</email>\n"
"<user_pwd>%#</user_pwd>\n"
"</getLogin>\n"
"</soap:Body>\n"
"</soap:Envelope>\n", userNameField.text,passWordField.text];
NSLog(#"%#", soapMessage);
NSURL *url = [NSURL URLWithString:#"web service url"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"your soap action" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection ){
webData = [[NSMutableData data] retain];
}else{
NSLog(#"theConnection is NULL");
}

NSURLConnection Never finish

i have the next code:
NSURL *url = [NSURL URLWithString:#"http:...."];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [""soapMessage"" length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [""soapMessage"" dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setTimeoutInterval:20];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[theConnection start];
Then sometimes, when i click a lot of times (Just to try where can fail the app) in my interface in the buttons assigned to start a new connection, the connection doesn't give a reply (I send the request but looks like is blocked after).
I would like to know what is the use of "setTimeoutInterval"? AND if there are some ways to limit the time to wait one response?
Thanks!
How do you know that the request started in the first place? You might be waiting for a response for a request that never started.
It appears you are not testing for theConnection being set to nil. also theRequest and url could also be nil if you run out of memory.