如何使用Objective c从远程服务器检索多媒体数据并存储文档目录

时间:2013-02-19 07:03:45

标签: iphone objective-c jsonp nsurlconnection nsdocumentdirectory

大家好,我是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];
    }

}

请帮帮我,我生气了什么错。

1 个答案:

答案 0 :(得分:-1)

对不起,我真的无法很好地阅读你的问题。从我看到的情况来看,您可能会从AFNetworking(https://github.com/AFNetworking/AFNetworking)中受益。它简化了下载过程,并且非常坚固。

在这里查看一个很好的教程:http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_afnetworking/