iOS 개발에서 메일과 문자 발송을 실현하는 간단한 예

4853 단어
이메일 보내기 1.라이브러리 파일 가져오기:MessageUI.framework 2.도입 헤더 파일 3.프록시 및 코드 구현 예:
 
  
- (void)didClickSendEmailButtonAction{ 
 
    if ([MFMailComposeViewController canSendMail] == YES) { 
         
        MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init]; 
        //  ( , "delegate", , 3 ) 
        mailVC.mailComposeDelegate = self; 
        //   
        NSArray *sendToPerson = @[@"[email protected]"]; 
        [mailVC setToRecipients:sendToPerson]; 
        //   
        NSArray *copyToPerson = @[@"[email protected]"]; 
        [mailVC setCcRecipients:copyToPerson]; 
        //   
        NSArray *secretToPerson = @[@"[email protected]"]; 
        [mailVC setBccRecipients:secretToPerson]; 
        //    
        [mailVC setSubject:@"hello world"]; 
        [self presentViewController:mailVC animated:YES completion:nil]; 
        [mailVC setMessageBody:@" , " isHTML:NO]; 
    }else{ 
     
        NSLog(@" "); 
     
    } 
 

 
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ 
 
    switch (result) { 
        case MFMailComposeResultCancelled: 
            NSLog(@" "); 
            break; 
        case MFMailComposeResultFailed: 
            NSLog(@" "); 
            break; 
        case MFMailComposeResultSaved: 
            NSLog(@" "); 
            break; 
        case MFMailComposeResultSent: 
            NSLog(@" "); 
            break; 
        default: 
            break; 
    } 
     
    [self dismissViewControllerAnimated:YES completion:nil]; 
}  
 
//  , ,  
- (void)didClickSendSystemEmailButtonAction{ 
 
    NSURL *url = [NSURL URLWithString:@"[email protected]"]; 
    if ([[UIApplication sharedApplication] canOpenURL:url] == YES) { 
         
        [[UIApplication sharedApplication] openURL:url];  
      
    }else{ 
     
        NSLog(@" "); 
    } 
 

문자 보내기 전 3단계 도입 설정과 메일 보내기
 
  
//  API  
- (void)didClickSendMessageButtonAction{ 
     
    if ([MFMessageComposeViewController canSendText] == YES) { 
         
        MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init]; 
        //   
        messageVC.messageComposeDelegate = self; 
        //  To Who 
        messageVC.recipients = @[@"18757289870"]; 
        messageVC.body = @"hello world"; 
        [self presentViewController:messageVC animated:YES completion:nil]; 
         
    }else{ 
     
        NSLog(@" "); 
    } 

 
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ 
     
    switch (result) { 
        case MessageComposeResultCancelled: 
            NSLog(@" "); 
            break; 
        case MessageComposeResultFailed: 
            NSLog(@" "); 
            break; 
        case MessageComposeResultSent: 
            NSLog(@" "); 
            break; 
        default: 
            break; 
    } 
     
    [self dismissViewControllerAnimated:YES completion:nil]; 
 

 
//   
- (void)didClickSendMessage2ButtonAction{ 
     
    NSURL *url = [NSURL URLWithString:@"sms:18656348970"]; 
    if ([[UIApplication sharedApplication] canOpenURL:url] == YES) { 
         
        [[UIApplication sharedApplication] openURL:url]; 
         
    }else{ 
     
        NSLog(@" "); 
    } 
 

좋은 웹페이지 즐겨찾기