Docs suggest the following but retain is not allowed when using ARC. What is a workaround, please?
// Create the URL for the source audio file. The URLForResource:withExtension: method is
// new in iOS 4.0.
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: #"tap"
withExtension: #"aif"];
// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];
Related
I'm, unsuccessfully, trying to load a PDF on iOS, when debugging the code the document shows "0x0" as value.
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSString *pdfPath = [appFolderPath stringByAppendingString:#"/Data/Raw/test.pdf"];
CFStringRef cfsPath = (__bridge CFStringRef)pdfPath;
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, cfsPath, kCFURLPOSIXPathStyle, 0);
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url);
What could be wrong with this snippet? I don't have much experience with iOS native development and Objective-C.
First of all check if the file exists at the given location in the application bundle.
It's highly recommended to use the URL related API of NSBundle
NSURL *appFolderURL = [[NSBundle mainBundle] resourceURL];
NSURL *pdfURL = [appFolderURL URLByAppendingPathComponent:#"Data/Raw/test.pdf"];
And use the more convenient PDFDocument class of PDFKit rather than the CoreFoundation API
#import PDFKit;
PDFDocument *document = [[PDFDocument alloc] initWithURL:pdfURL];
I want to import data from my iPhone to my application . up till now i have successfully imported Photo Library and music , but i also want to import pdf and other documents in my iOS app? How can i do that?
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSFileManager *mgr = [[NSFileManager alloc] init];
NSArray *allFiles = [mgr contentsOfDirectoryAtPath:bundlePath error:NULL];
for (NSString *fileName in allFiles)
{
if ([[fileName pathExtension] isEqualToString:#"pdf"])
{
NSString *fullFilePath = [bundlePath stringByAppendingPathComponent:fileName];
// fullFilePath now contains the path to your pdf file
// DoSomethingWithFile(fullFilePath);
NSLog(#"file: %#",fullFilePath);
}
}
NSURL *url = [[NSBundle mainBundle] URLForResource:#"" withExtension: #"pdf"];
NSLog(#"File: %#",url);
You can't do it in iOS 8 (I haven't checked 9 though). You can't even enlist your app (adding corresponding URL schemes) in the sharing menu of iBooks.
You have to associate your app with the PDF types which you want to open. You can do this by adding some parameters to your Info.plist.
There's a well-answered post which explains this:
How do I associate file types with an iPhone application?
You should also consider reading Apple's documentation and a blog post written by me.
I have an mp3 file in my Supporting Files folder which I would like to play in my app on a loop. I am using the following code to attempt this:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"smoothjazz" ofType:#"mp3" inDirectory:#"Supporting Files"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //infinite
[player play];
However, this returns an error saying:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
How can I access my file in supporting files, and play it as intended? All help appreciated.
EDIT: SOLVED! See my answer for how I did it :)
If Supporting Files folder is the Supporting Files group you see in every Xcode project by default than the inFolder: argument is not needed as Supporting Files is a Xcode grouping and not a real folder. So you should do:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"smoothjazz"
ofType:#"mp3"];
This should work.
Hope it helps.
May be the path you getting through this will be nil,
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"smoothjazz" ofType:#"mp3" inDirectory:#"Supporting Files"];.
Make sure you properly added the files to your bundle.
And add the proper checks before committing operations, its the best practice. Like follows, Hope this helps you..
// Define NSFileManager and add the proper checks for file existance like as follows.
NSFileManager *filemgr = [NSFileManager defaultManager];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"smoothjazz" ofType:#"mp3" inDirectory:#"Supporting Files"];
if (filemgr fileExistsAtPath:imageURL.absoluteString])
{
// Your code to play the sound file
}
Dan Spag was right - I didn't need to use 'inDirectory:', as it was in the supporting files. But the main problem was that I declared the AVAudioPlayer in the viewDidLoad method, meaning that it's lifespan was only as long as the method (or something along those lines). So I declared the variable as a global variable, and the problem was solved! The music plays.
I'm new to iOS and need to create a NSURL that points to an image that I have copied into the root of my application (i.e. same directory as the AppDelegate & my controller class files reside).
Giving the following code, why does the image not load & how can I get the if condition to validate (i.e. have a NSURL reference to my image)?
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *image2AbsolutPath = [mainBundle pathForResource:#"image2" ofType:#"jpg"];
NSURL *urlToImage = [NSURL URLWithString:image2AbsolutPath];
NSError *error = nil;
if (![urlToImage checkResourceIsReachableAndReturnError:&error]) {
NSLog(#"image2.jpg could not be reached.");
}
Edit: As per the comment; created an imageview and successfully loaded and viewed the image via; self.imageView.image=[UIImage imageNamed:#"image2.jpg"];
Only the NSURL loading seems to not work.
you mixup URLWithString and fileURLWithPath
//wrong
NSString *image2AbsolutPath = [mainBundle pathForResource:#"image2" ofType:#"jpg"];
NSURL *urlToImage = [NSURL URLWithString:image2AbsolutPath];
you use the former but pass it NOT a url string BUT a file path. Change to the latter and it would work
//works
NSString *image2AbsolutPath = [mainBundle pathForResource:#"image2" ofType:#"jpg"];
NSURL *urlToImage = [NSURL fileURLWithPath:image2AbsolutPath];
//but more simply
NSURL *urlToImage = [mainBundle URLForResource:#"image2" withExtension:#"jpg"];
To load the resource you need to use
NSURL *url = [[NSBundle mainBundle] URLForResource: #"image2" withExtension:#"jpg"];
If you look at the two different paths created with pathForResource vs the URLForResource method you will see the NSURL path is missing the required file://
pathForResource:
/Users/MacbookPro/Library/Developer/CoreSimulator/Devices/02D50AC9-5AF0-4F23-8757-1AF7D3153344/data/Containers/Bundle/Application/C5775786-E473-4048-9855-A2C6AA51BB05/MyApp.app/image2.png
vs
URLForResource:
file:///Users/MacbookPro/Library/Developer/CoreSimulator/Devices/02D50AC9-5AF0-4F23-8757-1AF7D3153344/data/Containers/Bundle/Application/C5775786-E473-4048-9855-A2C6AA51BB05/MyApp.app/image2.png
We have a web services function to which we are uploading an audio file into to be saved in the server. The code that I am using to create a data buffer (to pass to the web services) of the contents of the audio file is :
NSURL *soundFileURL = [[NSURL alloc] initFileURLWithPath:fnWithSlash];
audioData = [NSData dataWithContentsOfURL:soundFileURL];
NSLog(#"audiodata%d",audioData.length);
Here fnWithSlash contains the full path of the file. However audioData.length is always 0.
What am i missing?
Thanks in advance for your response.
Try using NSData's dataWithContentsOfFile: instead..If the file is in your bundle it will look like the example below, otherwise construct the filePath of the sound file to your desired location..
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"yourSound" ofType:#"wav"];
audioData = [NSData dataWithContentsOfFile:filePath];
NSLog(#"audiodata%d",audioData.length);