Does an iOS app have write access inside its bundle?
The bundle is read-only. You don't want to mess around with it for two reasons:
- Code Signing: the signature is verified by against the contents of the bundle; if you mess around with the bundle, you break the signature.
- App Updates: updates work by replacing the entire app bundle with a newly downloaded one; any changes you make will get lost.
Where you should save stuff:
- Documents: if you want it to persist and be backed up
- Library/Caches: if you just want to cache downloaded data, like profile pics; will be auto deleted by the system if it is low on room unless you specify with a special do-not-delete flag.
- tmp: temporary files, deleted when your app is not running
For a full explanation check out File System Programming Guide and QA1719.
How to access file included in app bundle in Swift?
Simply by searching in the app bundle for the resource
var filePath = NSBundle.mainBundle().URLForResource("file", withExtension: "txt")
However you can't write to it because it is in the app resources directory and you have to create it in the document directory to write to it
var documentsDirectory: NSURL?
var fileURL: NSURL?
documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last!
fileURL = documentsDirectory!.URLByAppendingPathComponent("file.txt")
if (fileURL!.checkResourceIsReachableAndReturnError(nil)) {
print("file exist")
}else{
print("file doesnt exist")
NSData().writeToURL(fileURL!,atomically:true)
}
now you can access it from fileURL
EDIT - 28 August 2018
This is how to do it in Swift 4.2
var filePath = Bundle.main.url(forResource: "file", withExtension: "txt")
To create it in the document directory
if let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last {
let fileURL = documentsDirectory.appendingPathComponent("file.txt")
do {
if try fileURL.checkResourceIsReachable() {
print("file exist")
} else {
print("file doesnt exist")
do {
try Data().write(to: fileURL)
} catch {
print("an error happened while creating the file")
}
}
} catch {
print("an error happened while checking for the file")
}
}
How To write into text File at my application's bundle in xcode
The bundle is read-only. You don't want to mess around with it for two reasons:
Code Signing: the signature is verified by against the contents of the bundle; if you mess around with the bundle, you break the signature.
App Updates: updates work by replacing the entire app bundle with a newly downloaded one; any changes you make will get lost.
Where you should save stuff:
Documents: if you want it to persist and be backed up
Library/Caches: if you just want to cache downloaded data, like profile pics; will be auto deleted by the system if it is low on room unless you specify with a special do-not-delete flag.
tmp: temporary files, deleted when your app is not running
For a full explanation check out File System Programming Guide and QA1719.
From following SO Answer:
Write a File into the App Bundle
Ok, based on your comment, here's what you need (I think - still not completely clear on the question, but let me know if this works for you).
- Create an empty file (say news.plist) in your project folder (SRCROOT). Add this to your repo.
- Add this file in your project's resources and add it in
Copy Bundle Resources
step inBuild Phases
- When you download content from internet, save it to project's temporary folder, then copy it to SRCROOT/news.plist overwriting existing file (optionally add a check on file checksum to avoid unnecessary change). This step is required ONLY when running on simulator.
- Whenever developers check-in, the updated news.plist should be checked in to the repo.
To make SRCROOT available in code, add SRCROOT=\"${SRCROOT}\"
to GCC_PREPROCESSOR_DEFINITIONS.
Other developer won't need to do anything except the last step - check in the updated file every time it changes.
Does this help?
How do you access a file from inside an app directory
You might be looking for NSBundle.mainBundle
which returns a reference to the NSBundle
for the application. Using this and the various instance methods provided by NSBundle
you may then determine the paths/URLs of the files packaged within the application bundle.
Related Topics
Get Size of a View in React Native
Presenting a Uialertcontroller Properly on an iPad Using iOS 8
Uilabel Layer Cornerradius Negatively Impacting Performance
How to Draw a Line Programmatically from a View Controller
Xcode Stuck at "Your Application Is Being Uploaded"
iOS 8 - Can't Install Enterprise App
Difference Between Protocol and Delegates
How to Check If a View Controller Is Presented Modally or Pushed on a Navigation Stack
Chain Multiple Alamofire Requests
Module Compiled with Swift 5.1 Cannot Be Imported by the Swift 5.1.2 Compiler
Allow Only Alphanumeric Characters for a Uitextfield
Record and Play Audio Simultaneously
How to Run an Action When a State Changes
Wkwebview Does Load Resources from Local Document Folder
Positioning Uitabbar at the Top
iOS Universal Device App with Spritekit, How to Scale Nodes for All Views
Ios: Serialize/Deserialize Complex JSON Generically from Nsobject Class