2011-08-16 2 views
49

NSTemporaryDirectory 폴더에 "temp.pdf"라는 파일의 파일 경로를 가져 오려고합니다 (파일을 확인했습니다).임시 디렉토리에있는 파일의 파일 경로 및 URL 얻기

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp.pdf" ofType:@"" inDirectory:NSTemporaryDirectory()]; 

: 그리고 내가 함께 해봤

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"pdf" inDirectory:NSTemporaryDirectory()]; 

:

나는이 사용

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"pdf"]; 

그러나 작동하지 않는 것 같다, 반환 값은 항상 null입니다.

그래서 약간 혼란스럽고 누군가 나를 도울 수 있습니까?

감사합니다.

답변

120

NSTemporaryDirectory()은 임시 디렉토리의 전체 경로를 제공합니다.

NSURL *furl = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.pdf"]]; 
+0

좋아, 그렇게 할 때 [UIDocumentInteractionController interactControllerWithURL : filepath]와 함께 파일 경로를 사용하고 싶습니다. [NSURL urlWithString : filepath]를 만들었지 만 Xcode에서는 "잘못된 스키마 (null)입니다. 파일 스키마 만 지원됩니다." – HasgardLucian

+19

'NSURL * furl = [NSURL fileURLWithPath : NSTemporaryDirectory() stringByAppendingPathComponent : @ "temp.pdf"]] ' – Joe

+0

임시 디렉토리에 모든 pdf 파일을 반환하려면 어떻게해야합니까 ?? –

13

스위프트 2.0

let tmpDirURL = NSURL.fileURLWithPath(NSTemporaryDirectory(),isDirectory: true) 
let fileURL = tmpDirURL.URLByAppendingPathComponent("stuff").URLByAppendingPathExtension("gif") 
print("FilePath: \(fileURL.path)") 
0

스위프트 3.0 대신 사용하는 당신의 mainBundle 당신은 당신이 대신 URL을해야하는 경우,이 작업을 수행

NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"temp.pdf"]; 

을 시도

var fileUrl: URL = URL(fileURLWithPath: NSTemporaryDirectory()) 
fileUrl.appendPathComponent("foo") 
fileUrl.appendPathExtension("bar")