-2

안녕하십니까? 저는 iPhone을 처음 사용합니다. nsurlconnection 및 json 파서를 사용하여 원격 서버에서 데이터를 가져옵니다. 서버에서 파일 하나만 다운로드하고 문서 경로에 저장했습니다. 하지만 내 서버 URL 수에 파일 (이미지, 오디오, 비디오, 텍스트 파일)이 있습니다. 점심 시간에 앱을 다운로드하고 문서 디렉토리에 저장하는 방법. 그리고 또한 서버에서 파일 이름과 같은 파일 이름을 문서에 넣고 싶습니다.Objective C를 사용하여 원격 서버 및 저장 문서 디렉토리에서 멀티미디어 데이터를 검색하는 방법

나는 이러한 방법을 시도했다.

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

죄송합니다. 질문을 잘 읽을 수 없습니다. AFNetworking (https://github.com/AFNetworking/AFNetworking)을 보면서 도움이 될지 모르지만 내가 본 것. 다운로드 과정을 단순화하고 견고합니다.

좋은 자습서 여기를보세요 : http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_afnetworking/

+0

.Richard @ Brown.The 링크가 좋다하지만 문서 폴더에 이미지를 다운로드해야합니다. 문서의 파일 이름이 서버의 파일 이름과 같아야합니다. 도와주세요. – user2085991