Error building from 5 to 6 IOS - objective-c

I have IPad application for IOS 5.1, now i'm trying to build it for 6.0, and get an error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
-[NSFileManager copyItemAtPath:toPath:error:]: source path is nil'
"
i found where is this problem, but i don't know how to correct it
in my AppDelegate
-(void)copyFileWithName:(NSString*)name Extension:(NSString*)extension ToDir:(NSString*)dirName withName:(NSString*)newName
{
NSString *folderPath = dirName;
NSString *filePathDocTxt = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.%#", newName, extension]];
NSString *filePathBundleTxt = [[NSBundle mainBundle] pathForResource:name ofType:extension];
//when i comment these two lines, my code compiles success, but i need these code
if(![[NSFileManager defaultManager] fileExistsAtPath:filePathDocTxt])
[[NSFileManager defaultManager] copyItemAtPath:filePathBundleTxt toPath:filePathDocTxt error:nil];
}

well the error is source path for the copyItemAtPath method is nil.
Since you copy from the bundle (your app) to another folder that implies that the file is not in the target.
make sure the file you attempt to copy is there and correctly added to the target (in Xcode)
to resolve the crash add an if to exclude copying if the item isn't there / assert it
NSString *filePathBundleTxt = [[NSBundle mainBundle] pathForResource:name ofType:extension];
assert(filePathBundleTxT);

Related

Objective C, Copy resource file from main bundle to documents: Error Domain=NSCocoaErrorDomain Code=4 The file doesn’t exist."

Here I want to achieve a function which can copy a res file from main bundle (which is added manually from Mac Finder to Xcode project ) (Fig 1) to Document Folder.
Click here to see the Xcode project structure
And to achieve that goal, I use NSFileManager. Here is the function code:
- (void)addCamConfig {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *destPath = [documentsDirectory stringByAppendingPathComponent:CAM_CONFIG];
if (![fileManager fileExistsAtPath:destPath]) {
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:#"cam" ofType:#"yaml"];
BOOL ret = [fileManager copyItemAtPath:sourcePath toPath:destPath error:&error];
if (error) {
NSLog(#"cam_ret:%d\nsourcePath:%#\ndestPath:%#\nerror:%#",ret,sourcePath,destPath,error.description);
}
}
}
In that case, sourcePath and destPath are both ensured not to be nil.
But strange thing happened in the first time, "copyItemAtPath:sourcePath toPath:destPath error:&error" return "NO" and error log showed below:
cam_ret:0
sourcePath:/private/var/containers/Bundle/Application/3DF49723-E11D-4D67-AD0F-39C2B82B80A4/NitroDemo.app/cam.yaml
destPath:/var/mobile/Containers/Data/Application/181F8B72-402B-4D0C-91D5-0D9759BC607E/Documents/nitro/cam.yaml
error:Error Domain=NSCocoaErrorDomain Code=4 "The file “cam.yaml” doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/private/var/containers/Bundle/Application/3DF49723-E11D-4D67-AD0F-39C2B82B80A4/NitroDemo.app/cam.yaml, NSUserStringVariant=(
), NSDestinationFilePath=/var/mobile/Containers/Data/Application/181F8B72-402B-4D0C-91D5-0D9759BC607E/Documents/nitro/cam.yaml, NSFilePath=/private/var/containers/Bundle/Application/3DF49723-E11D-4D67-AD0F-39C2B82B80A4/NitroDemo.app/cam.yaml, NSUnderlyingError=0x2826476f0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Surprisingly, when I run project second time, the error disappeared, "copyItemAtPath:sourcePath toPath:destPath error:&error" return "YES".
And if I remove this app from my iPhone, then rebuild the project , reinstall and run the app first time, the same error message appears again. And as I expected, when I run the project second time, the error disappeared.
So I wonder what exactly happened in the first time and second time?
====================
UPDATE: I solved this problem by creating an intermediate folder nitro.
The key point here is that, as you may not noticed in the log, the dest path XXX/Documents/nitro/cam.yaml contains a not existed intermediate folder nitro. So in the first time when I call copyItemAtPath:sourcePath toPath:destPath error:&error, it fails and may create that folder (just for my guess). As a result, when I run second time, the copyItemAtPath:sourcePath toPath:destPath error:&error returns YES.
Ok, based on the update you could solve it then by inserting something like below.
[NSFileManager.defaultManager createDirectoryAtPath:newDir
withIntermediateDirectories:YES
attributes:nil
error:NULL];

Objective-c NSInvalidArgumentException Xcode7

Hey Guys I'm just gonna cut to the chase I was trying to play audio
- (IBAction)click1:(id)sender{
Drill.image = [UIImage imageNamed:#"Drillx2"];
AudioServicesPlaySystemSound(PlaySoundID);
}
- (IBAction)click2:(id)sender
{
Drill.image = [UIImage imageNamed:#"Drillx1"];
}
- (void)viewDidLoad {
NSURL *SoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Drill" ofType:#"m4a"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, &PlaySoundID);
[super viewDidLoad];
}
And I get this error
'NSInvalidArgumentException', reason: '*** -[NSURL
initFileURLWithPath:]: nil string parameter'
Thanks,
Derek
Note : Yes the resource for the sound is named Drill.
In the code
NSURL *SoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Drill" ofType:#"m4a"]];
It didn't find the given file in the bundle file path. make sure you have added file in the proper target and also build again after adding the resource to make sure it exist in the bundle
Reference : As written in the parameter Passing nil for this parameter produces an exception.

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"];

iCloud file Upload error (NSFileWriteNoPermissionError = 513)

I'm trying to copy a file to iCloud using the follow code:
NSError *error;
//My Image Source file
NSURL *sourceURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"_219" ofType:#"jpg"]isDirectory:NO];
NSLog(#"source: %#", sourceURL);
NSFileManager *fileManager = [NSFileManager defaultManager];
//Discovering iCloud URL
NSURL *iCloudDocumentsURL = [[fileManager URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:#"Documents"];
NSLog(#"iCloudDocumentsURL: %#", iCloudDocumentsURL);
//Setting destination URL
NSURL *destinationURL = [iCloudDocumentsURL URLByAppendingPathComponent:#"image.jpg"];
NSLog(#"destination: %#", destinationURL);
//Copy file to iCloud
BOOL success = [fileManager setUbiquitous:YES itemAtURL:sourceURL destinationURL:destinationURL error:&error];
if (!success) {
#throw [NSException exceptionWithName:[error localizedDescription] reason:[error localizedFailureReason] userInfo:nil];
}
When I run this code I have the following result:
2012-12-31 15:15:30.888 iCloudTest[10614:907] source: file://localhost/var/mobile/Applications/B2202406-BEB8-41B6-A3C4-2327EFB85E54/iCloudTest.app/_219.jpg
2012-12-31 15:15:31.108 iCloudTest[10614:907] iCloudDocumentsURL: file://localhost/private/var/mobile/Library/Mobile%20Documents/xxxxxxxx~com~gazapps~iCloudTest/Documents/
2012-12-31 15:15:31.110 iCloudTest[10614:907] destination: file://localhost/private/var/mobile/Library/Mobile%20Documents/XXXXXXXX~com~gazapps~iCloudTest/Documents/image.jpg
2012-12-31 15:15:31.122 iCloudTest[10614:907] *** Terminating app due to uncaught exception 'The operation couldn’t be completed. (Cocoa error 513.)', reason: '(null)'
*** First throw call stack:
(0x38c312a3 0x32f1897f 0xbb617 0x3a31058d 0x3a350d71 0x3a34cae5 0x3a38e1c9 0xad241 0x3a351ad1 0x3a35165b 0x3a349843 0x3a2f1c39 0x3a2f16cd 0x3a2f111b 0x371295a3 0x371291d3 0x38c06173 0x38c06117 0x38c04f99 0x38b77ebd 0x38b77d49 0x3a34847d 0x3a3452f9 0xacff9 0x3627cb20)
libc++abi.dylib: terminate called throwing an exception
Looking at documentation, Cocoa error 513 means:
NSFileWriteNoPermissionError = 513,
So basically I'm without access to write stuff on iCloud... Looking at https://developer.icloud.com, everything seems to be ok (the documents folder is there).
What I'm doing wrong ?
iCloud has been working intermittently since December 30/31, even though everything may look good on developer.icloud.com. There's a discussion going on at the developer forums here:
https://devforums.apple.com/thread/176739?tstart=0
It's hard to diagnose iCloud issues at this point since it may just be problems on Apple's iCloud server. Some people have noted (on the discussion thread) that iCloud seems to be working better today than prior days so it may be worth testing your app again now.

Playing video on IOS

I have downloaded the source files from
http://mobile.tutsplus.com/tutorials/iphone/mediaplayer-framework_mpmovieplayercontroller_ios4/
and the app works perfectly, but when I try to swap out my own video by changing the following code:
pathForResource:#"big-buck-bunny-clip" ofType:#"m4v"
to
pathForResource:#"example" ofType:#"mp4"
the app crashes when I try to play the video and I receive a message in the output saying:
"2012-09-13 20:32:59.106 BigBuckBunny[1081:11f03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[NSURL initFileURLWithPath:]: nil string parameter'
** First throw call stack:
(0x1722022 0x10f6cd6 0x16caa48 0x16ca9b9 0x4e53b 0x4e4c5 0x2da4 0x1723e99 0x36d14e 0x36d0e6 0x413ade 0x413fa7 0x413266 0x3923c0 0x3925e6 0x378dc4 0x36c634 0x1e1aef5 0x16f6195 0x165aff2 0x16598da 0x1658d84 0x1658c9b 0x1e197d8 0x1e1988a 0x36a626 0x297d 0x28f5)
terminate called throwing an exception(lldb) "
I have my new video file inside the folder with the original big-buck-bunny-clip.m4v file, and I put it in the project.
What am I doing wrong and how do I fix this?
I managed to play my own video by going to the file within Xcode and checking the box next to BigBuckBunny under Target Membership
Check file exits and filepath you r passing is nil:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"example" ofType:#"mp4"];
if([[NSFileManager defaultManager] fileExitsAtPath:filepath])
{
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
else
{
NSLog(#"File not exists");
}