首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS后台使用NSURLSessionUploadTask发布参数?

如何在iOS后台使用NSURLSessionUploadTask发布参数?
EN

Stack Overflow用户
提问于 2014-11-29 17:55:00
回答 1查看 2.3K关注 0票数 0

我想发送一个带有参数的POST消息,使用iOS(比iOS7更晚)后台的NSURLSessionUploadTask,然后在我的.php中使用$_POST‘’parameter‘获取参数。

我知道我必须在后台使用"uploadTaskWithRequest: fromFile:“,但是我如何使用"file”而不是POST参数呢?

我正试着这样做。任何解决方案。谢谢。

代码语言:javascript
复制
- (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);
}

我像下面这样解决了这个问题。

代码语言:javascript
复制
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];
EN

回答 1

Stack Overflow用户

发布于 2015-07-01 06:59:37

这是我的解决方案。

代码语言:javascript
复制
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文件如下所示。

代码语言:javascript
复制
<?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.

?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27200978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档