Problem with hiding modalviewcontroller using ASIHTTPRequest - iphone

I have a problem with hiding modalviewcontroller when I connect to server with ASIHttpRequest.
I connect in background thread and show modalview in main thread.
This is my code:
[self performSelectorInBackground:#selector(loginServerRequest) withObject:nil];
- (void)loginServerRequest {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:#"https://11.111.111.11/api/login"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[AccountSettings getCompany] forKey:#"companyName"];
[request setPostValue:[AccountSettings getEmail] forKey:#"email"];
[request setPostValue:[AccountSettings getPassword] forKey:#"password"];
[request setRequestMethod:#"POST"];
[request setTimeOutSeconds:10];
[request setValidatesSecureCertificate:NO];
[request setDelegate:self];
[request startSynchronous];
[pool drain];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self performSelector:#selector(hideServerConnectView) withObject:nil afterDelay:0.0];
int status = [request responseStatusCode];
NSLog(#"%i", status);
if ([self.nibName isEqualToString:#"RootViewController"]) {
if (status == 200) {
//some code
}
}
}
- (void)hideServerConnectView {
[self.parentViewController dismissModalViewControllerAnimated:NO];
}
If server responses immediately modalviewcontroller doesn't hide!
If pass some seconds then everything is okay.
What's the problem??
I changed my code like this:
[self loginServerRequest];
ServerConnectView *viewC = [[ServerConnectView alloc] init];
[self.view addSubview:viewC.view];
[self presentModalViewController:viewC animated:YES];
[viewC release];
- (void)loginServerRequest {
NSURL *url = [NSURL URLWithString:#"https://11.111.111.11/api/login"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[AccountSettings getCompany] forKey:#"companyName"];
[request setPostValue:[AccountSettings getEmail] forKey:#"email"];
[request setPostValue:[AccountSettings getPassword] forKey:#"password"];
[request setRequestMethod:#"POST"];
[request setTimeOutSeconds:10];
[request setValidatesSecureCertificate:NO];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request{
[self hideServerConnectView];
int status = [request responseStatusCode];
NSLog(#"%i", status);
if ([self.nibName isEqualToString:#"RootViewController"]) {
if (status == 200) {
//some code
}
}
}
- (void)hideServerConnectView {
[self.parentViewController dismissModalViewControllerAnimated:NO];
}
And it didn't solve my problem.
Any ideas? Or something wrong?

You're mixing async and sync methods.
You set up the request as though it's an async request, but then call [request startSynchronous];.
Because of this, the delegate methods will not be called and your modal will not be dismissed.
The fix is to fire off the request async, using [request startAsynchronous];
This also means that you don't need to call performSelectorInBackground (or setup the autorelease pool in the loginServerRequest method).

In your asynchronous version, move [self hideServerConnectView]; just after [self loginServerRequest];
OR use - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait to invoke - (void)hideServerConnectView since UI update must occur on the Main Thread.

Related

not saving when using setDidReceiveDataSelector

i want to download a file and show the progress bar
i was able to do this.
now , i want to show the progress value in a label and use this code to progress init and update label :
[queue setDelegate:self];
[queue setRequestDidFinishSelector:#selector(updateLabel)];
[queue setDownloadProgressDelegate:progress];
[queue setShowAccurateProgress:YES];
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setTemporaryFileDownloadPath:[filePath stringByAppendingString:#".download"]];
[request setAllowResumeForFileDownloads:YES];
[request setDidFinishSelector:#selector(updateLabel)];
[request setDidReceiveDataSelector:#selector(updateLabel)];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setShouldAttemptPersistentConnection:NO];
[request setDownloadDestinationPath:filePath];
[queue addOperation:request];
[queue go];
but not save in the destination path !
and when i clear this code : 
[request setDidReceiveDataSelector:#selector(updateLabel)];
saving done !
what is problem ?
i want to update label text when progress value changed
This is what something you need to do with the Main Thread. Updating the UI of the application is performed by the main thread rather than any of the background thread.
Or
alternatively you can use the below code snippet which works for me :
- (void)fetchThisURLFiveTimes:(NSURL *)url
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addOperation:request];
[request cancelAllOperations];
[request setDownloadProgressDelegate:myProgressIndicator];
[request setDelegate:self];
[request setRequestDidFinishSelector:#selector(queueComplete:)];
[request go];
}
- (void)queueComplete:(ASINetworkQueue *)queue
{
NSLog(#"Value: %f", [myProgressIndicator progress]);
[self performSelectorOnMainThread:#selector(updateLabel) withObject:nil waitUntilDone:NO];
}

UIActivityIndicator

When I start UIActivityIndicatorView using StartAnimating method:
[ActivityIcon startAnimating];
it disable all user interactions so when the user Tap on Cancel button which should abort the download process and hide the UIActivityIndicator it does not work!!!
any suggestions would be appreciated.
Edit:
I am using separate thread to download the files in the background. All progress reporting and UI interaction I made it through:
[self performSelectorOnMainThread:#selector(RefreshScreen:) withObject:nil waitUntilDone:YES];
and RefreshScreen method is the one who interact with the UI elements.
try change this line: [request startSynchronous]; to: [request startAsynchronous];
EDIT
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}

Sending POST request in iPhone using ASIHTTPRequest 1.6.2

I am using ASIHTTPRequest 1.6.2 lib for all http transactions in IPhone. But i dont know, how can i post the data with ASIHTTPRequest in iPhone?
Can you please give me the code snippet which will work in in iphone?
I am using the following code for this. But i am getting response code as 0. Please help me to understand where i am going wrong.
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:inputXml forKey:#"inputXml"];
[request setPostValue:#"qftS6TJN343343V84hw=" forKey:#"key"];
[request setPostValue:#"1.2" forKey:#"version"];
[request setRequestMethod:#"POST"];
[request startAsynchronous];
You should use ASIFormDataRequest:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"first_name"];
[request setPostValue:#"Copsey" forKey:#"last_name"];
[request setFile:#"/Users/ben/Desktop/ben.jpg" forKey:#"photo"];
Here's the how to page. It covers all types of requests such as get, form post, custom post, put, etc...
http://allseeing-i.com/ASIHTTPRequest/How-to-use
Here's how to set it up in your XCode project:
http://allseeing-i.com/ASIHTTPRequest/Setup-instructions
Those instructions and snippets should work in iOS.
Following is the sample code...
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request appendPostData:[reqString dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:60];
[request startAsynchronous];
You will get detail guidance from here.
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:]];
NSMutableDictionary *dicData = [[NSMutableDictionary alloc] initWithCapacity:1];
[dicData setObject:txtPassword.text forKey:#"Password"];
[dicData setObject:txtEmailID.text forKey:#"Email"];
[request setPostBody:[[[self parseJsonFromObject:dicData] dataUsingEncoding:NSUTF8StringEncoding] mutableCopy]];
[request setRequestHeaders:[NSMutableDictionary dictionaryWithObjectsAndKeys:#"application/json", #"Content-Type", nil]];
[request setDelegate:self];
[request setDidFinishSelector:#selector(signUpWithEmailFinish:)];
[request setDidFailSelector:#selector(signUpWithEmailFail:)];
[request startAsynchronous];
(void)signUpWithEmailFinish:(ASIHTTPRequest *)request
{
if (request.responseStatusCode == 200)
{
NSDictionary *responseMessage = [self objectFromJson:request.responseString];
NSLog(#"ResponseMEssage=%#",responseMessage);
if (responseMessage)
{
if ([responseMessage objectForKey:#"user"] == nil)
{
NSLog(#"duplication not allowed");
[self animation:0];
return;
}
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_LOGIN_SUCCESS object:nil];
NSLog(#"Registration Complete");
UserLoginPage *userLogin=[[UserLoginPage alloc]init];
[self.navigationController pushViewController:userLogin animated:YES];
return;
}
}
}

Iphone ASIHTTP setDidFinishSelector issue

i have the following code:
- (void)imageDownloaded:(ASIHTTPRequest *) request idDisco:(NSNumber *)iddisco
NSLog(#"%d",[idPlace intValue]);
}
And
NSURL *url = [NSURL URLWithString:[item objectForKey:#"image"]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:[self performSelector:#selector(imageDownloaded:idDisco:)withObject:request withObject:disco.id_disco]];
But the compiler tells me that "passing argument 1 of setDidFinishSelector from incompatible pointe type"
It works fine but i don't know what i'm doing wrong.
Thanks
You have to pass #selector(someMethod:) there:
And the signature of the method should be
- (void)methodName:(ASIHTTPRequest *)request;
Look at this example:
- (IBAction)grabURLInTheBackground:(id)sender
{
if (![self queue]) {
[self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
}
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[[self queue] addOperation:request]; //queue is an NSOperationQueue
}
- (void)requestDone:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
}
- (void)requestWentWrong:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}

ASIHTTPRequest - HTTPS

Does ASIHTTPRequest support HTTPS connections? My connection right now works for a HTTP connection and errors if I try a HTTPS Connection. (Goes into requestFailed and gives me a ASIHTTPErrorRequestDomain)
-(void) getData
{
av.hidden = NO;
[av startAnimating];
NSString *urlString = [IP stringByAppendingString:#"Method1"];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSLog(#"URL = %#",url);
[request setRequestMethod:#"POST"];
[request setPostValue:#"val1" forKey:#"key1"];
[request setPostValue:#"val2" forKey:#"key2"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
//NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
[self parseData:responseData];
[av stopAnimating];
av.hidden = YES;
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
[av stopAnimating];
av.hidden = YES;
}
Thanks,
Teja
Whoops, sorry, figured it out -
[request setValidatesSecureCertificate:NO] works for reference.
Thanks to these guys - http://www.iphonedevsdk.com/forum/iphone-sdk-development/29417-asihttprequest-library-works-https.html
EDIT: Since this is getting some upvotes, I'd just like to add that this might not be the best approach for valid SSL certificates. The one I was using was a self-signed certificate, so this was fine.