pdf download link in ipad app not working - ipad

i have a standalone offline html application which is not meant for deployment on a server.
i have converted that html application into an ipad app.
The application has links to local pdf and excel files. I have provided links to download those file in the application. It works fine in browser.
But in ipad the links to download pdf report and excel sheet reports do not work (even if I tap on the links pdf doesn't show up.It behaves like a dead link!)
Any suggestions are highly appreciated!

For downloading pdf try following:
1) Create one button to call printTapped() method
(UIBarButtonItem is good for this here)
printTapped()
- (void)printTapped
{
NSData *pdfdata = [[NSData alloc] initWithContentsOfURL:#"try your url from where you want to download the pdf"];
//Store the Data locally as PDF File
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docsDirectory = [paths objectAtIndex:0];
NSString *filePath = [docsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.pdf",app.stritreejob]];
//=======//
}

Related

Is it possible to download a complete directory from the server in Objective-c

I am developing an app where I have to download a complete directory. This directory will contain for example:
article1/index.html
/images/image1.png
/image2.png
/image3.png...
I have tried to download this, but I only can download the index.html.
I am using this code:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(#"Downloading Started");
NSString *urlToDownload = #"http://localhost:8888/";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/issue/article%#/", documentsDirectory,articleID];
NSLog(#"filepath: %#", filePath);
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:NO];
NSLog(#"File Saved !");
});
}
});
If anyone has an idea. Let me know.
Thank you
The reason you're only getting index.html is because this is what the web server is serving you when you request that directory. If there is no index.html page, it will probably give you a directory listing - however, this is usually in HTML format and you would have to manually extract the file names from this output.
If you don't have access to this web server, your job is much harder - if you can get an HTML directory listing, you'll have to parse it yourself; if not, you can't retrieve the directory list at all. In this case, it seems like you do have access to the web server, which is great! You can make this task a lot easier for yourself...
Option one
If the data isn't likely to change, you can create a simple text document in the root directory like filelist.txt that contain a list of files/paths in the directory. Your app can first request this list, separate the entries and then start downloading each file.
Option two
You could create a simple web script (in something like PHP or your language of choice) that lists the current directory contents in a format that can be easily digested by your app - JSON, newline-separated, or anything else.
Option three
Package the directory contents in a .zip file, and have your app download and extract the archive. ZipArchive is one library that allows you to unzip files in Objective-C, and there's an easy tutorial on how to do this available on iCodeBlog.
Also, as a side note, I see you're using NSDocumentDirectory for downloaded content. Apple advises that the Documents folder should only be used for user-generated content, which does not include downloaded content. You should use NSCachesDirectory or NSApplicationSupportDirectory for data that your app downloads (note that you may have to create them first).

Using NSTemporaryDirectory without increasing tmp folder size

In my app the user has the possibility to upload and download 700*700mb images. These images are downloaded and displayed in a few UIImageViews.
My app itself is quite small, but the Documents & Data folder is enormous e.g. 17gb and it keeps increasing exponentially. My app runs on both ipad and on iPhone. Will the docs & data folder in my ipad is 6mb, on my ipod it is 17gb. I have downloaded the app package, and found out that the tmp folder is huge. there are some Stack-log files 1gb big. I found out the problem was related to the NSTemporarydirectory.
I save an image in this way:
NSString *title = [[NSUserDefaults standardUserDefaults]objectForKey:#"Folder3"];
PhotoiPhoneViewController* sharedSingleton = [PhotoiPhoneViewController sharedManager];
NSString *destDir = [[NSUserDefaults standardUserDefaults]objectForKey:#"Folder2"];
NSString *filename3 = [NSString stringWithFormat:#"%#%#sizefile.pages.%#", destDir, title, sharedSingleton.tagString];
NSString *SizePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"%#sizefile.pages.%#", title, sharedSingleton.tagString]];
[self.restClient loadFile:filename3 intoPath:SizePath];
Is this the right way to do it, or should I use:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *CommentPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#commentfile.txt.%ld", title, (long)sharedSingleton.tagNumber]]
Since I want the files to be deleted after use, I thought that the NStemporaryDirectory would be deleted as soon as the file was used. What should I do??
You could look into using NSURLCache as these manage the purging.
See also NSHipster/NSURLCache
Or you could use a library like SDWebImage which will handle a cache size as well
Edit: In answer to your question
The operating system used to leave files in the temporary directory, however, since iOS 5.0 it will purge files from this directory if it sees the need. See this link for more information

Converting an iPhone IOS Video File Uploader to work with a file stored in document directory

This is my first real project. I have an app that captures several seconds of video using AVFoundation, outputs this to a file in the documents directory and lets the user preview the video before they upload it using HTTP and a PHP script on my website.
All the video capture and preview work perfectly but I am stuck on uploading the video file.
I learnt a lot from this simpleSDK video which shows how to achieve the desired effect using a video file stored in the apps main bundle.
The code from the tutorial that set up videoData ready to upload originally looked like this:
NSData *videoData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"mov"]];
NSString *urlString = #"http://www.iphonedevnation.com/video-tutorial/upload.php";
The filename of the video file that I need to upload is always unique and generated using CFUUIDCreateString. I join this string to the path for the documents directory, add ".mov" to the end of it and save it into a text file for retrieving later.
This all works as I am able to retrieve the filename from the file and use it to preview the movie clip elsewhere in the app.
My path is in an NSString, that I have tried converting to NSURL and removing the file suffix to get it to work with the NSData *videoData.........line but it doesn't compile, I get an "No known class method for selector 'dataWithContentsOfFile:ofType.' error. I am targeting iOS 5 and using Xcode 4.3 with ARC and Storyboards.
I've been at this for best part of 5 hours now so hopefully someone can help. My code, which included tips from elsewhere on converting from a NSString to NSURL follows:
NSString *content = [[NSString alloc] initWithContentsOfFile:lastSavedTalentFilenamePath
usedEncoding:nil
error:nil];
NSLog(#"content=%#",content);
//Need to now remove the '.mov' file type identifier
NSString *shortContent= [content substringToIndex:[content length]-4];
NSLog(#"***************shortContent***************%#", shortContent);
NSURL *convertedContent = [NSURL fileURLWithPath:shortContent];
NSLog(#"***************convertedContent***********%#",convertedContent);
NSData *videoData = [NSData dataWithContentsOfFile:convertedContent ofType:#"mov"];];
There is no NSData method called dataWithContentsOfFile:ofType:
The methods available are:
+ dataWithContentsOfFile:
+ dataWithContentsOfFile:options:error:
both of which take the file location as an NSString so there's not need to convert to an NSURL

Transfering text files from a MacBook Pro to an iPad

I am writing an iPad app that will use several text files on my MacBook Pro as a data source for UITableViews that will display on the iPad.
Several questions:
I understand that in order for my app to fetch files from my MacBook Pro over the USB/iPad connector, my app must support file sharing. How do I accomplish this?
Since Apple made the iPad an appliance, I can't see its file system. So how can I declare paths to store the fetched files? Is the iPad a multi-user computer with multiple user home directories?
Can I write my app to interface with an SD card in the accessory connector so as to fetch text files from that card? What class should I use to do that?
[http://developer.apple.com/library/ios/#documentation/General/Reference /InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW1][1]
Add the UIFileSharingEnabled key to your Info.plist file. To see the files, open iTunes, look on the left panel, click on your iPad, look at the toolbar above the main content pane, click on "Apps", SCROLL DOWN, and you will see that you can drop files and export files (but you can't drag them, which is annoying). I actually got stumped on this, too
The way you make the file sharing files visible is to write them to a magical directory, which is obtained by the following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentDirPath = [paths objectAtIndex:0];
Here is the routine I use in-full:
NSString* FileUtils_newUserVisibleFilePath(NSString* toFile) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentDirPath = [paths objectAtIndex:0];
NSString* filePath;
if ([documentDirPath characterAtIndex:[documentDirPath length]-1] != '/') {
filePath = [[NSString alloc] initWithFormat:#"%#/%#",documentDirPath,toFile];
} else {
filePath = [[NSString alloc] initWithFormat:#"%#%#",documentDirPath,toFile];
}
[pool release];
return filePath;
}
I have no idea. But, I also know that a lot of iPad users don't use SD cards so I would consider this a minority feature
What some developers end up doing is making an HTTP server available on the iPad.
[1]: http://developer.apple.com/library/ios/#documentation/General/Reference /InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW1

Issues in save pdf file on iPad

I have a pdf file in my iPad application. I want to save that pdf file on iPad so that I can read it out of my application. I am using the following path for save pdf file on iPad:-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileBPath = [documentsDirectory stringByAppendingPathComponent:#"tmp.pdf"];
But I am not able to find this pdf file on my iPad.
Can anyone suggest me how can I find it ?
Is file path wrong? then suggest me the file path for saving the pdf file ?
Thanks in Advance
1) See this SO question/answer regarding creation of a pdf from a UIView
2) If you already have the pdf file as an NSData object, you can save that to a file in your documents directory using
[NSData writeToFile:fileBPath atomically:YES];
3) On the other hand, if the PDF file is part of your application bundle (ie included with the app at compile time) it won't be in the documents directory.
Take a look at the documentation for [NSBundle mainBundle] -- that is where your PDF will be.

Resources