In my project i saved the PDF file from NSData & displayed in WebView it working fine, i checked with Simulator & find in app folder the file is successfully downloaded without any corruption.
Here my problem is how can i view the file in real device(iPhone & iPad too) & share the files. like android file manager..
there is no option to view & share(Mail , Whatsapp etc..) the files?
my code below
NSError *errr;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/Folder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&errr]; //Create folder
[data writeToFile:[NSString stringWithFormat:#"%#/%#", dataPath, #"Report.pdf"] atomically:YES];
}
[self.webView loadData:data MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
Help me..
Actually, instead of WebView you could also use the QuickLook
Framework, which is designed for this specific purpose. You just pass
the location of the file and conform to the protocols. It will present
a view controller with the document inside and also give you native options to share and open the document in other apps.
Find the answer here.
Related
I'm writing an application for the Apple watch. I'm using the following method (from this SE answer) to write to a log file:
- (void) writeLogWith: (NSString *) content {
//Get the file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:#"whathappened.md"];
//create file if it doesn't exist
if(![[NSFileManager defaultManager] fileExistsAtPath:fileName])
[[NSFileManager defaultManager] createFileAtPath:fileName contents:nil attributes:nil];
//append text to file (you'll probably want to add a newline every write)
NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
[file seekToEndOfFile];
[file writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
[file closeFile];
return;
}
I'm running it by plugging in my phone and running it directly on my watch. The function is definitely executing (I've stepped thought with the debugger) and it also knows that the file exists and doesn't repeatedly try and create it.
Xcode tells me that the file information is:
Printing description of documentsDirectory:
/var/mobile/Containers/Data/PluginKitPlugin/8503AD6C-6EC9-4522-A867-27109B01B615/Documents
Printing description of documentsDirectory:
(NSString *) documentsDirectory = 0x16d66890
Printing description of fileName:
(NSString *) fileName = 0x16d66950
Printing description of fileName:
/var/mobile/Containers/Data/PluginKitPlugin/8503AD6C-6EC9-4522-A867-27109B01B615/Documents/whathappened.md
I'd like to know if it's writing things correctly, but when I look at the container (following these SE answers), the documents directory is empty.
My question is: where did my file go? And how can I find it?
I think what might be giving you problems is the NSFileHandle object. I have written thousands upon thousands of files to the documents folder and I have never used NSFileHandle to do this. Simply use the built in method on NSString to write your string to the file path.
Try this:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [[documentsDirectory stringByAppendingPathComponent:#"whathappened"] stringByAppendingPathExtension:#"md"];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
}
NSString *string = #"The String You Want To Write.";
NSError *error;
[string writeToFile:filePath atomically:false encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(#"There was an error writing file\n%#", error.localizedDescription);
}
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSLog(#"File exists :)");
}
else {
NSLog(#"File does not exist :(");
}
From other research, the answer appears to be:
If you are storing a file on the Apple Watch, it is stored in it's own container, which isn't visible via xcode.
It indeed, appears to be related to this bug report: https://openradar.appspot.com/radar?id=5021353337946112, found via this SE: How to export shared container of an iOS App with Xcode6.2?
I have a scenario in my application that I need to download mp3, pdf, text files using my application.
How can I download, and where to store, data using my application?
NSString *stringURL = #"http://www.somewhere.com/thefile.png";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"filename.png"];
[urlData writeToFile:filePath atomically:YES];
}
You can store in Document directory,Below is example to store a image .Below is link explained to store audio video file
How to download audio/video files from internet and store in iPhone app?
Document Directory is best to store data.
NSString *path = [[self applicationDocumentsDirectory].path
stringByAppendingPathComponent:#"fileName.txt"];
[sampleText writeToFile:path atomically:YES
encoding:NSUTF8StringEncoding error:nil];
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
}
It depends on what will you need to do with data. If data can be recreated or downloaded, save it in temp o cacje directory. If data is created by user and have to be backed up, you can use Documents to sync with iCloud.
I recommend you to read Apple documentation because if you this wrong way, your App could be rejected.
https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
You can exclude your files to be backed up (and stored in Documents folder)
https://developer.apple.com/library/ios/qa/qa1719/_index.html
I am new to iOS and just started learning. I have created an App for downloading files into Documents folder of the directory. When I download the files on simulator, I can find it through the path : /Users/......./Simulator/iOS7.0/Applications/Documents.
But I am not able to find the files when I run the App on the device and download the files. I have given the path using the following code:
filePath = [NSHomeDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: #"Documents/%#", self.downloadedFilename]];
Can anyone please tell me the way to find the files on the device using iTunes?
How about just downloading all the files from the sandbox to your Mac? Open Xcode, Window->Organizer.
Click on the Applications entry for the connected device, and click on your Application in the list of apps. Now you can see the data files on the device for that app, and you can download the whole bundle from the device to your Mac.
Use below sample code for create and write data with in created folder document
NSData *data = [[NSData alloc] initWithBytes:saves length:4]; //replace with your file content
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/MyFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *appFile = [dataPath stringByAppendingPathComponent:#"MyFile"];
[data writeToFile:appFile atomically:YES];
Update: Did you check this bool in your info.plist before connect it with itunes.? Check this bool in your info.plist to share files with itunes. Refer UIFileSharingEnabled and see this picture below.
Hey guys a quick question!
I am making an iPad app which uses the dropbox sync api. Now I am wondering how to play movie files within my dropbox filesystem in the MPMovieplayer. Since the dropbox filesystem is not the same as the local sandbox and I don't know how to retrieve a nsurl of the file I can't play the video.
This is something I tried (my last hope) but it doesn't work:
+(NSString*)loadMovie : (DBPath*)path
{
DBFile *file = [self openFileAtPath:path];
NSData *data = [[NSData alloc] initWithData:[file readData:nil]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *vpath = [documentsDirectory stringByAppendingPathComponent:file.info.path.name];
[[NSFileManager defaultManager] createFileAtPath:vpath contents:data attributes:nil];
return vpath;
}
You'll need to write the file out to local storage first. See https://forums.dropbox.com/topic.php?id=110085 on the Dropbox forum.
i am new in ipad programming. At the moment i am trying to save and retrieve images using the assetslibrary framework. I do not want to save the images to the photos folder or an album of the photos folder, because then i will not have the permission to access them. I am working with ios 5. I do not want to use the uiimagepicker-popovercontroller way to retrieve them. I just want to put the images in a custom folder on ipad and then retrieve them with assets and put them into an array. I would appreciate any help. Thank you in advance.
Below 2 ways to save images.
Save to general album
UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
Save to the local APP folder
NSData *imageData = UIImagePNGRepresentation(imageToSave);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"3DImage.png"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];