I want to open an email attachments(say .ppt, .doc, .xls .. etc files) through my iOS(iPad)app.
I am able to achieve the "Open In" on the mail attachments, my app opens up after that.
The handleOpenURL function will be called along with the url of the opened attachment.
Now in this function when i try to check for the file existence, it is not present.
NSFileManager *defaultManager = [NSFileManager defaultManager];
NSString *filePath = [NSString stringWithFormat:#"%#",url];
if ([defaultManager fileExistsAtPath:filePath]) {
NSLog(#"fileExists");
}else{
NSLog(#"file does not Exists");
}
What i want to achieve here is I want to copy the file through this url and paste it in some other folder.
But here the file itself doesn't exists.
Any help is highly appreciated.
That's not the proper way to convert an NSURL to a path. You should do:
NSString *filePath = [url path];
Related
I am saving video/image in document directory.Now once the image is saved in document directory I want to save its reference in my local database.So I am thinking I can save URL of the image in the local database.
So is it constant throughout my app?
It's not constant, i have observed every time you launch the app it'll be different, but your data is moved to this new path. You can save your file name in your database, and dynamically append this file name to NSDocument directory.
- (NSString *)documentsFilePath:(NSString *)fileName {
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths firstObject];
NSString *filePath = [docsDir stringByAppendingPathComponent:fileName];
return filePath;
}
- (void)storeFile:(NSString *)fileName {
NSString *filePath = [self documentsFilePath:fileName];
// create if needed
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
// Write your data to file system here...
}
}
- (void)deleteFile:(NSString *)fileName {
NSString *filePath = [self documentsFilePath:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSError *deleteErr = nil;
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&deleteErr];
if (deleteErr) {
NSLog(#"Can't delete %#: %#", filePath, deleteErr);
}
}
}
Please handle nil checks and store only filename in DB
No, it's not constant. Whenever your app reinstall or updated on device the document directory will change, because when app installed on device os made an directory for app with some random id and each install this random it get changed by OS.
So, you need to make it dynamic own your own, like store the file name only and append the document directory path while using it.
I would suggest only saving the filename or subdirectory/filename (if you have a subdirectory) in the database and then only attaching that to the NSDocumentDirectory.
This will ensure that you always know where the file is...
NSDocumentDirectory is however consistent accross updates, so the files should remain in the document directory even if you update...
I'm getting this behaviour using Xcode 8.0. The problem is, after downloading a file and storing it on documents directory (code provided below), QLPreviewController only displays document's name and size. The property currentPreviewItem returns the correct path document. What's even more strange, is that if I try to open that document from another controller in my app, it works fine. I've implemented both QLPreviewControllerDelegate and QLPreviewControllerDataSource.
Code for downloading and saving document:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSURL *url = [NSURL URLWithString:file[#"url"]];
NSData *data = [NSData dataWithContentsOfURL:url];
if (!data) {
completion([NSError new]);
return;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:AppName];
path = [path stringByAppendingPathComponent:file[#"name"]];
file[#"filePath"] = [NSURL fileURLWithPath:path];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
completion(nil);
});
});
Then, when user selects a document I use the content saved on #"filePath" to show QLPreviewController. I've tried pushing it and presenting it modally and in both cases it just displays a gray page with document's name and size.
File wasn't showed because downloaded contents didn't contain file's extension. When I tried opening it in other controller, it worked because I temporary used another file name containing the correct extension. So I solved the problem adding the extension .pdf to downloaded files that didn't include it. This made QLViewController displaying correctly the file.
In my case, a file was created with the correct name, but the file contained an error message from the server, thus not representing the expected file structure.
I just make a file browser for iOS, I just need to know how to open any kind of file in a UIWebview. I think the webview can open any kind of file, that's because I chose to use a UIWebView to view the files directly in the app. Now my question.
How to programmatically switch to a UIViewController in the same UIStoryBoard and display the file? The file URL is stored in a string called path.
This is how fare I am, I just need to add the file viewer.
Can someone help me?
-(void) displayFile:(NSString*)fileName
{
NSError *error = nil;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
TO get the filename
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath]) //Optionally check if folder already hasn't existed.
{
// do something
}
}
Hope this helps you...
A little embarrassing question, but I can find an answer which works in my case... I need to put some xml file (settings.xml) in order to read some data from it during application runtime.
According to some answers here and not only here, I have putted it here:
~/Library/Application Support/iPhone Simulator/5.0/[AppUUID]/Documents
and I'm trying to use it as follows:
// Loading data from external XML File
NSURL *url = [[NSBundle mainBundle]
URLForResource: #"settings" withExtension:#"xml"];
NSError *err;
if ([url checkResourceIsReachableAndReturnError:&err] == NO){
NSLog(#"FILE NOT FOUND");
}
Result: "FILE NOT FOUND".
I've tried to do put the file under any possible directory in
~/Library/Application Support/iPhone Simulator/5.0/[AppUUID]/ and efect is still the same.
I'm using XCode 4.2
If you are putting the file into the .../Documents folder then you need to use the following code to access it (you are looking for it in the App Bundle, which is a different location altogether):
NSString *docFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [docFolder stringByAppendingPathComponent:#"settings.xml"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filename])
{
// Read file
}
else
{
NSLog(#"settings.xml file not found!");
}
I am donwloading and retrieving files with ASIHTTPRequest. It already works, but now I am trying to break the download, when it is already stored. I can't figure out, how to implement this problem. I would like to solve it with an if-clause: if the file is not cached, download it else break. Would you mind to help me writing a proper Objective-C code? I have the file path, if I need it so compare or look after that file.
Thanks in advance!
pseudo code :
-(IBAction) download : (id) sender {
if (data1.pdf) {
// the download algorithm
}
else
break;
}
If you mean how you can check if a file is stored in the documents directory of your device, then you can use:
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* file = [documentsPath stringByAppendingPathComponent:#"data.pdf"];
if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
...