Tag: afnetworking 2

链接背景NSURLSession上传

有没有人成功链接NSURLSession后台上传? 我正尝试使用NSURLSession的后台上传以5 MB的部分上传一个巨大的video文件。 上传必须按顺序进行。 整个事情在前台工作正常。 我正在使用AFNetwoking这个,它的一个多部分上传。 但是,当应用程序在后台,第一项上传罚款,并在后台启动第二个(在AFURLSessionManager setDidFinishEventsForBackgroundURLSessionBlock)。 但是它突然停止(我最好的猜测是在30秒内,因为在后台唤醒的应用程序的最大寿命为30秒),然后什么也没有发生。 我预计第二届会议将在后台完成,并呼吁第三等 – 连锁行为,但这似乎并没有工作。 我已经试着用一个HTTPMaximumConnectionsPerHost = 1一次性添加所有的文件部分到一个NSURLSession – 这工作正常,并上传完整的文件的部分。 但文件部分是随机select的,即第1部分上传,然后第5部分,第3部分,第10部分等。 我试图在NSOperationQueue中添加这个操作之间的依赖关系,这似乎弄乱了整个事情 – 上传根本不起作用。 我知道video文件可以在后台作为一个单独的file upload,但服务器期望在5 MB的部分。 因此,我想我唯一的select是链上传,或者将所有的部分添加到NSURLSession,但要确保它们总是按照它们添加的顺序上传。 任何帮助,将不胜感激。 我的代码: – (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"%d", rand()]]; AFURLSessionManager *manager = […]

Swift – 为什么init(编码器)在AFHTTPSessionManager中是必需的?

我在iOS开发方面不是很有经验。 AFHTTPSessionManager XCode的子类build议我包含所需的init(coder) : import UIKit let _sharedAPIManager = APIManager(baseURL: NSURL(string: API_URL)!) class APIManager: AFHTTPSessionManager { /** * Singleton service * (https://github.com/hpique/SwiftSingleton) */ class var sharedInstance : APIManager { return _sharedAPIManager } init(baseURL url: NSURL!) { super.init(baseURL: url, sessionConfiguration: nil) self.responseSerializer = AFJSONResponseSerializer() as AFJSONResponseSerializer self.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer self.requestSerializer.setValue(API_KEY, forHTTPHeaderField: "X-Api-Key") self.requestSerializer.setValue("3", forHTTPHeaderField: […]

使用AFNetworking和PHP从照片库上传选定的图像

我正尝试使用AFNetworking上传一张从照片库中select的图像,但我有点困惑。 一些代码示例正在使用图像数据直接上传,一些正在使用文件path。 我想在这里使用AFNetworking示例代码: NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"]; NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (error) { NSLog(@"Error: %@", error); } else { NSLog(@"Success: %@ %@", […]

AFNetworking 2.0:无法使用JSON从IOS进行POST

自从我的IOS代码升级到使用AFNetworking版本2.0而不是1.x后,我无法再使用JSON参数进行HTTP发布。 我怀疑这是因为我的HTTP请求头不是“应用程序/ JSON”,但我试图做到这一点,但仍然无法正常工作。 这是我的testing代码尝试由JSON POST: NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys: email, @"email", password, @"password", nil]; [[OTApiClient sharedInstance] POST:@"login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"result: %@", responseObject); Boolean response = [[responseObject valueForKey:@"success"] boolValue]; if(response == TRUE) { //ok, success NSLog(@"success"); UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Successfully logged in" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; […]

如何在AFnetworking中的AFHTTPRequestOperationManager请求中返回

嗨我发布数据使用AFHTTPRequestOperationManager类获取来自服务器的响应,但无法返回数据。 方法返回是先执行然后成功的数据来了,我想要得到的价值作为回报。 这是我的代码 .h文件 @interface ServerRequest : NSObject { } -(NSString *) JsonData:(NSString *)newparams actionmethod:(NSString *)action parameters:(NSDictionary *)params; .M #import "ServerRequest.h" #import "AFNetworking.h" @implementation ServerRequest { } -(NSDictionary *) getJsonData:(NSString *)anynewparams actionmethod:(NSString *)action parameters:(NSDictionary *)params { NSMutableDictionary *json = [[NSMutableDictionary alloc] init]; NSString *url = @"http://gjkhdhdyi/ghdgd/Rest/"; url = [url stringByAppendingString:action]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; […]

NSURLSession,上传任务 – 获取实际的字节传输

我得到了错误报告,说我的iOS应用程序在缓慢连接上无法上传图像。 虽然我的超时时间可能不够长,但还有一个问题。 我发现上传进度很快达到了100%,尽pipe我可以在Charles看到字节仍在传输。 我使用NSURLSession的以下方法: – (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler 并实现下面的委托方法来接收进度事件: – (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend 其实我使用AFNetworking 2.5.2,它使用这种方法。 我的理论是,这个委托方法报告从手机发送的字节,而不是实际的字节传输。 以非常低的连接发送300kb的数据包,将立即发送5-6个包,并在等待接收时报告100%的进度。 是否有可能获得已确认传输的实际字节数的进度事件?

AFNetworking 2.0中的enqueueHTTPRequestOperation的替代方法

我们需要在我们的应用程序中同时下载文件。 在AFNetworking的早期版本中,我们使用下面的代码同时下载了2个文件: (AFHTTPClient) [_httpClient.operationQueue setMaxConcurrentOperationCount:MAX_CONCURRENT_OPERATIONS]; [self.httpClient enqueueHTTPRequestOperation:downloadObj.downloadOperation]; 现在我们要把我们的AFNetworking升级到2.0。 我们使用的是AFHTTPRequestOperationManager ,而不是AFHTTPClient setMaxConcurrentOperationCount: value 但我们正在手动开始我们的下载过程。 我正在寻找替代(enqueueHTTPRequestOperation 2.0)为此自动下载。

为什么RestKit改变我的响应内容types?

简而言之:我尝试从HTTP请求头的content-type设置为@"text/html ..但是由于某种原因,RestKit将其更改为application/JSON 解释:如果我只是使用AFNetworking来做这个请求,事情就像一个魅力一样。这就是我的AFNetworking代码的样子: AFHTTPClient *client = [AFHTTPClient alloc] initWithBaseURL: [NSURL URLWithString:kApiBaseUrl]]; singleton.parameterEncoding = AFJSONParameterEncoding; [singleton setDefaultHeader:@"Accept" value:@"text/html"]; [client getPath:getPath parameters:nil success:successCallback failure:failureCallback]; 如果我使用完全相同的客户端并附加到 MyClient *client = [MyClient getSingleton]; //MyClient is instantiated as above self.objectManager = [[RKObjectManager alloc] initWithHTTPClient:client]; self.objectManager.managedObjectStore = self.managedObjectStore; // this should have already been done by my client, but putting // it […]

AFNetworking SSL钉住过期的证书

如果在ios应用上使用ssl pinning,如何更新过期的ssl证书? 似乎只有应用程序更新才能更新证书,但不更新应用程序的用户将不会收到此更新。

允许与AFNetworking无效的证书

我一直在使用下面的代码 AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request]; operation.allowsInvalidSSLCertificate=YES; 现在我已经将AFNetworking更改为最新版本2.0。 operation.allowsInvalidSSLCertificate不再适用于AFHTTPRequestOperation 。 根据我使用的文件 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.securityPolicy.allowInvalidCertificates = YES; 和我的请求代码是 AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog (@"success: %@", operation.responseString); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error: %@", error.description); } ]; [operation start]; [operation waitUntilFinished]; 但是这不适用于需要证书的HTTPS。 我该怎么做才能做到这一点?