2011-05-30 31 views
1

我使用NSFileManager來讀取和寫入文件。它工作正常,但我無法查看該文件中寫入的數據,它可以在模擬器中使用,但在iPhone中無法使用。 viewcontroller.h:如何查看由Xcode中的代碼寫入文件中的數據?

#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

你最有可能做了一些擰的地方......如果您張貼一些代碼我們可能會發現您的錯誤在哪裏。 – Vladimir 2011-05-30 06:39:29

+0

請編輯你的問題,並把代碼放在那裏(用適當的格式) - 它是不可能在評論中閱讀... – Vladimir 2011-05-31 07:16:44

+0

現在檢查它,並在我的電話中,如果可能的話錯誤。 – megha 2011-06-01 07:11:51

回答

0

你可以使用實時調試器嗎?
1)設置斷點檢查變量/路徑 2)創建一些的try-catch塊,看看是否有一個「未處理」異常的地方

相關問題