NSURLConnection shown as leaking in instruments - iphone

Hello another stupid question regarding leaks and also NSURLConnection. How do i release it? Is it enough if i release in the following 2 methods?
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
(void)connectionDidFinishLoading:(NSURLConnection *)connection
Because in instruments it shows me the line where I alloc my connection as the source of leaking.
(EDIT1: OK I don't get it. After the following code my urlConnection has a retain count of 2. WTF?)
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self];
This is the line that instruments points me to.
EDIT2: here is some code:
I create the connection here
- (void) makeRequest
{
//NSString *urlEncodedAddress = [self.company.street stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *urlString = [[NSString alloc] initWithFormat:
#"http://maps.google.com/maps/api/geocode/xml?latlng=%f,%f&sensor=false",
bestEffort.coordinate.latitude,bestEffort.coordinate.longitude];
debugLog(#"%#",urlString);
NSURL *url = [[NSURL alloc] initWithString: urlString];
[urlString release];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL: url];
[url release];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self];
debugLog(#"connection created %# rc %i", urlConnection, urlConnection.retainCount);
[urlRequest release];
connection = urlConnection;
}
I release it here
-(void)connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error
{
debugLog(#"ERROR with the connection: %#", error.localizedDescription);
//[activityIndicator setHidden:YES];
debugLog(#"connection will be released or else %# %i", _connection, [_connection retainCount]);
[connection release];
connection = nil;
[webData release];
webData = nil;
if (!cancel)
[delegate rgc_failedWithError: self : error];
isWorking = FALSE;
}
Or here
-(void)connectionDidFinishLoading:(NSURLConnection *)_connection
{
debugLog(#"connection will be released (or else) %# %i", _connection, [_connection retainCount]);
[connection release];
connection = nil;
debugLog(#"DONE. Received Bytes: %d", [webData length]);
//NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
//debugLog(#"%#",theXML);
//[theXML release];
.....
.....
}
EDIT3: Problem solved by not caring whether it's leaking or not! Simple!

You're correct to release it in the delegate methods, however the analysis tools like instruments and the clang analyser aren't clever enough to deal with that and report a false positive. I'd be inclined to file a bug with apple, it will certainly be a duplicate but will tell them that more developers are finding this annoying.

Related

POST username and password in JSON

I have a web service I am working with a url such as
//myurl/index.jsp?user_name=bob&user_pwd=new
as you can see the username has been set as "bob" and password "new". The site give this json file when entered,
[{"success":"1"}]
How do you implemented it on xcode, where when a user enters "bob" as username and "new" as password it should lead to the next controller. How do can I achieve that ?
I followed this tutorial though it's not quite the same, how do you do this. Thanks.
Use a navigation controller and set you view controller as the rootViewController. Then after the user has entered the credentials push the new view controller onto the navigation stack.
Get the value of success from json response. If it is equal to 1 then push to next controller else do nothing.
In Following code you can pass data by either GET or POST method … use any one (As You Wish)
-(void) sendRequest
{
//////////////////////////// GET METHOD /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
NSString *strURL=[NSString stringWithFormat:#"http://myurl/index.jsp?user_name=bob&user_pwd=new"];
self.request=[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]];
self.nsCon=[[NSURLConnection alloc] initWithRequest:request delegate:self];
//////////////////////////// POST METHOD /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
NSString *postString = [NSString stringWithFormat:#"&user_name=bob&user_pwd=new"];
NSString *url = [NSString stringWithFormat:#"http://myurl/index.jsp/"];
self.request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[self.request setHTTPMethod:#"POST"];
[self.request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(self.nsCon)
{
self.receivedData=[[NSMutableData alloc] init];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error",#"") message:NSLocalizedString(#"Not Connected !!",#"") delegate:nil cancelButtonTitle:NSLocalizedString(#"OK",#"") otherButtonTitles:nil];
[alert show];
[alert release];
}
}
#pragma mark -
#pragma mark - Connection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[GeneralClass stopHUD];
NSLog(#"Connection failed.");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error",#"") message:NSLocalizedString(#"Connection failed!",#"") delegate:nil cancelButtonTitle:NSLocalizedString(#"OK",#"") otherButtonTitles:nil];
[alert show]; alert = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
NSMutableDictionary *receivedData = [responseString JSONValue];
if([[receivedData objectForKey:#"success"] isEqualToString:#"1"])
{
MainDetailViewController *mdController = [[MainDetailViewController alloc] init];
[self.navigationController pushViewController:mdController animated:YES];
}
else
{
NSLog(#"%#",receivedData);
}
}
Post login data
NSString *soapMsg = [NSString stringWithFormat:#"&data={\"LoginUser\":[{\"UserName\":\"%#\",\"Password\":\"%#\"}]}",firstname.text, password.text];
NSHTTPURLResponse *response;
NSData *myRequestData = [ NSData dataWithBytes: [ soapMsg UTF8String ] length: [ soapMsg length ] ];
NSString *postLength = [NSString stringWithFormat:#"%d", [myRequestData length]];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:#"http://myurl/index.jsp"]];
[request setHTTPMethod: #"POST" ];
[request setHTTPBody: myRequestData ];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody: myRequestData];
NSURLConnection *myConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
Receive and vaild json data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:WebXmlData encoding:NSUTF8StringEncoding];
NSDictionary *results = [responseString JSONValue];
BOOL success = [[results objectForKey:#"success"] boolValue];
if (success) {
ViewController *viewController =[[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
}
}
Not quite the answer you are looking for but this is more a recommendation for what should you use.
I use github link for Network related activities.
They have nice documentation and very easy to use (uses blocks)
NSDictionary *paramsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"user",#"client_id",#"password",#"new", nil];
[[YourAFHttpClientExtenstion sharedInstance] postPath:LOGIN_PATH parameters:paramsDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
//handle success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// handle error.
}];
YourAFHttpClientExtenstion is extending AFHttpClient and adds a convening method of shared instance and implements initWithBaseUr:

Better approach to performing a series of SOAP requests in Objective-C

I use the following hack-job code to perform a series of SOAP requests that download data from a server for use in the application:
This code is called when the 'update' button is pressed:
- (IBAction) update {
UIAlertView *errorView;
if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {
errorView = [[UIAlertView alloc]
initWithTitle: #"Network Error"
message: #"No Network connection availible!"
delegate: self
cancelButtonTitle: #"OK" otherButtonTitles: nil];
[errorView show];
}
else
{
[appDelegate.categories removeAllObjects];
[appDelegate.currencies removeAllObjects];
[appDelegate.projects removeAllObjects];
HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
HUD.labelText = #"Downloading..";
[self requestCategories];
}
}
Below is a typical request, I use approximately 6 of them.
// SOAP requests
- (void) requestCategories {
// Indeterminate mode
categories = [[NSMutableArray alloc] init];
xmlBlock = CATEGORY;
NSString *soapMsg =
[NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?> <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/\"> <soap:Body> <Categories xmlns=\"http://tempuri.org/\"> <UID>string</UID> <Username>string</Username> <Password>string</Password> </Categories> </soap:Body> </soap:Envelope>"
];
//---print it to the Debugger Console for verification---
NSLog(#"%#", soapMsg);
NSURL *url = [NSURL URLWithString:
#"http://www.$$%$%^^^%$$££.co.uk/%$^£^£^$&£.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
//---set the headers---
NSString *msgLength = [NSString stringWithFormat:#"%d",
[soapMsg length]];
[req addValue:#"text/xml; charset=utf-8"
forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://tempuri.org/Categories"
forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:#"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
//[activityIndicator startAnimating];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [NSMutableData data];
}
}
Below are my delegate methods for NSURLConnection (and a parsing method):
-(void) connection:(NSURLConnection *) connection
didReceiveResponse:(NSURLResponse *) response {
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection
didReceiveData:(NSData *) data {
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection
didFailWithError:(NSError *) error {
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc]
initWithBytes: [webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
//---shows the XML---
NSLog(#"%#", theXML);
if (xmlBlock == CATEGORY){
[self parseXML:webData];
[self requestCurrencies];
}
else if (xmlBlock == CURRENCY){
[self parseXML:webData];
[self requestNominals];
}
else if (xmlBlock == NOMINAL){
[self parseXML:webData];
[self requestProjects];
}
else if (xmlBlock == PROJECT){
[self parseXML:webData];
[self requestRegister];
}
else {
[self parseXML:webData];
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"37x-Checkmark.png"]];
HUD.labelText = #"Done!";
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:2];
}
}
- (void) parseXML: (NSMutableData *)localWebData {
xmlParser = [[NSXMLParser alloc] initWithData: localWebData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
}
I don't think you need to see my xml parsing delegate methods (if you do let me know). My question is, is there a better way to implement this functionality in my app? As in perform the requests one after another while displaying some kind of progress indicator to the user?
Thanks,
Jack
use NSOperation queue, that is make your class a subclass of NSOperation in which you are sending the request to service, and rename of your method to main. then make the property of this class in parent class and add all the request in operation queue. And for the finishing, use keyobserver for your that property of nsopertion subclass

Generating a URL string for NSURLRequest fails to initiate

This one has me pretty confused. If I put in the following:
NSString *LoginURLString = [NSString stringWithFormat:#"http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=13242134"];
//NSLog output: http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=2314084
And use this in a URL request, it works fine, but I need to make this dynamic, so I have it concatenate the URL string with a new UserID by using the following:
NSString *user = [NSString stringWithFormat:#"%#", [[NSUserDefaults standardUserDefaults]stringForKey:#"CustomerID"]];
//user = [[NSUserDefaults standardUserDefaults] stringForKey:#"CustomerID"];
NSString *LoginURLString = [NSString stringWithFormat:#"http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=%#", user];
//NSLog output: http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=2314084
Here is the rest of my request initializer:
NSString *urlString = LoginURLString;
responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
And the other methods that handle the request:
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[responseData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responseData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
// [connection release];
CommonPickUpArray = [[NSMutableArray alloc] init];
CommonLocationInfoArray = [[NSMutableArray alloc] init];
NSString *data = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
NSLog(#"%#", data);
}
This request never even starts. I really don't understand why. I have tried to output the two strings to the NSLog and either way they look exactly the same. Can anyone explain? Thanks for your help!
Edit: The Connection didFailWithError method is outputting this:
Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0xf6a6f50 {NSUnderlyingError=0xf6a75d0 "bad URL", NSLocalizedDescription=bad URL}
Output from answer 1:
2012-05-08 13:45:24.959 AmericanTaxi[1295:707] Connection failed with error: bad URL
2012-05-08 13:45:24.960 AmericanTaxi[1295:707] for the URL: (null)
Output of urlString and LoginURLString:
2012-05-08 13:57:40.415 AmericanTaxi[1320:707] LoginURLString: http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=2314084
2012-05-08 13:57:40.417 AmericanTaxi[1320:707] urlstring: http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=2314084
In your didFailWithError, check the URL and see why it is bad by adding this to your didFailWithError delegate:
NSLog(#"Connection failed with error: %#", [error localizedDescription]);
NSLog(#"for the URL: %#", [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
Post the result.

rest web services in iphone

I have a problem now. I need to pass an transactionID and an user password to a rest service and it is suppose to return me a true/false value (in XML format). However, it is consistently returning me (null).. I am totally lost some one please help.
NSString *urlString = [NSString stringWithFormat:#"https://10.124.128.93:8443/axis2/services/C3WebService/completeWithdrawal Transaction?transactionId=%#&password=%#", _transactionID.text, _userPassword.text];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSString *result = [[NSString alloc] initWithContentsOfURL:url];
NSLog(#"%#",result );
My result is constantly returning me null. How do i continue from here?
.h:
NSMutableData *responseData;
.m:
- (void)load {
NSURL *myURL = [NSURL URLWithString:#"https://10.124.128.93:8443/axis2/services/C3WebService/completeWithdrawal Transaction?transactionId=%#&password=%#", _transactionID.text, _userPassword.text];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[responseData release];
[connection release];
[textView setString:#"Unable to fetch data"];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[responseData
`enter code here` length]);
NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
}
Consider using NSURLConnection which has a callback for the result and also a callback to get detailed error details. It als doesn't execute on the UI thread (doesn't hang UI during the request).
NSURL *url = [NSURL URLWithString:#"http://www.mysite.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"GET"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Then you can implement the delegate methods to get the error, the data and other details:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString* responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"result: %#", responseString);
[responseString release];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"error - read error object for details");
}
You might try using "initWithContentsOfURL:encoding:error:" instead and check "error". Also, use Charles or other http sniffer and compare results to a straight browser request ( did you check results in a browser?)

How can I make a website as an iPhone native application?

I'm planning to convert my website into an iPhone native application. I'm stuck at the point of how to achieve this.
Initially, I have developed the first screen i.e., LOGIN screen of my application. My server is built in Java. I'm able to send the login credentials to the server and able to see the request on the server. But I'm unable to receive the response from the server for the request I've sent.
A part of my code is:
NSString *post = #"username=";
post = [post stringByAppendingString:username];
post = [post stringByAppendingString:#"&password="];
post = [post stringByAppendingString:password];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://mysite.com/login.action?"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
// [request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
receivedData = [[NSMutableData data] retain];
NSLog(#"In \"LOGIN()\" : receivedData = %#",receivedData);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
NSLog(#"In \"didReceiveResponse()\" : receivedData = %#",receivedData);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
NSString *ReturnStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"In \"didReceiveData()\" : receivedData = %#",receivedData);
NSLog(#"Return String : %#", ReturnStr);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *urlData;
if (nil != receivedData) {
urlData = [[[NSString alloc] initWithData:receivedData
encoding:NSUTF8StringEncoding] autorelease];
}
NSLog(#"In \"connectionDidFinishLoading()\" : urlData = %#",urlData);
[receivedData release];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
[connection release];
[receivedData release];
NSLog(#"Connection failed! Error - %# %#",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
How do I develop a LOGIN application?
I'm sending a request successfully to the server but I'm unable to get the response from the server. How can I overcome this issue?
If your experience is with web development you may be interested in looking at NimbleKit for iPhone. It's a simple XCode plugin that allows you to use HTML and JavaScript to develop the entire native application.