0

I've tried most of advises from stackoverflow but none of them was good for me.

My client wants to save user data in json with GET request in format:

http://website.com/users/save.php?save={"email":"user@domen.com","name":"Will Smith"}

I'm using objective-c Please advise Thanks

Edit

my php file looks like this:

<?php
$save = $_GET['save'];
$fp = fopen("save_users_ww.txt", "a+");
fputs($fp,$save."\r\n");
fclose($fp);
?>

so post don't work for me. Thank you for help again

Victor
  • 51
  • 1
  • 6
  • Vicor as a beginner, please explain your question as much as possible. I think you asking to post data from app to server. If Yes,then please create a post request & send the data on the server. Look into the following link https://www.raywenderlich.com/67081/cookbook-using-nsurlsession http://stackoverflow.com/questions/19099448/send-post-request-using-nsurlsession http://stackoverflow.com/questions/34327919/post-request-with-raw-body-using-nsurlsession https://developer.apple.com/reference/foundation/urlsession – Gagan_iOS Nov 19 '16 at 20:47
  • Your question is vague to the point of being indecypherable. Are you saying that you want to download data from a server with a GET request and then save that data locally? Or are you saying that you have data that you want to send to a server? Your URL looks like you're trying to put the JSON right in the URL. You can't do that. You can use query strings to set a small number of key value pairs, but you can't deliver a JSON payload that way. If you're sending data to a server you want to use a POST, not a GET. – Duncan C Nov 19 '16 at 23:49
  • Dear Duncan C, yes I'm trying to upload json data to server, actually to txt file. I'm confused too. I added php file, it works great if I just open link in web browser, but how should it be done in objectve-c? Thanks – Victor Nov 20 '16 at 19:31

1 Answers1

0

In case someone else will need a solution, this is my code:

.h file:

@interface WelcomeViewController : UIViewController <NSURLConnectionDelegate>
{
NSMutableData *_responseData;
}

.m file:

-(void)postToDataBaseWithName:(NSString*)nameToPost andEmail:(NSString*)emailToPost  {
    NSString *str = [NSString stringWithFormat:@"http://website.com/users/save.php?save={\"email\":\"%@\",\"name\":\"%@\"}", emailToPost, nameToPost];
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // Create url connection and fire request
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if(conn) {

    }
}

also add delegate methods to see how it works:

#pragma mark NSURLConnection Delegate Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    // A response has been received, this is where we initialize the instance var you created
    // so that we can append data to it in the didReceiveData method
    // Furthermore, this method is called each time there is a redirect so reinitializing it
    // also serves to clear it

    NSLog(@"didReceiveResponse");
    _responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Append the new data to the instance variable you declared
    NSLog(@"didReceiveData");
    [_responseData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
              willCacheResponse:(NSCachedURLResponse*)cachedResponse {
    // Return nil to indicate not necessary to store a cached response for this connection
    return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // The request is complete and data has been received
    // You can parse the stuff in your instance variable now
    NSLog(@"connectionDidFinishLoading");

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // The request has failed for some reason!
    // Check the error var
    NSLog(@"didFailWithError = %@", error);
}
Victor
  • 51
  • 1
  • 6