LoginSignup
1
0

More than 1 year has passed since last update.

macOSでUSBメモリに書き込むプログラムを作る

Last updated at Posted at 2021-07-17

はじめに

普通にやると書き込めなかったのでメモ。
今回はSwiftUIで作ったが、多分他でもいける。

entitlements の設定

.entitlements の項目ができているはず。これの変更が必要。
まず、App Sandbox をNOにする。YESだと書き込み先がSandboxになるらしい。
次に com.apple.security.files.user-selected.read-write を追加、TypeはBoolean, Valueは 1
上に com.apple.security.files.user-selected.read-only があるので、こちらは一応 0 にしておく。

実際の書き込み

ここまで来たら普通に書ける。

let fileman = FileManager.default
fileman.createFile(atPath: <USBメモリなどのパスを含めたファイル名>, contents: nil, attribytes: nil)
let file = FileHandle(forWritingAtPath: <上で作ったファイル名>)
if (nil == file) {
    // エラー処理
}
else {
    file?.write(<書き込みデータ>)
    file?.closeFile()
}

動かすと

初回実行時にはリムーバブルメディアの許可を求める表示があって、許可すると書き込みできる。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0