3

I want to upload voice recording on server.

My file url is:

file:///Users/xantatech/Library/Developer/CoreSimulator/Devices/77F4D768-1F04-4390-B60F-F1FE79388653/data/Containers/Data/Application/87C457FA-1A9F-4CA9-A651-6A3D411A0B7E/Documents/myAudio0.mp3

My Code is:

NSData* data = [NSData dataWithContentsOfURL:audioURL options:NSDataReadingUncached error:&error];

But the data is nil.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Hari Mohan
  • 214
  • 1
  • 4
  • 16

6 Answers6

1

Please try below code

NSString *audioURLString = @"file:///Users/xantatech/Library/Developer/CoreSimulator/Devices/77F4D768-1F04-4390-B60F-F1FE79388653/data/Containers/Data/Application/87C457FA-1A9F-4CA9-A651-6A3D411A0B7E/Documents/myAudio0.mp3";
NSString *sendStr = [[audioURLString absoluteString] stringByReplacingOccurrencesOfString:@"file:///private" withString:@""];
NSData *data = [[[NSData dataWithContentsOfFile:sendStr]]];

Good luck.....

Maddy
  • 311
  • 2
  • 11
0

Use this code for get data from Document folder:

if([[NSFileManager defaultManager] fileExistsAtPath:filepath)
{
   NSData *data = [[NSFileManager defaultManager] contentsAtPath:filepath];
}
else
{
   NSLog(@"File not exits");
}
Adnan T.
  • 1,166
  • 1
  • 8
  • 8
0

You just need to do:

NSURL *imgPath = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"png"];
NSString *path = [imgPath absoluteString];  
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:path];
User511
  • 1,456
  • 10
  • 28
0

Try this

NSData* data =[[NSData alloc]init];
data = [NSData dataWithContentsOfFile:[NSURL URLWithString:path]];
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

After run:
file:///Users/xantatech/Library/Developer/CoreSimulator/Devices/77F4D768-1F04-4390-B60F-F1FE79388653/data/Containers/Data/Application/"87C457FA-1A9F-4CA9-A651-6A3D411A0B7E"/Documents/myAudio0.mp3
The Bold text "87C457FA-1A9F-4CA9-A651-6A3D411A0B7E" may change;
You should always use the method to get the path in sandBox:

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"myAudio0.mp3"];

nvNovaDev
  • 41
  • 4
0

You can use this:

NSData *data = [NSData dataWithContentsOfURL:yourURL];
Nikodem
  • 89
  • 1
  • 8