2012-11-14 2 views
0

이메일에 라벨 텍스트를 첨부하고 싶습니다. UILabel 내부의 텍스트를 이메일 첨부 파일로 보내려면 어떻게해야합니까? 라벨의 텍스트를 이메일 첨부 파일로 보내는 방법은 무엇입니까?

나는 코드를 사용하고 있습니다 :

-(IBAction)send:(id)sender { 

    if ([MFMailComposeViewController canSendMail]) 
    { 
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 100)]; 
     label.numberOfLines = 0; 
     label.textAlignment = UITextAlignmentCenter; 
     label.text = @"text"; 

     [self.view addSubview:label]; 
     [label release]; 

     MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; 

     mailer.mailComposeDelegate = self; 
     [mailer setSubject:@"Subject"]; 

     NSString *fileName = @"my file.txt"; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName]; 

     NSData *myData = [NSData dataWithContentsOfFile:path]; 
     [mailer addAttachmentData:myData mimeType:@"text/plain" fileName:fileName]; 

     // Fill out the email body text 
     NSString *emailBody = @"Email Body"; 

     [mailer setMessageBody:emailBody isHTML:NO]; 

     [self presentModalViewController:mailer animated:YES]; 

     [mailer release]; 
    } 
    else 
    { 
     UIAlertView *alertm = [[UIAlertView alloc] initWithTitle:@"Failure" 
                 message:@"Please make sure that your email application is open" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles: nil]; 
     [alertm show]; 
     [alertm release]; 
    } 
} 

가 어떻게 라벨이 이메일로 첨부에 링크 할 수 있습니까?

도움이 될 것입니다.

+0

? .txt 파일? – Martol1ni

+0

@woz 그가 자기에게 메시지를 보내달라고 요청했습니다. –

+0

@woz 메일보기 컨트롤러를 열었을 때 첨부 파일을 볼 수있는이 코드로 인해 이메일에 아무 것도 없다면, 나는 이유를 모른다. 저를 도와주세요 ? – 4slices

답변

0

거의 다 왔어.

- 당신은 파일에서 텍스트를 읽고이

NSData *data = [self.label.text dataUsingEncoding:NSUTF8Encoding]; 
+0

그래서 대체해야합니다 : NSData * myData = [NSData dataWithContentsOfFile : path]; 코드로 하지만 내 코드에서 선언 된 (경로)에 대해 오류가 발생하거나 잘못된 것이라고 생각합니까? – 4slices

0

파일에서하지만, 라벨 자체에서 데이터하지 읽어 === 당신은 라벨을 추가 할 수 있습니다 첨부 본문에 텍스트. 이 같은

 NSString *emailBody = [NSString stringWithFormat:@"Your Voice File Attached. %@", label.text]; 

뭔가

당신이 파일로 보내 원하는 작업 형식
+0

첨부 파일로 추가하여 편집 할 수 없습니다. – 4slices

+0

아, 죄송합니다. 그러면 "file.txt"파일에 레이블 텍스트를 추가해야한다고 생각합니다. –

+0

또는 두 번째 .txt 파일을 label.text와 연결하십시오. –

관련 문제