I used NSFileManager to read and write in a file. It works fine, but I cannot view the data written in that file, it works with simulator but doesn’t work in the iPhone.
viewcontroller.h:
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface next : UIViewController <MFMailComposeViewControllerDelegate,UITextViewDelegate,UIAlertViewDelegate>{
IBOutlet UIButton *read;
}
@property(nonatomic,retain)IBOutlet UITextView *txt;
@property (nonatomic, retain) IBOutlet UILabel *message;
-(IBAction)showPicker:(id)sender;
-(void)displayComposerSheet;
-(void)launchMailAppOnDevice;
@end
viewcontroller.m:
-(void) viewDidLoad {
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"mainbg.png"]];
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSString *path = [[NSBundle mainBundle] pathForResource:@"help" ofType:@"txt"];
if ([filemgr fileExistsAtPath:path ] == YES)
NSLog (@"File exists");
else
NSLog (@"File not found");
NSString *fileName = [[NSBundle mainBundle] pathForResource:@"help" ofType:@"txt"];
NSString *content1 = [[NSString alloc] initWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
//use simple alert from my library (see previous post for details)
//NSArray *lines = [content1 componentsSeparatedByString:@"\n"];
txt.text = content1;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"file"
message:content1
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
NSLog(@"content1");
[content1 release];
[alert release];
[super viewDidLoad];
}
//mailing
-(IBAction)showPicker:(id)sender
{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Hello friends!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"me11@yahoo.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"me@xyz.com", @"me2@xyz.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"hii@exe.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"help" ofType:@"txt"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"file/txt" fileName:@"help"];
// Fill out the email body text
NSString *emailBody = @"hii how r u !";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
message.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
message.text = @"Email: Canceled";
break;
case MFMailComposeResultSaved:
message.text = @"Email: Saved";
break;
case MFMailComposeResultSent:
message.text = @"Email: Sent";
break;
case MFMailComposeResultFailed:
message.text = @"Email: Failed";
break;
default:
message.text = @"Email: Not Sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Workaround
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
NSString *recipients = @"mailto:friend@abc.com?cc=me@xyz.com,@me2@xyz.com&subject=Hello !";
NSString *body = @"&body=hello how r u !";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
Can you use the real-time debugger ?
1) set break points to check the variables/path
2) create some try-catch blocks to see if there’s an ‘unhandled’ exception somewhere