본문 바로가기

Swift

swift에서 클라우드로 파일 관리 핵심 - URLForUbiquityContainerIdentifier

클라우드에 파일을 저장하거나 저장한 파일을 다운로드 하는 경우는 뭐 다른 특별한 메소드가 따로 있는 것은 아니다.

그냥 일반적인 로컬 작업과 동일한 메소드를 사용하면 된다. 바로 이거!

try fileManager.copyItemAtURL(itemUrl, toURL: localURL!)

그럼 뭣이 중한가? 중요한 것은 물론 클라우드의 디렉토리 정보이다.

그래서 이런 메소드를 사용하기 전에 작업할 클라우드 디렉토리 정보를 사전에 알아놔야 하는데 그것은 아래 메소드를 이용하여 기본 디렉토리 정보를 알아낼 수 있다.


// 클라우드 기본 디렉토리 정보 가져오기

let containerURL = fileManager.URLForUbiquityContainerIdentifier(nil)

// 클라우드 Documents 디렉토리 URL

cloudDocumentsDirectory = containerURL?.URLByAppendingPathComponent("Documents")

// 클라우드 Documents 디렉토리의 default.realm 파일 URL

let destinationUrl = cloudDocumentsDirectory.URLByAppendingPathComponent("default.realm")


이 메소드만 이용하면 클라우드에서 기본적인 파일 관리가 가능하다.