How to view files in iPhone app? - iphone

I am building an iPhone app that can save email attachments. I would like to be able to view the files from inside the application as well. I need to be able to view as many kinds of files as possible: .txt, .pdf, .png, .jpeg., .doc, .xls, etc.
The files will reside inside of my application's documents folder. What is the best solution for this? My first thought is to convert my file-paths to URLs and load them in a UIWebView, but I'm not sure if this is the best way, or if it will handle all those file types. Any thoughts?

The way Apple's own Mail app does this is with the QuickLook framework. I'd suggest you do the same. QLPreviewController does all the heavy lifting for you. You can also use UIDocumentInteractionController; this is still using QuickLook behind the scenes to generate the preview.

The UIWebView is the best way to show this kind of file.
Alternatively you can try to read the document in an other app with this code:
UIDocumentInteractionController *interactionController = [[UIDocumentInteractionController interactionControllerWithURL:document.documentURL] retain];
interactionController.delegate = self;
BOOL canOpenDocument = [interactionController presentOpenInMenuFromRect:CGRectMake(365, 200, 300, 400) inView:self.view animated:YES];
if(!canOpenDocument) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Your phone can't read this kind of file, please install an app from appstore for that" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}

Related

UIWebView Error Delegate Method vs Reachability

I'm in the process of submitting my app to the App Store, but I read that I must notify the user if the internet connection is down when my app needs it. The Apple page mentioned Reachability as well. Currently, though, I'm using the UIWebView delegate method didFailLoadWithError...
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error Loading" message:[error localizedDescription] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[errorAlert show];
}
...and it's working fine. My question is, will my app be rejected for not using Reachability to do this, or is it fine doing what I am currently doing?
Thanks in advance.
No, you're perfectly ok using didFailLoadWithError:.
Reachability class could be used to check if host is up (or internet connection at all) before even trying to load some page. But it is not neccessery, as long as you handle the possible errors - which obviously you do.
EDIT:
It is still a good practice to know wheather you will be able to reach a certain host or not. You could even modify GUI for each case (instead of just reporting an error). But this can always be done in update :)

iPhone - a UI component that looks like an Alert box - Beginner

I need to know what this UI component is called ? It looks like an Alert but has textfeilds and buttons in it ?
1.) What is this UIComponent called ?
2.) Is there any video tutorial which shows how to implement this ? (If so link) or any tutorials that discuss the implementation of this
It's UIAlertView
Here is the tutorial to make custom UIAlertView
That is an Android component, there is no equivalent in iOS.
There is the UIAlertView, but you cannot edit text within it.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/cl/UIAlertView
If you want to be able to let users edit text in the component, you have to create it from scratch, e.g. create a UIView and add a UITextView to it and some UIButtons and create the functionality to display/dismiss the component yourself.
AlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
The component on iPhone is called a UIAlertView. You can add subviews in it to get text-fields, although this is not recommended by Apple - but you won't get rejected either. In iOS5, there are dedicated UIAlertView types for this. If you are building for iOS5, this is the method you should use.

download and open zip folder path in iphone

i download a zip file from web server and store on a specific location.
is it possible after downloading user can see that downloaded file ? i mean how user know where file is stored so instead of searching by user i want to open that folder for user...and if this possible then how?
thank you in advance
Are you downloading the zip file within the app and storing it somewhere?
#pooja i had worked on the same situation....in my case i was downloading the zip file from web server and i was saving that file to the documentsDirectory itself.Now as you are saying that how will user came to know that the file is downloaded or not. Then in this case i would suggest you or what i had done was.....
//Your request
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setDelegate:self];
[request setDownloadDestinationPath://Your path here];
[request setDidFailSelector:#selector(zipFileDownloadFailed:)];
[request setDidFinishSelector:#selector(zipFileDownloaded:)];
- (void)zipFileDownloaded:(ASIHTTPRequest *)request{
UIAlertView *Downloaded = [[UIAlertView alloc] initWithTitle: #"Successfully Downloaded" message: #"" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[Downloaded show];
[Downloaded release];
Downloaded = nil;
}
- (void)zipFileDownloadFailed:(ASIHTTPRequest *)request{
UIAlertView *DownloadFailed = [[UIAlertView alloc] initWithTitle: #"Download Failed" message: #"" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[DownloadFailed show];
[DownloadFailed release];
DownloadFailed = nil;
}
Now this alert will conform the user that the file is successfully downloaded or not....Now use this file wherever you want in your app...user is now conformed that the file is downloaded or not ......no need to show that to him at that place where you are downloading the file that will be something which will be so complicated.
Edited my code as per your last comment : Yes you have to unzip that file whose zip file you had downloaded within your application only..How will user unzip that file .....you have to unzip that file programatically and you have to show the contents of that file in your application.....Look there is a predefined API to zip and unzip files so try doing some searches and unzip your file in your application....
Also have a look on iPhone Unzip code as asked earliar and also download the ziparchive from here......i had taken help from all of them.
Hope you got my point and this answer will be useful to you.
Good Luck!

Trying to add a Vibrate and Sound to my Alert View

One thing I've learned at engineering school is to always to intense validation of input. I think it's great that with the iPhone SDK you can create a sound and a vibrate option. I would like to put both of these into my Alert View, which shows when the user doesn't fill in a field correctly.
However, I'm getting a ton of errors. Is it not possible to put the vibrate and sound options within an alert view? Here is the code I am using below,
//create vibrate
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
//play sound
SystemSoundID pmph;
id sndpath = [[NSBundle mainBundle]
pathForResource:#"mySound"
ofType:#"wav"
inDirectory:#"/"];
CFURLRef baseURL = (CFURLRef) [[NSURL alloc] initFileURLWithPath:sndpath];
AudioServicesCreateSystemSoundID (baseURL, &pmph);
AudioServicesPlaySystemSound(pmph);
[baseURL release];
//show alert view
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Age Error"
message:#"Your age must be at least 40 years old and less than 100 years old"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
I have all of the above code in the
- (void)textFieldDidEndEditing:(UITextField *)textField
method.
Here is the errors I get when I try to run it
http://screencast.com/t/Nzc5NDdhMmI
Any help would be greatly appreciated. Not sure what I'm doing wrong since I'm pasting this code directly from another source online.
I've never used the Sound Services, but it looks like you need to import the AudioToolbox framework.
#import <AudioToolbox/AudioToolbox.h>

Best Practices for implementing a modal Alert View using Cocoa Touch?

Does anyone have any best practices, tips & tricks, or recommendations for how to create modal Alert Views with Cocoa Touch?
I like to think there is a way to make this difficult task trivial or at least easier.
You can use something like this:
void AlertWithMessage(NSString *message)
{
/* open an alert with an OK button */
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Name of the Application"
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
I just use UIAlertView to display modal alerts. What additional functionality are you looking for?
Check out the UICatalog sample code from Apple. It shows the usage of both Alerts and Sheets on the phone.