woshidan's loose leaf

ぼんやり勉強しています

iOSでBundleファイルを扱う

  1. .bundle 拡張子のついたフォルダーを用意
  2. その下にファイルを置く
  3. .bundle 拡張子のついたファイルをXCode上で追加したいプロジェクトに追加
  4. 下記のように NSStringNSData, UIImage などの関数でデータを読み込む
NSString* filePath = [NSString stringWithFormat:@"%@/%@",  [[NSBundle mainBundle] pathForResource:@"test" ofType:@"bundle"], FILE_NAME];
NSString* content = [NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:nil];
NSLog(@"content: %@", content);

NSData *contentData = [NSData dataWithContentsOfFile:filePath
                                              options:NSDataReadingMappedIfSafe
                                                error:nil];

[UIImage imageNamed:filePath];

参考