Closed
Description
The file paths for iOS writable locations is a bit rough and hard to figure out. Like, the pwd
of an iOS (simulator) app when running is /
. The Data Container in for an App Sandbox is kinda hard to find.
A few weeks ago a friend asked me how to do this and I came up with:
use std::fs::File;
use std::io::Write;
use objc2_foundation::NSFileManager;
use std::path::PathBuf;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let ns_file_manager = unsafe { NSFileManager::defaultManager() };
let temp_dir = unsafe { ns_file_manager.temporaryDirectory() };
let path = unsafe { temp_dir.relativePath() }.expect("Failed to get path from temp dir");
let path = path.to_string();
let path = PathBuf::from(path.clone()).join("results.txt");
println!("WRITING TO PATH: {path:?}");
let mut output = File::create(path)?;
let line = "hello";
let _ = write!(output, "{}", line);
Ok(())
}
The objc2_foundation
dependency in the Cargo.toml is:
[dependencies]
objc2-foundation = {version = "0.2.0", features = ["NSFileManager", "NSURL", "NSString"] }
This might also be useful for other apple device targets.
This would be helpful for:
- Make tempfile be a macos-only dependency sfackler/rust-native-tls#295
- An attempt at supporting criterion benchmarks sonos/dinghy#229
When I get the time, I'm happy to submit a PR for this feature (and help maintain it) though let me know if such a feature is out of the scope of this project.
Metadata
Metadata
Assignees
Labels
No labels