Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Simple Writing to Text File Web Server - iPhone SDK

I have a website server and I am trying to get it to communicate with my app. I am able to 'download' from the server to read text files and use them as NSStrings, but I was wondering how I could go the other way around. I am trying to 'upload' a short NSString to my server by either:

-Adding a line to an existing text file
-Creating a whole new text document and uploading that

I am guessing I need will need somehow need to use the FTP login details, but otherwise I am lost as to how to start.

Any help would be much appreciated.
Tagged:

Replies

  • appdesignifyappdesignify Posts: 38New Users
    What type of server do you have? Is it hosted on a linux box with PHP?

    I am assuming it is and if that is the case you can write a simple php file that will take in a url parameter to write to your server.

    You could write a php file called say... writetext.php

    <?php
    $var = $_REQUEST['myvar'];
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, "my posted var" .$var ."\n");
    fclose($fh);
    ?>
    Then from within your app you could do something like this:

    NSString *host = @"http://www.example.com/writetext.php?myvar=%@&quot;;
    NSString *urlString = [NSString stringWithFormat:host, @"text I want to write to a file"]
    NSURL *url = [NSURL URLWithString:host];
    NSMutableRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    You would probably want to use a library like ASIHTTPRequest or since that library is not being supported any longer this one is probably better https://github.com/gowalla/AFNetworking.

    You also may notice that in our examples we are using synchronous requests, but this is really only for simplifying our example code for demonstration purposes. In general, you should really use asynchronous requests because synchronous requests will lock up your user interface for the duration of the request.

    Of course you could always use a service like www.parse.com for cloud storage API too

    Let me know if you have any questions
    Post edited by appdesignify on

    iPhone App Templates

    app templates
    image

  • iminichrispyiminichrispy Posts: 11New Users

    What type of server do you have? Is it hosted on a linux box with PHP?

    I am assuming it is and if that is the case you can write a simple php file that will take in a url parameter to write to your server.

    You could write a php file called say... writetext.php


    $var = $_REQUEST['myvar'];
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, "my posted var" .$var ."\n");
    fclose($fh);
    Then from within your app you could do something like this:

    NSString *host = @"http://www.example.com/writetext.php?myvar=%@&quot;;
    NSString *urlString = [NSString stringWithFormat:host, @"text I want to write to a file"]
    NSURL *url = [NSURL URLWithString:host];
    NSMutableRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    You would probably want to use a library like ASIHTTPRequest or since that library is not being supported any longer this one is probably better https://github.com/gowalla/AFNetworking.

    You also may notice that in our examples we are using synchronous requests, but this is really only for simplifying our example code for demonstration purposes. In general, you should really use asynchronous requests because synchronous requests will lock up your user interface for the duration of the request.

    Of course you could always use a service like www.parse.com for cloud storage API too

    Let me know if you have any questions
    I get multiple errors when I enter the code into Xcode. I fixed some semi-colons, but I'm not sure about the urlString (it isn't used).

    My php file is identical to yours.

    And this is my current Xcode action:

    NSString *host = @"http://www.iminichrispy.com/HSP/HousePoints/Data/writetext.php?myvar=%@&quot;;
    NSString *urlString = [NSString stringWithFormat:host, @"text I want to write to a file"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    Post edited by iminichrispy on
  • appdesignifyappdesignify Posts: 38New Users
    ah sorry, typo there. Try this

    Edit: It keeps messing up my " by replacing it with &quote;

    NSString *host = @"http://www.example.com/writetext.php?myvar=%@&quot;;
    NSString *urlString = [NSString stringWithFormat:host, @"text I want to write to a file"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    Also make sure you add the php tags into your php file, since I forgot to put them in in my original post (I have edited it now to below like it should be)

    <?php
    $var = $_REQUEST['myvar'];
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, "my posted var" .$var ."\n");
    fclose($fh);
    ?>
    Post edited by appdesignify on

    iPhone App Templates

    app templates
    image

  • iminichrispyiminichrispy Posts: 11New Users

    ah sorry, typo there. Try this

    Edit: It keeps messing up my " by replacing it with &quote;


    NSString *host = @"http://www.example.com/writetext.php?myvar=%@&quot;;
    NSString *urlString = [NSString stringWithFormat:host, @"text I want to write to a file"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    Also make sure you add the php tags into your php file, since I forgot to put them in in my original post (I have edited it now to below like it should be)

    <?php
    $var = $_REQUEST['myvar'];
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, "my posted var" .$var ."\n");
    fclose($fh);
    ?>
    Hmmm, I'm still having some difficulty. I've edited the php file to match yours and in the Xcode action, I also changed your first instance of NSMutableRequest to NSMutableURLRequest, so this is what I have:

    Xcode:
    image

    Php File:
    image

    However, nothing happens when I call the xcode action, but when I load the url of where the writetext.php file is located, then it creates the testFile.txt and has the text "my posted var."
    Post edited by iminichrispy on
  • appdesignifyappdesignify Posts: 38New Users
    ok lol, looks like I need sleep. I forgot the most important part!! The part that actually sends the request.

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    The correct code is the following. ALSO keep in mind that if you are going to send data like this you will need to urlencode the string or it might not work. So you will notice that in this final example I left out the spaces in the text. You should find or write a good url encoding function to go along with this and I would take the time to make this do a POST asynchronously as not to block the main thread. I didn't do any of that in this post so that I could make this example as simple as possible.

    Let me know if this works for you

    NSString *host = @"http://www.example.com/writetext.php?myvar=%@&amp;quot;;
    NSString *urlString = [NSString stringWithFormat:host, @"texttowritetofile"];
    NSURL *url = [NSURL URLWithString:host];
    NSMutableRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    iPhone App Templates

    app templates
    image

  • iminichrispyiminichrispy Posts: 11New Users
    ok lol, looks like I need sleep. I forgot the most important part!! The part that actually sends the request.

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    The correct code is the following. ALSO keep in mind that if you are going to send data like this you will need to urlencode the string or it might not work. So you will notice that in this final example I left out the spaces in the text. You should find or write a good url encoding function to go along with this and I would take the time to make this do a POST asynchronously as not to block the main thread. I didn't do any of that in this post so that I could make this example as simple as possible.

    Let me know if this works for you

    NSString *host = @"http://www.example.com/writetext.php?myvar=%@&amp;quot;;
    NSString *urlString = [NSString stringWithFormat:host, @"texttowritetofile"];
    NSURL *url = [NSURL URLWithString:host];
    NSMutableRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    Yes, that works! I'm not worried about the encoding, because the string I will be sending is made up of multiple parts (using %@), and I can simply replace the spaces with addition signs. Just one more thing, it works to post a file, but how can I get it to instead of replacing the text in the file, preserve what's there and simply add a line. I am thinking that I can download the current testfile.txt using the code below, and then when I send the text again to the server, I include the line I downloaded and attach the line I am adding. However, I am having trouble setting this up.

    Downloading file:

    NSURL *url = [NSURL URLWithString:@"http://www.iminichrispy.com/HSP/HousePoints/Data/testFile.txt"];
    currentFile = [NSString stringWithContentsOfURL:url encoding:NSStringEncodingConversionAllowLossy
    error:nil];
    Post edited by iminichrispy on
  • Duncan CDuncan C Posts: 8,034Tutorial Authors, Registered Users
    How you upload depends on the server. You could use an FTP library if the server is running FTP. You could also create an NSURLRequest and set it's HTTPMethod to @"PUT", then submit it using an NSURLConnection. You would then need code on the server to receive and store the data that you sent. You could write a PHP script on the server, for example, to receive the string that you PUT.

    If you hunt around on the internet you should be able to find a tutorial on using an NSURLRequest to do an HTTP put.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • appdesignifyappdesignify Posts: 38New Users
    What you will want to do is change your php script to use fopen command in append mode like follows:

    //notice the 'a' instead of 'w'
    //if you open the file in append mode like below any data you write to the file will always be appended
    $fh = fopen($myFile, 'a') or die("can't open file");

    iPhone App Templates

    app templates
    image

  • iminichrispyiminichrispy Posts: 11New Users

    What you will want to do is change your php script to use fopen command in append mode like follows:


    //notice the 'a' instead of 'w'
    //if you open the file in append mode like below any data you write to the file will always be appended
    $fh = fopen($myFile, 'a') or die("can't open file");
    Awesome, this does exactly what I wanted! Thanks for all your help on this by the way.

  • appdesignifyappdesignify Posts: 38New Users
    Hey no problem, glad I could help :)

    iPhone App Templates

    app templates
    image

  • iminichrispyiminichrispy Posts: 11New Users


    NSString *host = @"http://www.example.com/writetext.php?myvar=%@&amp;quot;;
    NSString *urlString = [NSString stringWithFormat:host, @"texttowritetofile"];
    NSURL *url = [NSURL URLWithString:host];
    NSMutableRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    Just one more thing, how can I get rid of the warning I'm getting (returnData is an unused variable).

  • appdesignifyappdesignify Posts: 38New Users
    Easiest way would be to not return anything, but you should probably check the return response for an error and take appropriate action.

    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    iPhone App Templates

    app templates
    image

Sign In or Register to comment.