问题描述
|
我使用NSFileManager读取和写入文件。它可以正常工作,但是我无法查看写入该文件的数据,它可以与模拟器一起使用,但不能在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:@\"[email protected]\"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@\"[email protected]\",@\"[email protected]\",nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@\"[email protected]\"];
[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:[email protected][email protected],@[email protected]&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]];
}
解决方法
您可以使用实时调试器吗?
1)设置断点以检查变量/路径
2)创建一些try-catch块,以查看某处是否存在“未处理的”异常