How to send data in my PHP server using URL Xcode

2019-06-14 17:06发布

问题:

I want to send NSString data from Xcode to my PHP server using in this example url: http://11.1.12.173:1000

I need a solution for XCODE 8.1 because some codes are not working in latest Xcode.

NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://11.1.12.173:1000"]];
[req setHTTPMethod:@"POST"];
NSData *postData = [deviceToken dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
[req addValue:postLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:req
                                        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                            // Do something with response data here - convert to JSON, check if error exists, etc....
                                        }];    
[task resume];

i tried this code but its not working.

回答1:

It's a synchronous solution, but it may work for you. Just use it in a different queue and you won't have problems even if the connection is too slow. This is what I do to perform a POST request:

-(NSString*)launchURL:(NSURL*)url withPostString:(NSString*)post
{
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

    NSError *error = nil;
    NSHTTPURLResponse *response = nil;
    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    if (response.statusCode >= 200 && response.statusCode < 300)
    {
        NSString *responseData = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
        if (responseData.length > 0) return responseData;
    }
    else if (error) return [error description];

    return nil;
}

The string need to have the correct encoding (percent encoding) to work, and in order to do that, I add that function to NSString (that method always works, even on very old versions of macOS):

@implementation NSString (WebString)
-(NSString*)stringToWebStructure
{
    NSString* webString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    webString = [webString stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
    webString = [webString stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];

    return webString;
}
@end

You just need to add both to the same file that is going to make the request and use it in that way:

[self launchURL:[NSURL URLWithString:@"http://11.1.12.173:1000"] withPostString:[deviceToken stringToWebStructure]];

Confirmed to work in Xcode 8.