Session Maintenance for Login Page - cocoa

My Requirement: I want to send login details(email, password) to server, have to maintain session for valid username.
How to create & maintain a session using "NSURLConnection" of iphone SDK?
If you found any Tutorials/Code snippets on this issue,Please post the corresponding linke here.
Advance thanks for your help.

NSString *urlString = [NSString stringWithFormat:#"http://www.mywebsite.com/login.php?email=username#yahoo.com&pwd=pass"];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];

Related

App terminates when using NSURL with parameters

In my app I sent a request to my PHP file to download some info. Whenever I use the code below it will terminate:
-(void)downloadItems
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:#"commentsId"];
NSLog(#"The comments id is : %#",myString);
// Download the json file
NSString *urlstring = [NSString stringWithFormat:#"http:/MyWebSite.com/checkphp.php?title=%#", myString];
NSURL *jsonFileUrl = [NSURL URLWithString:urlstring];
NSLog(#"The qasida id is: %#", jsonFileUrl);
// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
But if I remove myString from urlString like this:
NSURL *jsonFileUrl = [NSURL URLWithString:#"http:/myWebSite/checkphp.php?title=%#"];
the app will not terminate and it will retrieve data.
Can any one tell me what is wrong with my code?
Thanks

Differentiate between NSURLConnection objects in delegate

I have two request starting one after the other. Starting request like this
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.com"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
NSURLConnection * connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[connection start];
and another request starting like this.
NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.apple.com"]];
NSURLRequest *request1 = [NSURLRequest requestWithURL:url1 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
NSURLConnection *connection1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
[connection1 release];
How can i differentiate between these two in delegate method?
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{}
Don't want to keep any extra class variable for this purpose.
It's Simple :
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection == connection1)
{
//It's for connection1.
}
else if (connection == connection2)
{
//It's for connection2.
}
}
You can go through this Beautiful SO Question : Managing multiple asynchronous NSURLConnection connections
take your NSURLConnection objects in .h file and check in your delegate method as Markus has suggested.
or
Subclass your NSURLConnection and then you can add tag property to the connection class while creating, in you delegate methods check for appropriate tag. You can find working tutorial here.

Dynamic UIWebView Link

So normally to load a UIWebView I do..
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
However, now I need to do this dynamically, depending on a table cell that a user clicks. I load the table cells from JSON and everything works fine. Now I need to get the "id" of the post in the cell, and load it dynamically. Currently I've tried this but it doesn't work...
NSURL *url = [NSURL URLWithString:#"http://www.google.com/story.php?id="];
url = [NSURL URLWithString:storyId relativeToURL:url];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
Any tips?
Why don't you just format the URL string?
NSURL *url = [NSURL URLWithString:[NSString
stringWithFormat:#"http://www.google.com/story.php?id=%#", storyId]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];

How to give two json request to two different api at a time from one page and from one method

I am working on my new iPhone application,in that I need the datas and addresses from one url and from anther url I need to get the images. These two results are in the form of json response data but I am getting only the result for the datas....
Here is my code
- (void)viewDidLoad
{
jsondataimg=[[NSMutableString alloc] initWithString:#""];
#####for this url i'm not getting the result##########
NSString *urlimg = [NSString stringWithFormat:#"https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%#",name];
//NSLog(#"%#",urlimg);
NSURL *url1= [NSURL URLWithString:urlimg];
NSURLRequest *request1 = [[NSURLRequest alloc] initWithURL: url1];
NSURLConnection *connection1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
jsonData = [[NSMutableString alloc] initWithString:#""];
NSString *urlString = [NSString stringWithFormat: #"https://maps.googleapis.com/maps/api/place/details/json?reference=%#&sensor=false&key=your key",selectedname];
NSLog(#" the name is%#",selectedname);
NSLog(#" the reference is%#",selectedrefer);
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];
[connection1 release];
[request1 release];
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString *partialData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *partialData1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[jsondataimg appendString:partialData1];
[jsonData appendString:partialData];
[partialData release];
[partialData1 release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
# pragma mark for fetching the image urls
NSDictionary *imageurls=[jsondataimg JSONValue];
NSDictionary*images=[imageurls objectForKey:#"responseData"];
NSLog(#"%#",images);
##########here null value is displaying#######
#pragma mark for fetching the datassss
NSDictionary *filesJSON = [jsonData JSONValue];
NSDictionary *address1 = [filesJSON valueForKey:#"result"];
NSLog(#"Found %#",address1);
}
Just use this no need to set delegate methods nsurl connections.. you will get the data...and same for next one.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=mumbai"]]];
NSURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *strResponse = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// NSLog(#"%#",strResponse);
SBJSON *sbJason = [[SBJSON alloc] init];
NSMutableDictionary *getPlaceList = [sbJason objectWithString:strResponse];

leaks when using NSData, NSURL,NSMutableURLRequest,NSURLConnection and sendSynchronousRequest

I am getting the leak at this line in below code" NSData *returnData = [NSURLConnection ..........."
NSURL *finalURL = [NSURL URLWithString:curl];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:finalURL
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[theRequest setHTTPMethod:#"GET"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
BOOL enabled = [self getAutoGenerateObject:returnData];
return enabled;
please help me out of this problem.
Thank You,
Madan Mohan
You will need to release the returnData. That's why in Apple's examples in 'URL Loading Programming Guide / Using NSURLConnection', the returnData is assigned to an iVar and release in dealloc or connectionDidFinishLoading in the case of Asynchronous communication.
Depending what operation you are performing in you method getAutoGeneratedObject, but in theory it can take ownership there.
You could also mark returnData as autoreleased, but that is not always recommended especially if the response data is large.
NSURL *finalURL = [NSURL URLWithString:curl];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:finalURL
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[theRequest setHTTPMethod:#"GET"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
BOOL enabled = [self getAutoGenerateObject:returnData];
[returnData release];
return enabled;

Resources