Login to system in iphone. - iphone

I am new in iphone internet programming. I am writing an app which has login screen.
I am using ASIHTTPREQUEST, this is the link myurl.com/index.php what I want to login. In chrome, I looked at from the developer console this url posts to myurl.com/ajax/login.php when I login the system with username and password.
Anyway, when I click the login button after entering username and password in app, it doesn't login the system.It prints the FAIL:1 ( when I write myurl.com/ajax/login.php, FAIL:1 seen in the browser.)
Detailed description of my problem:
"There is an login screen in myurl.com/index.php. And in web browser, if I write my username and passw, It posts to myurl.com/ajax/login.php ( from the developer console). Anyway, in app I have written myurl.com/index.php as a url but when I request it prints the html elements of that page in console. If I write myurl.com/ajax/login.php as a url, it prints FAIL:1 in console. ( normally in web browser I have written myurl.com/ajax/login.php, it gives an FAIL:1)"
Edit: I have solved problem, "forKey: #"username" need to be equal "forKey: #"ID" and "forKey: #"password" need to be equal "forKey: #"PWD". Because in developer console username and password returned as a ID and PWD. Thanks for comment and answer.
This is the code:
-(IBAction)login:(id)sender
{
NSURL *url = [[NSURL alloc] initWithString:#"https://myurl.com/index.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setTimeOutSeconds:120];
[request setPostValue:studentid.text forKey:#"username"];
[request setPostValue:starspassword.text forKey:#"password"];
[request setDelegate:self];
[request startAsynchronous];
[url release];
}
-(void) requestFinished:(ASIHTTPRequest *)request
{
NSString *responseString = [request responseString];
UIAlertView *alert= [[[UIAlertView alloc] initWithTitle:#"Success" message:#"http works"
delegate:self cancelButtonTitle:#"okay" otherButtonTitles:nil] autorelease];
[alert show];
NSLog(#"SUCCESS: %#", responseString);
//Request succeeded
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}
-(void) requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(#"ERROR: %#", [error localizedDescription]);
UIAlertView *alert= [[[UIAlertView alloc] initWithTitle:#"fail" message:#"http fails" delegate:self cancelButtonTitle:#"okay" otherButtonTitles:nil]autorelease];
[alert show];
//Request failed
}
Sorry for my english, there are some examples about this topic but I didn't solve this problem. Thanks for your advice.

Have you tried this instead? ASI includes a way to do basic authentication
[request setUsername:studentid.text];
[request setPassword:starspassword.text];
Scroll down to Handling HTTP Authentication
http://allseeing-i.com/ASIHTTPRequest/How-to-use

Related

Uploading video on YouTube

I need to upload video from my iphone app to youtube, i have got the access token form google oauth but getting problem while uploading video to youtube, for post request to upload video i am using ASIFormDataRequest. Below is my code, please help me to resolve my mistake, thanks in advance.
- (IBAction)buttonTouched:(id)sender
{
NSLog(#"%d %#",[sender tag],[[NSUserDefaults standardUserDefaults] valueForKey:#"access_token"]);
progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30, 90, 200, 20.0)];
[alertView addSubview:progressView];
[alertView show];
[progressView setProgress:0];
NSString *name=[videosToShow objectAtIndex:[sender tag]];
NSString *newFilePath = [NSString stringWithFormat:#"%#.mov", [self.path stringByAppendingPathComponent:name]];
NSLog(#"Content URL :%#;", newFilePath);
NSURL *url = [NSURL URLWithString:#"https://uploads.gdata.youtube.com/feeds/api/users/default/uploads"];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setRequestMethod:#"POST"];
NSLog(#"Auth Token== %#",[[NSUserDefaults standardUserDefaults] valueForKey:#"access_token"]);
[request setPostValue:[[NSUserDefaults standardUserDefaults] valueForKey:#"access_token"] forKey:#"authentication_token"];
[request setPostValue:devKey forKey:#"developer_key"];
[request setPostValue:#"abcabc" forKey:#"boundary_string"];
[request setPostValue:devKey forKey:#"content_length"];
NSString *path = [[NSBundle mainBundle] pathForResource:#"xmlcontent" ofType:#"txt"];
NSString *strXml = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[request setPostValue:strXml forKey:#"API_XML_Request"];
[request setPostValue:#"video/mov" forKey:#"video_content_type"];
[request setPostValue:#"video/mov" forKey:#"Binary File Data"];
[request setFile:newFilePath withFileName:#"1.mov" andContentType:#"video/mov" forKey:#"uploaded"];
[request setUploadProgressDelegate:self];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setDidFailSelector:#selector(requestFailed:)];
[request setShowAccurateProgress:YES];
[request startAsynchronous];
}
- (void)requestFinished:(ASIFormDataRequest *)request {
[alertView dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Congrats!!!" message:#"File has been downloaded."
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)requestFailed:(ASIFormDataRequest *)request1 {
NSError *error = [request1 error];
NSLog(#"%#", error);
}
Below is the file xmlcontent.txt
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
<media:title type="plain">Bad Wedding Toast</media:title>
<media:description type="plain">
I gave a bad toast at my friend's wedding.
</media:description>
<media:category
scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
</media:category>
<media:keywords>toast, wedding</media:keywords>
</media:group>
</entry>
Below is the error that i am getting
Error Domain=ASIHTTPRequestErrorDomain Code=3 "Authentication needed"
UserInfo=0x1c8ed0 {NSLocalizedDescription=Authentication needed}
You are using the GData library but haven't linked/added it. Check out GData Objective-C Client Setup in XCode 4 on how to build/link the GData library correctly.

How to send POST value to website?

I need to submit value from my application via UITextField and I want this value to show on website that i sent request to. I use ASIHTTPRequest to send request to website. i tried something like this:
NSURL *url = [NSURL URLWithString:#"http://www.project4hire.com/freelance_job_16265.html"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
//
[request setPostValue:priceField forKey:#"bid"];
[request setPostValue:dayField forKey:#"days2c"];
[request setPostValue:commentField forKey:#"comment"];
[request setPostValue:#"1" forKey:#"notify"];
[request setPostValue:#"placebid" forKey:#"Place Bid >>"];
[request setPostValue:#"e6fb12104854e6e9" forKey:#"suid"];
[request setPostValue:#"placebid" forKey:#"a"];
[request setPostValue:#"16265" forKey:#"pid"];
[request setDelegate:self];
[request setDidFailSelector:#selector(requestBidFailed:)];
[request setDidFinishSelector:#selector(requestBidFinished:)];
[request startAsynchronous];
}
- (void)requestBidFailed:(ASIHTTPRequest *)request
{
//notify user
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Error sending request to the server" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
- (void)requestBidFinished:(ASIHTTPRequest *)request
{
NSLog(#"Status: %d", request.responseStatusCode);
NSLog(#"string: %#",request.responseString);
}
Here are the Bid Form:BidForm
Here are request and response header:Header
I got response 200, but the value that i sent not show on the website. Can anyone advice me?
Thanks
I noticed you are posting to a HTML file. Unless you have some special setup to allow The HTML files to be executable, the html file will not process the data you posted to it. Is it that only one value is not showing or are all values not showing. If all values are missing, then what I stated initially is correct and you will need to use something like PHP, CF, Perl or whatever language you want to receive the data you are posting from your app.
I had the same problem, but mine was working on simulator but it was not working on device then I read an article that it shows the founders of the ASIHTTPRequest API no longer update their library (I dont know if that article was reliable or not), so I decided to use an updated library which is RestKit. You can download and set it up from this website: restkit.org, if you have any problem for installing you can ask me to help you. here are the simple code for posting on restkit library:
- (void)post
{
[RKClient clientWithBaseURLString:#"http://www.project4hire.com"];
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
priceField, #"bid",
dayField, #"days2c", nil];
[[RKClient sharedClient] post:#"/freelance_job_16265.html" params:params delegate:self];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSRange range = [[error localizedDescription] rangeOfString:#"-1012"];
if (range.length > 0){
//Do whatever here to handle authentication failures
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Error sending request to the server" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
RKLogError(#"Hit error: %#", error);
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
if ([request isGET]) {
// Handling GET /foo.xml
if ([response isOK]) {
// Success! Let's take a look at the data
NSLog(#"Retrieved XML: %#", [response bodyAsString]);
}
} else if ([request isPOST]) {
// Handling POST /other.json
if ([response isJSON]) {
NSLog(#"Got a JSON response back from our POST!");
}
} else if ([request isDELETE]) {
// Handling DELETE /missing_resource.txt
if ([response isNotFound]) {
NSLog(#"The resource path '%#' was not found.", [request resourcePath]);
}
}
NSLog(#"HTTP status code: %d", response.statusCode);
NSLog(#"HTTP status message: %#", [response localizedStatusCodeString]);
NSLog(#"Header fields: %#", response.allHeaderFields);
NSLog(#"Body: %#", response.bodyAsString);
}

ASIHTTPRequest login page

I try to make an iphone application that can login to the web application that use the https securing the user information.Now i am stuck in the login page. I don't know how to check the real account in the website of user when logging in by my application. I got the response only 200 even if i put the wrong account.
here is my code:
- (IBAction)clickOK:(id)sender {
NSURL *url = [NSURL URLWithString:#"https://www.freelancer.com/users/login.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setUseKeychainPersistence:YES];
[request setDelegate:self];
request.shouldPresentCredentialsBeforeChallenge = YES;
[request setRequestMethod:#"POST"];
[request setPostValue:usernameField.text forKey:#"username"];
[request setPostValue:passwordField.text forKey:#"passwd"];
request.timeOutSeconds = 30;
[request setDidFailSelector:#selector(requestLoginFailed:)];
[request setDidFinishSelector:#selector(requestLoginFinished:)];
[request startAsynchronous];
}
- (void)requestLoginFailed:(ASIHTTPRequest *)request
{
//notify user
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Error sending request to the server" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
- (void)requestLoginFinished:(ASIHTTPRequest *)request
{
int statusCode = [request responseStatusCode];
NSString *statusMessage = [request responseStatusMessage];
NSLog(#"StatusCode: %d", statusCode);
NSLog(#"StatusMessage: %#", statusMessage);
}
Can anyone suggest me how to check the real account of this website and keep user login?
Thanks for any help
You've started something Asynchronously
[request startAsynchronous];
And then just expected it to return something straight away:
NSLog(#"%#",[request responseString]);
NSLog(#"%d",[request responseStatusCode]);
You need to put something like this:
[request setDidFinishSelector:#selector(didFinishRequest:)];
And then move your NSLogs and any other stuff that leeches off of the response into that method.

ASI HTTP Request for posting a review

In my app I am sending a ASI HTTP post request so I user can post a review to my web service however I'm having some trouble sending the request it always seems to be failing I'm sure its a simple error I'm over looking but its driving me made.
My Create Review Class is as follows:
-(void)Submit
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://furious-ice-356.heroku.com/places/%#/reviews.xml",self.identifier]];
//[NSURL URLWithString:[NSString stringWithFormat:#"https://furious-ice-356.heroku.com///places/%#/reviews.xml",self.identifier]];
self.bestnight.text = #"Monday";
ASIFormDataRequest *request1 = [ASIFormDataRequest requestWithURL:url];
[request1 setUsername:self.username];
[request1 setPassword:self.password];
[request1 setRequestMethod:#"POST"];
[request1 setPostValue:self.bestnight.text forKey:#"review[best-night]"];
[request1 setPostValue:self.comments.text forKey:#"review[comments]"];
[request1 setPostValue:self.rating.text forKey:#"review[rating]"];
[request1 setDelegate:self];
[request1 startAsynchronous];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(#"FINISH Response received: %#", [request responseString]);
UIAlertView * av = [[[UIAlertView alloc] initWithTitle:#"Thank You" message:#"Your review was successfully posted. Thank You for making our App Better." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] autorelease];
[av show];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(#"FAILED Response received: %#", [request responseString]);
}
Try adding:
[request setShouldPresentCredentialsBeforeChallenge:NO];
Or if that doesn't work, try calling the same but passing 'YES'. Also as others have said double check your username/password.

login to a website in iphone

i am developing one app which will login to vonage website and do some functions like call froward etc . For that purpose first i am trying to login to vonage website
i did these steps
1) Found the login url of vonage
https://secure.vonage.com/vonage-web/public/login.htm
2) used livehttpheader addon to capture outgoing post request data when i clicked sigin
the data i found is
username=myusername&password=mypassword&redirectingURL=%2Fwebaccount%2Fdashboard%2Fchoose.htm&frontDoorLogin=&goToSelection=callforwarding&loginsubmit=Sign+In
after i got my data i started coding in xcode , i dont know what is best way to do it but i searched for sometime then i tried like this
-(IBAction) buttonpressed {
NSURL *url = [NSURL URLWithString:#"https://secure.vonage.com/vonage-web/public/login.htm"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setValue:#"myuser" forKey:#"username"];
[request setValue:#"mypass" forKey:#"password"];
[request setValue:#"/webaccount/dashboard/choose.htm" forKey:#"redirectingURL"];
[request setValue:#"" forKey:#"frontDoorLogin"];
[request setValue:#"callforwarding" forKey:#"goToSelection"];
[request setValue:#"Sign+In" forKey:#"loginsubmit"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIFormDataRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:#"Hmm" message:#"http works" delegate:self cancelButtonTitle:#"okay" otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(#"Output = %#",responseString);
}
- (void)requestFailed:(ASIFormDataRequest *)request
{
NSError *error = [request error];
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:#"fail" message:#"http fails" delegate:self cancelButtonTitle:#"okay" otherButtonTitles:nil];
[alert show];
[alert release];
}
but when i ran app in my simulator it crashed .
am i doing correct way in posting data ? and is there any other way to do this ?
can any one help me in this issue
i am using asihttprequest library
thanks in advance
regards
Edit : My blind mistake i types setvalue instead of setpostvalue ,this will solve this issue :)
look at the section titled Sending a form POST with ASIFormDataRequest
// using post value
[request setPostValue:#"myuser" forKey:#"first_name"];
also I usually do my alerts with an autorelease, not sure if that is your problem but you might want to try that changes also
UIAlertView *alert= [[[UIAlertView alloc] initWithTitle:#"Hmm" message:#"http works"
delegate:self cancelButtonTitle:#"okay" otherButtonTitles:nil] autorelease];
[alert show];
UIAlertView Delegate Code
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:
(NSInteger)buttonIndex {
// do your thing
}