I am attempting to load a local pdf file using the following code for a Cocoa project (not an iOS project).
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"testPDFFile"
ofType:#"pdf"
inDirectory:#""];
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[[webView mainFrame] loadRequest:request];
I can see the WebView object is loading, but the document is not shown. It looks like the Outlet is connected correctly.
I have tried moving the file to various directory locations, and have tried specifying a directory in the inDirectory parameter.
Currently, the file is located in the “Supporting Files” directory. If I change inDirectory to read…
inDirectory:#”Supporting Files”]; I receive an error that says… “[NSURL initFileURLWithPath:]: nil string parameter”
How can I get this statement to find and show my file?
Try:
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"testPDFFile"
ofType:#"pdf"];
As long, as you are accessing resources in your own bundle, you don't have to set a (foreign) bundle directory.
Related
Hi all We wrote a program that works pretty fine on emulator. But when we upload project to our own device it can read the file and display items but when it tries to erase file and write again it doesnt work.
What might be the possible problems and how to fix this issue?
Seems like you are trying to write the file in the app bundle. For security purposes, Apple does not let you touch the bundle. You have to :
Check that your file exists in the sandbox
If not, copy it from the app bundle
Then, work with the copied file
I think it works in the simulator because there is no sandbox when you run your app in the simulator.
The documentation for finding the right folders in your app's sandbox is below.
Sandbox documentation
File System programming guide
EDIT :
Here is some code (did not run it in Xcode, there could be some typo errors) to copy a file from bundle into a given directory in the Documents directory. Be careful this code overrides the file in the Documents directory every time.
NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *fileDirectory = [documentsDirectory stringByAppendingPathComponent:#"MyDirectory"];
// Create folder if it does not exist
BOOL isDirectory;
if (![[NSFileManager defaultManager] fileExistsAtPath:fileDirectory isDirectory:&isDirectory])
{
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:fileDirectory withIntermediateDirectories:NO attributes:nil error:&error];
if(error)
{
NSLog(#"Error creating folder : %#",error.localizedDescription);
return;
}
else
{
NSLog(#"Folder created");
}
}
// Save to disk
NSString *filePathOnDisk = [fileDirectory stringByAppendingPathComponent:#"fileInSandbox.foo"];
NSString *pathFromBundle = [[NSBundle mainBundle] pathForResource:#"fileFromBundle" ofType:#"foo"];
NSData *fileData = [NSData dataWithContentsOfFile:pathFromBundle];
NSError *error;
if (![fileData writeToFile:filePathOnDisk options:NSDataWritingAtomic error:&error])
{
NSLog(#"Error writing file : %#", error.localizedDescription);
}
else
{
NSLog(#"File saved to disk successfully");
}
Then, when you want to work with your copied file, use the path we created in filePathOnDisk.
Once again, please, read the documentation related to file management in order to understand what you're doing with your files.
I made a fully functional tweak with theos and I need to use an image file
in it , the code for getting the image is correct (tested on Xcode) .
but the image isn't included in the final DEB file .
and I have this makefile :
SDKVERSION=6.0
include theos/makefiles/common.mk
include theos/makefiles/tweak.mk
TWEAK_NAME = MyTweak
MyTweak_FRAMEWORKS = Foundation CoreGraphics UIKit
MyTweak_FILES = Tweak.xm image.png
include $(THEOS_MAKE_PATH)/tweak.mk
But when I try to compile I get :
No rule to make target `obj/image.png.o', needed by `obj/MyTweak.dylib'. Stop.
what can I do to include it ??
(Sorry for bad syntax , asking from iphone).
MyTweak_FILES variable should only include files that can be compiled. Make file handles resources differently.
To include resources you need to create a bundle as follows.
1) Create a folder called Resources in the tweak.xm directory.
2) Put all your resource files (all your PNG's) into that folder.
3) Add the following info to your make file
BUNDLE_NAME = your_bundle_identifier
your_bundle_identifier_INSTALL_PATH = /Library/MobileSubstrate/DynamicLibraries
include $(THEOS)/makefiles/bundle.mk
4) Define your bundle as follows on top of your tweak.xm file.
#define kBundlePath #"/Library/MobileSubstrate/DynamicLibraries/your_bundle_identifier.bundle"
5) You can now initialize the bundle and use the images within your tweak as follows:
NSBundle *bundle = [[[NSBundle alloc] initWithPath:kBundlePath] autorelease];
NSString *imagePath = [bundle pathForResource:#"your_image_name" ofType:#"png"];
UIImage *myImage = [UIImage imageWithContentsOfFile:imagePath]
In the above steps replace your_bundle_identifier with your tweaks bundle identifier which would be in the control file. (ex: com.yourdomain.tweak_name)
Also replace your_image_name with the name of the image you want to use.
You can pretty much use any resources (ex: sound files) the above way.
In addition to the posted answer, it's common practice to place bundles in "/Library/Application Support/" rather than "/Library/MobileSubstrate/DynamicLibraries/"
I'm working on typical newsstand app and I have problem with unzipping downloaded file.
-(void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL {
// copy the file to the destination directory
NSURL *finalURL = [[self contentURL] URLByAppendingPathComponent:#"magazine.zip"];
ELog(#"Copying item from %# to %#",destinationURL,finalURL);
[[NSFileManager defaultManager] copyItemAtURL:destinationURL toURL:finalURL error:NULL];
[[NSFileManager defaultManager] removeItemAtURL:destinationURL error:NULL];
// Downloaded file magazine.zip is in finalURL now and in next step I will try to unzip it
[SSZipArchive unzipFileAth:[finalURL path] toDestinan:[[self contentURL]path] overwrite:YES password:nil error:nil];
[self sendEndOfDownloadNotification];
}
And nothing happens. I checked if the file is really located at finalURL path and yes it is. The SSZipArchive has problem to open the magazine.zip file. I tried to find some example how to unzip downloaded Newsstand issue but I didn't find anything.
destinationURL is the temporary path of zip file you downloaded. You should directly unzip the file from here to destination
[SSZipArchive unzipFileAth:[destinationURL path] toDestination:[[self contentURL] URLByAppendingPathComponent:#"magazine.zip"] overwrite:YES password:nil error:nil];
//Remove temp file
[[NSFileManager defaultManager] removeItemAtURL:destinationURL error:NULL];
How do I move directories in cocoa?
Whenever I use NSFileManager I get an error.
NSFileManager* fileManager = [NSFileManager defaultManager];
[fileManager moveItemAtPath:[srcpath retain] toPath:[dstpath retain] error:&error];
I end up getting the error:
nameOfDir1 couldn't be moved to nameOfDir2
Oh, I had to enter the directory name that I'm copying after the directory that I'm copying to.
I have a console program which is linked to the Foundation framework on Mac. How do I find out the folder the executable is in?
Even though the tool is not in a bundle, you can still use some of the NSBundle methods. For example:
NSString * binaryPath = [[NSBundle mainBundle] executablePath];
NSString * executableFolder = [binaryPath stringByDeletingLastPathComponent];
What about [[NSBundle mainBundle] bundlePath]
The first argument passed to main() ( argv[0] ) is the path to the executable itself. If you wrote said console program, you could get it that way.