본문 바로가기

개발도구/iOS - 아이폰 개발

[아이폰] Document 에 파일 저장과 호출

// 파일 :  pokeb-asi-http-request-v1.8.1-78-gf99ca46 (2).zip
 
// 참고 사이트 : http://allseeing-i.com/ASIHTTPRequest/ 
// 사용 블로그 : http://blog.naver.com/revolution59?Redirect=Log&logNo=140130960809 
// http://adolchristin.tistory.com/40

NSURL *url = [NSURL URLWithString:@"http://www.robtowns.com/music/blind_willie.mp3"]; //url stream

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; // ASIHTTPRequest framework 사용 //[iPhone] 파일 시스템 (Document Directory 경로찾기)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];        
NSString *downloadPath = [documentsDirectory stringByAppendingPathComponent:@"1.mp3"];//// 위에 스트림의 주소로 다운 받은 파일을 새로운 이름으로 변경 저장


//downloadPath 콘솔에서 확인 위치에서 파일을 확인하세요~~
// The full file will be moved here if and when the request completes successfully
NSLog(@"%@",downloadPath); 
[request setDownloadDestinationPath:downloadPath];

/
/파일다운을 위한 재시도 설정 YES
 
[request setAllowResumeForFileDownloads:YES];

//재시도 횟수
[request setNumberOfTimesToRetryOnTimeout:15]; 

//타임아웃 설정시간
[request setTimeOutSeconds:20.0];

// This file has part of the download in it already
// 위에 저장된 파일의 (로딩)불러오기
NSString *downloadTempPath = [documentsDirectory stringByAppendingPathComponent:@"1.mp3"];
NSLog(@"%@",downloadTempPath);
[request setTemporaryFileDownloadPath:downloadTempPath];

//파일 읽기 쓰기 매니지저 호출 
NSFileManager *fileManager = [NSFileManager defaultManager];

// 파일의 존재유무
if ([fileManager fileExistsAtPath:downloadTempPath]) {

NSLog(@"존재");
}

//원하는 확장자만 필터링하기 
NSArray *list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSArray *filteredlist = [list pathsMatchingExtensions:[NSArray arrayWithObjects:@"mp3", nil]];  
NSLog(@"%@",filteredlist);

[request startAsynchronous];



================참고 소스===================

파일을 url 다운받아서 다운받은파일을 nsbundle 이용하여 열고싶습니다.


파일 다운까지는 잘되는것 같은데 막상 실행만하면 nsbundle부분에서 오류가 생기는데요


webview 다운받은 파일을 보여주기위해서 필요한것들


고수님들의 많은 지도 부탁드립니다.


중요 소스는 다음과 같습니다.

- (void)viewDidLoad {

    NSString* Down_URL =@"http://saveyo.tistory.com/attachment/cfile23.uf@194C834A4EACF6D52CE9D2.pdf";


    NSURLRequest* theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:Down_URL]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];


}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    if(connection != theConnection) return;


/

NSFileManager* FM = [NSFileManager defaultManager];


//[iPhone] 파일 시스템 (Document Directory 경로찾기)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];        


    

    

    NSString *downloadPath = [documentsDirectory stringByAppendingPathComponent:@"FileName.pdf"];

//NSString *downloadPath=[NSString stringWithFormat:@"%@.%@",@"FileName",@"pdf"];


if([FM createFileAtPath:downloadPath contents:DownLoad_Data attributes:nil])

{

NSLog(@"데이터저장성공");

}

    

    NSURL *pdfURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"FileName"ofType:@"pdf"]];

    [detailViewController.webDtl loadRequest:[NSURLRequest requestWithURL:pdfURL]];

//데이터삭제

if(DownLoad_Data) 

{

NSLog(@"Release");

[DownLoad_Data release];

}

}