Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-17-2010, 12:45 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 2
Runner1985 is on a distinguished road
Default posting data to server using asihttprequest

Hi All,

My goal is to create an app that sends the number "1" to a webserver which records the number inside a text file hosted there. I'm very new to apps, php, asihttprequest, etc. Research has suggested using the asihttprequest wrapper for its simplicity. I set up a webpage recently using iPower which I would like to communicate with. I have created an app with a button, when pressed activates the following code:

-(IBAction)upload{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.mywebpage.com/write.php"]];
[request setPostValue:@"Ben Copsey" forKey:@"name"];
[request setPostValue:@"ben@allseeing-i.com" forKey:@"email"];
[request setDelegate:self];
[request startAsynchronous];
}


I need help writing a php file that will communicate with the above code and write to a text file. I've been trying to deduce how to make this work from others' broken apps, example code, and thread questions, but it's hard troubleshooting problems with my limited knowledge. I would greatly appreciate if anyone could recommend a tutorial on how to achieve my goal. In addition, I have the following questions:

1. Do the code request setPostValue:@"Ben Copsey" forKey:@"name" send the string in quotations as data to the server?
2. What does "forkey" mean?
3. I asked iPower if I need to change the permissions on my php and text files. The said they are currently set to the default of 644. I'm not sure what this entails. Should this exercise be as simple as uploading the php and text files to the parent directory (http://www.mywebpage.com/write.php) without checking permissions?

I appreciate any help on my project.

Andrew
Runner1985 is offline   Reply With Quote
Old 12-17-2010, 02:36 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

You shouldn't need to change the permissions. Your server will be the one writing to the file, not the iPhone app, so there shouldn't be a problem.

Hope this helps:

Code:
// write.php

<?php
$name = $_POST['name']; // Ben Copsey
$email = $_POST['email']; // ben@allseeing-i.com

$file = "file.txt";
$fh = fopen( $file, 'w' );
$carriageReturn = "\n";
fwrite( $fh, $name );
fwrite( $fh, $carriageReturn );
fwrite( $fh, $email );
fclose( $fh );
Hope that helps. Try to figure out what each line is doing.
thomashw is offline   Reply With Quote
Old 12-18-2010, 03:52 AM   #3 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

Quote:
Originally Posted by Runner1985 View Post
Hi All,

1. Do the code request :setPostValue@"Ben Copsey" forKey:@"name" send the string in quotations as data to the server?
2. What does "forkey" mean?
3. I asked iPower if I need to change the permissions on my php and text files. The said they are currently set to the default of 644. I'm not sure what this entails. Should this exercise be as simple as uploading the php and text files to the parent directory (create a free web site at mywebpage.com) without checking permissions?

I appreciate any help on my project.

Andrew

1) yes, that code prepare the data that will be sent.
2) "for Key", mean that you are storing "Ben Copsey" in a key: "name" (the same concept that use NSDictionary)
3) no, you no need to change anything

for your purpose, 644 mean: permission 6 for User, permission 4 for Group and permission 4 for Other (User\Group\Other are the unix classes for account)
permission 6 = 110 in binary, so 1= Read 1=Write 0=execute for user,
permission 4 = 100 in binary, so 1= Read for group,
permission 4 = 100 in binary, so 1 = Read for other,

and this is the default permissions.
__________________

Last edited by dany_dev; 12-18-2010 at 03:58 AM.
dany_dev is offline   Reply With Quote
Old 12-18-2010, 07:45 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 2
Runner1985 is on a distinguished road
Default

Quote:
Originally Posted by dany88 View Post
1) yes, that code prepare the data that will be sent.
2) "for Key", mean that you are storing "Ben Copsey" in a key: "name" (the same concept that use NSDictionary)
3) no, you no need to change anything

for your purpose, 644 mean: permission 6 for User, permission 4 for Group and permission 4 for Other (User\Group\Other are the unix classes for account)
permission 6 = 110 in binary, so 1= Read 1=Write 0=execute for user,
permission 4 = 100 in binary, so 1= Read for group,
permission 4 = 100 in binary, so 1 = Read for other,

and this is the default permissions.
Thank you both for your help. I got my app running and successfully posted data to my website through the app simulator.

What does this mean, [request setDelegate:self];?

Also, how do I make the server echo back a response to confirm successful data transfer?

Last edited by Runner1985; 12-18-2010 at 08:01 AM.
Runner1985 is offline   Reply With Quote
Old 12-18-2010, 08:41 AM   #5 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

Quote:
Originally Posted by Runner1985 View Post
What does this mean, [request setDelegate:self];?
this mean that the "delegate" for the object "request" will be the istance of class from where you wrote that line of code.

what is Delegate?
Delegation pattern - Wikipedia, the free encyclopedia

Basically delegate is an object that has been assigned as a delegate of another object, and the delegate basically have the task to receive some "events" from the object.
In this case assuming that you are writing on CustomViewController, then you will have something like

CustomViewController.m:
Code:
-(void)myDownloadMethod{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];   //We are on CustomViewController.m, so self represent the current istance of CustomViewController!
   [request setDidFinishSelector:@selector(requestDone:)];
   [request setDidFailSelector:@selector(requestWentWrong:)];
  [request startAsynchronous];
}

- (void)requestDone:(ASIHTTPRequest *)request
{
   NSString *response = [request responseString];
}
 
- (void)requestWentWrong:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}
You are setting the delegate for object request, that delegate is "self" self is a special variable that return a pointer to "the current class"

Why we use Delegate? because the object "request", will call method like
Code:
  [request setDidFinishSelector:@selector(requestDone:)];
   [request setDidFailSelector:@selector(requestWentWrong:)];
that methods (requestDone, requestWentWrong) will be called just on delegate (so in this case you write these methods on CustomViewController.m), if you no set the delegate, that metods will be not called and you aren't able to retrieve the response from server.

Quote:
Originally Posted by Runner1985 View Post
Also, how do I make the server echo back a response to confirm successful data transfer?
in the php script you can add (as last line is fine)
Code:
echo "i saved data!";
and then you can retrieve that on
Code:
- (void)requestDone:(ASIHTTPRequest *)request
{
   NSString *response = [request responseString];

  if([response isEqualToString:@"i saved data!"]){
    NSLog(@"nice.....");
  }
}
__________________

Last edited by dany_dev; 12-18-2010 at 08:50 AM.
dany_dev is offline   Reply With Quote
Old 03-16-2011, 07:57 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 19
snakeninny is on a distinguished road
Default

Hi guys

all of ur posts are helpful to me, but i still get another question:
if i wanna send/upload a local file to server with the following code snippet:
Code:
ASIFormDataRequest *requestUp=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"SomeUploadServer"]];
[requestUp setFile:@"/Users/snakeninny/Library/Application Support/iPhone Simulator/4.2/Library/test.rtf"  forKey:@"rtf"];
[requestUp setDelegate:self];
[requestUp startAsynchronous];
how do i create this server side to receive files that my iPhone sends? im also a rookie in php or any kinds of server... plz lend me a hand and i reeeeeeeeeeeeeeealy appreciate that!
snakeninny is offline   Reply With Quote
Reply

Bookmarks

Tags
client, send, upload

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 391
9 members and 382 guests
chemistry, daudrizek, HemiMG, jeroenkeij, Kirkout, PavelMik, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:59 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0