0

iOS(iOS7以降)のバックグラウンドからNSURLSessionUploadTaskを使用してパラメーター付きのPOSTメッセージを送信し、.phpで$ _POST ['parameter']を使用してパラメーターを取り出したいと思います。

バックグラウンドから「uploadTaskWithRequest: fromFile:」を使用する必要があることはわかっていましたが、POST パラメーターの代わりに「file」を使用するにはどうすればよいですか?

私はこのように努力しています。任意のソリューション。ありがとう。

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
NSURL *url = [NSURL URLWithString:@"http://some_server/some.php"];
NSString *identifier = @"identifier";
NSURLSessionConfiguration *configration = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSURLSession *session = [NSURLSession sessionWithConfiguration:configration];

NSString *parameters = @"parameter1=value1&parameter2=value2";
[request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];

NSString *sampleText = @"test";
NSString *filePath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"test.txt"];
NSError *error;
[sampleText writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

filePath = [NSString stringWithFormat:@"file://%@", filePath];
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePath]];

[task resume];

completionHandler(UIBackgroundFetchResultNoData);
}

以下のようにこの問題を解決しました。

NSURL *url = [NSURL URLWithString:@"http://..."];
NSURLSessionConfiguration *configuration;
configuration.timeoutIntervalForRequest = 5;
configuration.timeoutIntervalForResource = 5;
configuration.HTTPMaximumConnectionsPerHost = 1;
configuration.allowsCellularAccess = YES;
configuration.networkServiceType = NSURLNetworkServiceTypeBackground;
configuration.discretionary = NO;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
                                                              delegate:self
                                                         delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"PUT";

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                    object, @"key", nil];

NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];

NSString *path = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"json.data"];
[data writeToFile:path atomically:YES];

path = [NSString stringWithFormat:@"file://%@", path];
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:[NSURL URLWithString:path]];

[task resume];
4

1 に答える 1

0

これが私の解決策です。

NSURL *url = [NSURL URLWithString:@"http://..."];
NSURLSessionConfiguration *configuration;
configuration.timeoutIntervalForRequest = 5;
configuration.timeoutIntervalForResource = 5;
configuration.HTTPMaximumConnectionsPerHost = 1;
configuration.allowsCellularAccess = YES;
configuration.networkServiceType = NSURLNetworkServiceTypeBackground;
configuration.discretionary = NO;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
                                                          delegate:self
                                                     delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"PUT";

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                object, @"key", nil];

NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];

NSString *path = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"json.data"];
[data writeToFile:path atomically:YES];

path = [NSString stringWithFormat:@"file://%@", path];
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:[NSURL URLWithString:path]];

[task resume];

サーバー内の .php ファイルは以下のようなものです。

<?php

$put = fopen("php://input", "r");
if (!$put)
{   
    fclose($put);   
    exit();
}

$lines = "";
while ($line = fgets($put))
{   
    $lines = $lines . $line;
}   
$lines = str_replace(array("\r", "\n"), '', $lines);
$json = json_decode($lines,  true);
fclose($put);

//Do something with $json.

?>
于 2015-06-30T22:59:36.737 に答える