I'm having trouble figuring this out, or finding an easy solution. I figured out the downloading part I think from the apple docs. It seems simple. Here's my code:
Code:
- (void)viewDidLoad
{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.myschool.edu/~myuserid/test.rtf"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection)
{
receivedData=[[NSMutableData data] retain];
}
else
{
// inform the user that the download could not be made
}
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
[receivedData release];
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
[receivedData release];
[connection release];
}
I feel like their should be just as simple code for uploading to a server but I can't seem to find any on Apple docs. Maybe a "didSendData" instead of a "didReceiveData" method? I imagine I'd also have to pass my login information as well. Any tips?
I've stumbled upon a few things but nothing that seems to be that simple. Is there no solution equivalent to the downloading example from the apple docs? No NSURLRequest/Connections?
You do it basically the same way, but the "data" you're uploading gets encoded into the URL request. I've never done an async request like you did. Here's how I do it (synchronously):
Thanks for the response. What makes mine asynchronous and yours synchronous? I just got my download code from the apple website. Is one way better than the other?
Also looking at the code, it looks like you're making a string out of your login information and some other strings. What does that actually do? I don't really understand how a lot of this works. I need to actually upload a file.
Thanks for the response. What makes mine asynchronous and yours synchronous? I just got my download code from the apple website. Is one way better than the other?
I'm using sendSynchronousRequest:, you're using initWithRequest:. In mine, it will sit in that method until it's done. Which is not a good thing for a large transfer, but mine is quite small. For a fair amount of data, yours is better.
Quote:
Originally Posted by jcafaro10
Also looking at the code, it looks like you're making a string out of your login information and some other strings. What does that actually do? I don't really understand how a lot of this works. I need to actually upload a file.
That's the actual data. Does it really need to be a file when it gets to the server, or is your server side going to read the data and do something with it? How are you creating the file on the phone in the first place?
There are ways to encode actual files into an POST request, but you'll need to look up the appropriate parameters to set in the NSURLRequest object. It's much more complicated. I found this example with a quick Google search: Cocoabuilder - (Ben Lachman) Re: File upload with NSURLRequest fails
Without giving too much away, I have a program. It creates some data in the form of objects. I want to store those objects (an array of them) on the server so that I can download them later, make changes, and put them back. Is this a viable way to achieve this? Right now I'm doing everything locally on the phone. I'm just archiving it and unarchiving it. I'm looking to replace that with uploading and downloading from the server.
Without giving too much away, I have a program. It creates some data in the form of objects. I want to store those objects (an array of them) on the server so that I can download them later, make changes, and put them back. Is this a viable way to achieve this? Right now I'm doing everything locally on the phone. I'm just archiving it and unarchiving it. I'm looking to replace that with uploading and downloading from the server.
Sure. How big will these objects be? You could just archive your array of objects to an NSData object, do an urlEncode on it, and plug it into the code I already posted.
It'll probably take you longer to write the code on the server side to handle the HTTP POST request and save the data.
Sure. How big will these objects be? You could just archive your array of objects to an NSData object, do an urlEncode on it, and plug it into the code I already posted.
It'll probably take you longer to write the code on the server side to handle the HTTP POST request and save the data.
joe
Hmm. Thats the problem I think. I was going to ask how the server knows where the login/password information is. See, the server I'm using is my schools. They give us some web space which I can access by going to http://www.myschool.edu/~myusername and download files from there. I guess I was just hoping there was some generic way to upload and store data on a server.
Hmm. Thats the problem I think. I was going to ask how the server knows where the login/password information is. See, the server I'm using is my schools. They give us some web space which I can access by going to http://www.myschool.edu/~myusername and download files from there. I guess I was just hoping there was some generic way to upload and store data on a server.
Can you run PHP scripts from that server space? If so, that's what you need to do. Or Perl, but I find PHP easier for anything except heavy duty string manipulation. There's no way to upload files to a webserver without some cooperation from the server.
It's also possible someone has implemented an FTP client class for the iPhone.
Looks like this might be what you need for the uploads, assuming you have FTP access to that server. Can't imagine how else you would use it, if you didn't.
Can you run PHP scripts from that server space? If so, that's what you need to do. Or Perl, but I find PHP easier for anything except heavy duty string manipulation. There's no way to upload files to a webserver without some cooperation from the server.
It's also possible someone has implemented an FTP client class for the iPhone.
joe
I don't think I can run php scripts. I don't have any experience with php. I know the server uses something called filedrawers to provide a web-based way to upload files which I can access at webfiles.myschool.edu. But I can also use SSH Secure File Transfer by logging into one of the schools machines.
For testing lets say I set up a simple Apache server on my own computer. Then I could use the code you gave me to upload data from it, and use the code I got from the apple website to download data from it. How do I write php code to do the rest though? I don't know much about apache or php or anything. All I know is I have xampp installed and I can make an apache server that I can access on my local network.
Can I just download a php upload script and hope that it works or will I have to tailor it specifically to make it work with the IPhone? Most of the php upload scripts I've seen tend to be made for html forms and since I'm not really too familiar with any of that, I'm not sure if I can just pretend my IPhone data came from an html form somehow.
Bump. Still looking for an answer on the php side of things. Are there php iphone scripts that will handle storing stuff on a server with php?
I think the reason you're not getting an answer is that this is not an iPhone specific question. It's a question about how to do the server side of a client-server interaction. You client side could be an app written for iPhone OS, Mac OS X, Windows, anything.
You need to decide how you want to transfer the data (JSON, XML, something else), then see if you can find some example code in whatever server language you want (PHP, I think) to do that. Most probably on a PHP board. Once you have that working (testing it by calling the PHP app from a web browser), then someone here could probably help if you have problems implementing the iPhone code to talk to the server.
Ah, one of my posts is missing. I made a PHP upload script (borrowed a php upload script) that's very simple:
Code:
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
I just want it to store a file.
Now on my IPhone is where I have the question. What code goes here? I got myself started by trying to use some of the code you showed me but I'm a bit stuck:
submit.php
* have this post to post.php
* set a test variable like an upload field for images.
post.php
* during the learning phase have post.php be the following:
Code:
<?php phpinfo(); ?>
Look at the POST variables.
You can also use print_r() to print array values.
Once you see the data that's posted (IF it is posting correctly) then decide what to do with it. PHP's docs have plenty of examples, i.e.
PHP: move_uploaded_file - Manual should have some example code on how to move the files where you want (it looks like you are already looking at this aspect... search for the tizag.com tutorial).
If your school limits you, you may have to shell out a few bucks a month for a cheap hosting account.
I appreciate your advice and normally I would completely agree that I'd need to go ahead and learn some php, but at this point, I have about a week of school left and need to go ahead and finish this project. I think I did the php part correctly considering it's just an upload script I found. If I don't know how to post stuff from my iphone though, then I'm not sure it matters how much PHP I know if I can't even use it. I have some books on PHP that I've been going through in my spare time so I am making an attempt to learn it.
I have some NSMutableData, I need to figure out how to encode it correctly, and then turn it into postData.
Code:
NSString *post = [NSString stringWithFormat:@"uploadedfile=%@",
[self urlEncodeValue:data]
];//this is the confusing part
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
The biggest problem you have right now is that the PHP script you want to use is assuming you're uploading a FILE. But you're not. You're POSTing a form with form variables. Actually uploading a FILE takes a lot more setup on the client (iPhone) end.
Ok, well at least now I know why it's not working. Is there an easy/straight forward guide to file uploading? Is it something you could show me how to do? Maybe with some sample code? I didn't have much luck in searching the apple docs but maybe I'm not searching in the right place.
Quote:
Originally Posted by FlyingDiver
The biggest problem you have right now is that the PHP script you want to use is assuming you're uploading a FILE. But you're not. You're POSTing a form with form variables. Actually uploading a FILE takes a lot more setup on the client (iPhone) end.
You just need to access the $_POST[] data at the start of your php script in order to access the data you are uploading via the obj-c code that you are using. Just place the post variable name in single quotes in side the square brackets.
e.g.
<?php>
$myVar=$_POST['myPostedData'];
<?>
You can then use the myVar variable which will contain the input sent as 'myPostedData' in the obj-c code.
If you are sending a file then you need different obj-c code to send it.
PS If anyone wants a job coding a simple iphone app that I haven't time to do then drop me an email. Just sending 2 post items to a PHP web server and formatting/displaying the 3 response data on iphone. No need to be pretty as I have time to edit the xib file. It would pay a little if the code is good.
What do you want to upload? It likely makes a difference as to the best way to approach it.
Actually, he did, when he posted this:
Quote:
I have some NSMutableData, I need to figure out how to encode it correctly, and then turn it into postData.
jcafaro10 - since the data you have is in a NSData object, forget about uploading a file. You don't have one now, and it's more trouble than it's worth to create one just to do the upload as a file.
spangat has the right idea. Use the code I posted earlier to create a POST URL that has your NSData encoded into a form variable. Then use spangat's PHP code to get started on receiving it in the form submission. Once you have it in a PHP string variable, you can write that to a file on the server.
Quote:
I have some NSMutableData, I need to figure out how to encode it correctly, and then turn it into postData.
That doesn't tell us anything. He has some quantity (how many?) of bytes that he wants to store on the server and retrieve. We knew that without him saying it. What ELSE would you upload to a server than "some quantity of bytes"?
Anyway, looking back through the responses, I see it is application objects that he wants to store on the server and later retrieve.
The problem he has is really not on the iPhone side. It is, as others have stated, that he needs to learn something about server-side scripting, and needs to gain access to a server that will let him do scripting and store data. It doesn't sound as if the server he is currently using will permit him to do that.
No, web servers do not simply, by default, accept any data you send to them and store it somewhere! There has to be a program (script) installed on the server to do that. There's no generic way to do this.
You need to write a "web service" that will authenticate the user, accept uploads, and send downloads. You need to design a "protocol" for this, which can be as simple as designated URLs for performing the various functions (authenticate, upload, download, etc.). (This is referred to as REST. But there are other schemes.)
Most here are trying to solve the problem of how to encode the data for transmission, but that is a non-problem at this point. First, he needs a server that is suitable for the job, a server-side script, and he needs to learn how to write the script. You can encode all day, but it's not going to help if there isn't some script there on the server to do something with it!
I know that he said that he needs to know how to encode the data. However, it is not uncommon for people to not really know what they need to know to solve a problem, or to really understand what the problem is. (I am not exempt from this!) Later discussion revealed that he was assuming that if he could successfully encode the data, that somehow it would be magically stored on the server, and then he could get it back. Wrong problem.
That is why I suggested stepping back, and fully describing the application.
Exactly what he is doing with the data and what is represents IS important. It will influence the design of the web service.
I urge people to step back and look at the bigger picture, rather than home-in on some minutiae that is not going to help at this point.
Finally, I note that he said that he was using a web server "at school". Unless this is just a personal project, or something only for students of his school (and with approval of the school) they probably wouldn't appreciate their server being used as a back-end for an iPhone application! You can get inexpensive commercial web hosting that will allow you to write scripts, and perhaps that is part of the solution.