[swift] Swift에서 특정 디렉토리 삭제 전 사용자 확인하기

어떤 경우든 사용자에게 삭제할 디렉토리에 대한 확인 메시지를 표시하여 실수로 데이터를 손실하지 않도록 할 수 있습니다.

import Foundation

func deleteDirectory(atPath path: String) {
    let fileManager = FileManager.default
    
    // 사용자 확인 메시지 표시
    let confirmAlert = UIAlertController(title: "경고", message: "디렉토리를 삭제하시겠습니까?", preferredStyle: .alert)
    confirmAlert.addAction(UIAlertAction(title: "확인", style: .destructive, handler: { action in
        do {
            try fileManager.removeItem(atPath: path)
            print("디렉토리 삭제 성공")
        } catch {
            print("디렉토리 삭제 실패: \(error.localizedDescription)")
        }
    }))
    confirmAlert.addAction(UIAlertAction(title: "취소", style: .cancel, handler: nil))
    
    // 확인 메시지 표시
    if let viewController = UIApplication.shared.keyWindow?.rootViewController {
        viewController.present(confirmAlert, animated: true, completion: nil)
    }
}

// 사용 예시
let directoryPath = "/path/to/directory"
deleteDirectory(atPath: directoryPath)

위의 예시에서는 UIAlertController를 사용하여 사용자에게 삭제를 확인하는 확인 대화 상자를 표시하는 방법을 보여줍니다.

물론, 실제 프로덕션 코드에서는 사용자 경험을 고려해야 하며, 이 예제는 해당 목적으로만 사용되어야 합니다.

관련 문서: Apple Developer Documentation - UIAlertController