iOS mainBundle not loading resources - objective-c

I had a working iOS project, and whilst working on a git branch, the project would suddenly not load any resources from the main Bundle...I'm loading an audio file for a simple AVAudioPlayer with:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/nadamintrov1.aif", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
NSLog(#"%#", url);
splashScreenAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
and in other ViewControllers I'm also loading other files such as:
patch = [PdBase openFile:#"klangfarbe6ep.pd" path:[[NSBundle mainBundle] resourcePath]];
if (!patch) {
NSLog(#"Failed to open patch!");
// Gracefully handle failure...
}
but whilst both those worked just fine before, now I get "couldn't initialise splash audio player" and for the patch:
open: /var/mobile/Applications/464B65AD-9E2C-42C9-8BBA-12AFD0D48393/Nadam.app/klangfarbe6ep.pd: No such file or directory
klangfarbe6ep.pd: No such file or directory
2012-02-26 12:07:17.332 Nadam[520:707] Failed to open patch!
which is weird...I mean the NSLog(#"%#", url); I put up for the AudioPlayer clearly returns:
2012-02-26 12:06:49.127 Nadam[520:707] file://localhost/var/mobile/Applications/464B65AD-9E2C-42C9-8BBA-12AFD0D48393/Nadam.app/nadamintrov1.aif
so why on earth isn't it loading it? Diff from git over the last few changes don't show anything in the project file that I can see as corruption...
anyone had that happen to them before? any way of debugging it? I read this thread:
What is the alternative of [NSBundle mainBundle] URLForResource:withExtension: in iOS 3 SDK
But can't understand why it was working before and not now with no project changes...

I don't know exactly what is the issue here. But it is the most common problem. In two situations such issue occurs. 1. file path is wrong. 2.File is not included in bundle.
After checking above file path shown by you I don't think it is the issue of file path. But you can check your file is included in target or not. Just right click on your file and select get info. Where check in target your current target is selected or not.

Related

NSString pathway always returns nil despite having the file contained in the folder

So i'm trying to create a video screen saver for macOS 10.12 by using NSString to obtain the pathway, and NSURL to store the pathway.
However every time I test the screen saver, it crashes with this error:
Application Specific Information:
com.apple.preference.desktopscreeneffect v.5.1 (Desktop & Screen Saver)
Crashing on exception: *** -[NSURL initFileURLWithPath:]: nil string parameter
I believe the problem lies here:
// Create the video path then use it. (Problem at the moment: the pathway always returns nil)
NSString *screenSaverPath = [[NSBundle mainBundle] pathForResource:#"apple" ofType:#"mp4"];
NSURL *screenSaver = [NSURL fileURLWithPath:screenSaverPath];
//Obtain the size of the screen.
NSSize size = [self bounds].size;
//Draw the video player.
AVPlayer *video = [AVPlayer playerWithURL:screenSaver];
But I am unsure as to how I can tackle this problem. Does anyone know any way as to how you can resolve this problem? Thank you!
Screen savers are loaded into an application. So mainBundle is the ScreenSaverEngine process that loaded your plugin, not your plugin. You should probably use bundleForClass to get the bundle your screensaver's main class is in.

iOS Webview loadRequest from string returns nil on ipad and ipad retina

I've a menu on my app which loads local html file but it detects first if there is an updated file inside documents directory and if the file exists, the value of the html_path will load the updated file with its path on docs.dir of the app. This code works fine with iphone, and ipad retina 64 bit, but on ipad and ipad retina, the html_path variable returns nil.
NSString *html_path;
html_path = [[NSBundle mainBundle]
pathForResource:#"somehtml"
ofType:#"html" inDirectory:#"www"];
local_request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:html_path]];
[_webView loadRequest:local_request];
Below is the error that I got:
Terminating app due to uncaught exception `NSInvalidArgumentException`,
reason: `*** -[NSURL initFileURLWithPath:]: nil string parameter`
I wonder the cause of this error, please help as I'm stuck with this error.
I suspect this could be a problem with your resources not being included in the bundle for the application.
Select your target, go to "Build Phases" > "Copy Bundle Resources", press the "+" button and add your "somehtml.html" to the bundle.
I have solved the issue, it seems like
html_path = [[NSBundle mainBundle]
pathForResource:#"somehtml"
ofType:#"html" inDirectory:#"www"];
that code above does not return the correct path, or the correct string path to be recognized by the NSURL fileURLWithPath.
the solution that i came up with was, i separated the bundle path and put that on the string
NSString *bundle = [[NSBundle mainBundle] bundlePath];
then i use stringByAppendingPathComponent to concatenate the html file name
htmlFile = [bundle stringByAppendingPathComponent:#"www/somehtml.html"];

Program directory in Objective-C (OSX)

I'm developing an OSX-application and in it, I'd like to know what the current directory is (i.e. the directory which holds .app-file).
At the moment, I'm using the following code:
NSString *dir=[[NSFileManager defaultManager] currentDirectoryPath];
[[NSAlert alertWithMessageText:#"dir"
defaultButton:#"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:dir] runModal];
When running from Xcode (Run-button), this gives me the debug directory (which is what I'm looking for), but when double-clicking the app in Finder (so, in the debug directory), it's giving me / which puzzles me.
Why does this happen and how can I get the current directory reliably?
That is the bundle folder:
NSString *appPath = [[NSBundle mainBundle] bundlePath];
(reference).
When programming in Apple Swift you will get the application path with:
let pathtoapplication: String = NSBundle.mainBundle().bundlePath

howto resolve UIDocumentStateSavingError and UIDocumentStateClosed

I have an iPhone-iCloud app. Now one document has the state UIDocumentStateSavingError and UIDocumentStateClosed. I can see the file on developers.icloud.com with the status "file upload is pending". But i don't know what to do now
When i try to delete the file with the code:
NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fileCoordinator coordinateWritingItemAtURL:fileURL options:NSFileCoordinatorWritingForDeleting error:nil byAccessor:^(NSURL* writingURL) {
NSFileManager* fileManager = [[NSFileManager alloc] init];
[fileManager removeItemAtURL:writingURL error:nil];
}];
the file is still there. I tried to delete the app on all devices but nothing changed. How can I delete this file or resolve the problem?
It's long ago but still unanswered, so if your problem persists:
For me it works to prepend the [fileManager removeItemAtURL:...] with a [fileManager setUbiquitous:NO...] on the file and then let the fileManager delete the local copy.
Take care to use NSError and boolean result handling of the fileManager actions to be sure it really works. So if you get back an NSError you could see the reason why it failed. That's always good practice.
I stumbled into your question because I get the message "File upload is Pending" after such a deleted file is re-created even if it's some hours between deletion and creation.

NSFileManager works when built in Xcode as release, but not when ran as standalone OS X Cocoa app

I have the following function written to randomly pick a file from a directory. It works totally fine when I build the project in Xcode for release with the application that automatically opens. However, if I open the application from finder, pressing the button that triggers this function will cause my program to freeze then crash. The only thing I could think of was changing the argument to contentsOfDirectoryAtPath: to not have the ./, but either version has the same exact issue.
Looking at Console tells me that my program exited abnormally with a Floating Point Exception, but I have no idea what's causing it. Is there something jumping out to you guys that I'm not seeing? I only started learning/using objective-C and cocoa about a week ago, so this is all fairly new to me.
Thanks for taking a look at this...
- (NSMutableString*)setFilePathRandom{
NSArray* files;
NSFileManager* fileManager;
fileManager = [[NSFileManager alloc] init];
files = [fileManager contentsOfDirectoryAtPath:#"./Random Trippy Pics" error:NULL];
NSString* directoryPath = (NSMutableString*)[fileManager currentDirectoryPath];
NSString* fileName;
do{
fileName = [files objectAtIndex:(arc4random()%[files count])];
}while([fileName isEqualToString:#".DS_Store"]);
filePath = [NSString stringWithFormat:#"%#/Random Trippy Pics/%#",directoryPath,fileName];
[fileManager release];
return filePath;
}
When an OS X application is run from Xcode, its current directory is the path to the build folder. When run "normally", the current directory is /. So your program is looking for a directory at /Random Trippy Pics, which almost certainly doesn't exist. Where is that directory normally?
Edit:
You could get the directory in which the application is currently stored with this bit of code:
NSString *currentStoragePath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
However, if the Random Trippy Pics directory is required by the application, you should store it in a known location -- preferably the application's Resource directory. Then you can get the contents with:
NSArray *files = [[NSBundle mainBundle] pathsForResourceOfType:nil inDirectory:#"Random Trippy Pics"];