---
title: "How do we create a temporary file in iOS"  
description: "How do we create a temporary file in iOS"  
author: "Tarun Kumar"  
published: 2015-09-05  
updated: 2015-09-07  
canonical: https://www.mindstick.com/forum/33441/how-do-we-create-a-temporary-file-in-ios  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 1 minute  

---

# How do we create a temporary file in iOS

In C# we could easily create a [temporary file](https://www.mindstick.com/forum/23062/asp-dot-net-temporary-file-saving) and get its [name](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide) using this [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server):

```
Path.GetTempFileName();
```

This function would create a file with a [unique](https://www.mindstick.com/blog/12870/how-to-be-unique-in-business) name in the temporary [directory](https://www.mindstick.com/forum/226/how-to-get-application-directory-using-c-sharp-csharp) and return the [full](https://www.mindstick.com/articles/12893/experience-the-full-power-of-suitecrm) [path](https://www.mindstick.com/articles/44333/4-expert-tips-on-how-to-pick-the-right-career-path) to that file.

In the Cocoa API's, the closest thing we find is: NSTemporaryDirectory

We are [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) something obvious or is there no built in way to do this?

## Replies

### Reply by Anonymous User

I created a pure Cocoa solution by way of a category on NSFileManager that uses a combination of NSTemporary() and a globally unique ID.

Here the header [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp):

```
@interface NSFileManager (TemporaryDirectory)     -(NSString *) createTemporaryDirectory;  @end
```

And the implementation file:

```
@implementation NSFileManager (TemporaryDirectory)-(NSString *) createTemporaryDirectory {    // Create a unique directory in the system temporary directory    NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString];    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:guid];    if (![self createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil]) {            return nil;    }    return path;} @end
```

This creates a [temporary](https://www.mindstick.com/forum/2292/how-to-set-temporary-path-of-jdk-in-windows) directory but could be easily adapted to use createFileAtPath:contents:attributes: instead of createDirectoryAtPath: to create a file instead.


---

Original Source: https://www.mindstick.com/forum/33441/how-do-we-create-a-temporary-file-in-ios

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
