如何在iPhone模拟器中检查下载的文件?

时间:2014-03-09 23:35:39

标签: ios iphone ios-simulator afnetworking

我正在努力下载文件,它似乎工作,因为我检查了模拟器的文件夹确实我确实下载了文件。这是我的下载方法:

NSString *JSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:params
                                                                                options:NSJSONWritingPrettyPrinted
                                                                                  error:&error]
                                       encoding:NSUTF8StringEncoding];
NSDictionary *data = @{@"data": JSON};

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"download"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFHTTPRequestOperation *op = [manager POST:server
                                parameters:data
                                  success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                      NSLog(@"successful download to %@", path);
                                      [blockView removeFromSuperview];
                                      UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
                                      [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
                                      [self.view addSubview:webView];

                                  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                      NSLog(@"Error: %@", error);
                                      [blockView removeFromSuperview];
                                      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Download failed!"
                                                                                      message:error.localizedDescription
                                                                                     delegate:self
                                                                            cancelButtonTitle:@"OK"
                                                                            otherButtonTitles:nil];
                                      [alert show];
                                  }];
op.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

现在我有两个问题:

  1. 在下载之前所有我知道的都是文档ID,所以我不知道它是pdf还是png或者其他 - 我不应该对它做些什么吗?理想情况下,我会添加适当的扩展名,但在下载完成后就知道了......
  2. 我应该如何在模拟器中测试以检查我是否可以在设备(或那种情况下的模拟器)上读取下载的文件?
  3. 修改 响应总是application/octet-stream

    EDIT2 下面的解决方案 - 但我仍然想知道如何通过邮件检查/显示/发送/使用真实设备上的下载文件?

    TIA

1 个答案:

答案 0 :(得分:0)

所以我下载,保存并在UIWebView成功显示:)我把它放在请求操作的成功块中:

NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[[operation.response.allHeaderFields objectForKey:@"Content-Disposition"] substringFromIndex:22]];
path = [path substringToIndex:path.length-1];
[operation.responseData writeToFile:path atomically:YES];
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
[self.view addSubview:webView];