大家好,我是 iPhone 新手。我正在使用 nsurlconnection 和 json 解析器从远程服务器检索数据。我只从服务器下载了一个文件,并存储在文档路径中。但是在我的服务器 url 中有很多文件,比如(图像、音频、视频、文本文件)。如何在应用午餐时间下载并保存在文档目录中。而且我希望文档中的文件名与服务器中的文件名相同。
我试过这些方法。
ViewController
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSMutableData *responseData;
NSArray *filesCount;
}
@property(nonatomic,retain)NSArray *filesCount;
@property(nonatomic,retain) NSMutableData *responseData;
@end
.m viewController
#import "ViewController.h"
#import "JSON/JSON.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize filesCount,responseData;
- (void)viewDidLoad
{
[super viewDidLoad];
responseData =[[NSMutableData data]retain];
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://XXXXXXX/XXXXX/filesCount.php"]];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"DidFailWithError" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSLog(@"response string is %@",responseString);
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
filesCount = [json objectWithString:responseString error:&error];
[responseString release];
NSLog(@"filesCount is %@",filesCount);
if (filesCount==nil) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Json parsing failed" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alert show];
}
else{
NSMutableString *text = [NSMutableString stringWithString:@"\n"];
for (int i = 0; i < [filesCount count]; i++)
[text appendFormat:@"%@\n", [filesCount objectAtIndex:i]];
NSLog(@"text is %s",[text UTF8String]);
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:text ]]];
NSData *addImageData = UIImagePNGRepresentation(img);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSRange lastComma= [text rangeOfString:@"/" options:NSBackwardsSearch];
NSString *requiredSubString = [text substringFromIndex:(lastComma.location+1)];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:requiredSubString];
[fileManager createFileAtPath:savedImagePath contents:addImageData attributes:nil];
NSLog(@"Saved Document dir %@",savedImagePath);
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"" message:@"files are downloaded" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alert1 show];
}
}
请帮帮我我疯了怎么了。