2009-09-29 20 views
5

मुझे पता है कि मेल ऐप लॉन्च करके मेरे ऐप में एक ईमेल कैसे भेजना है और फिर मेरे ऐप पर लौट रहा हूं ... लेकिन मैं अपने ऐप को मेल ऐप खोलने के बिना ईमेल भेजने में सक्षम होना चाहता हूं। उदाहरण के लिए मेरे पास मेरे ऐप पर एक बटन होगा, उस बटन पर क्लिक करके एक ईमेल भेजा जाएगा। मैं तब उपयोगकर्ता को सूचित करूंगा कि ईमेल भेजा गया है ...आईफोन ऐप ईमेल भेजें

क्या कोई ऐसा कर रहा है?

धन्यवाद।

सामी

+0

आप प्राप्तकर्ताओं को कैसे चुनते हैं, या यह हार्ड-कोडेड है? – Tim

उत्तर

3

यह करने के लिए सबसे अच्छा तरीका है कि मेल भेजने करता है अपने ऐप के लिए वेब सर्वर है। आप ई-मेल के ब्योरे के साथ पास करेंगे और आपका सर्वर इसे उपयोगकर्ता की तरफ भेज देगा।

+0

यदि आप वेबसर्वर तक नहीं पहुंच पा रहे हैं तो आपको कोई समस्या है। फिर आपको वेबसर्वर को बाद में पुनः प्रयास करने के लिए संदेश कतार करना होगा। लेकिन आपका ऐप बाद में नहीं चल रहा है। यह वह जगह है जहां आईफोन को कुछ पृष्ठभूमि प्रसंस्करण के लिए अनुमति दी जाती है, सीमित, कहें, अगर स्क्रीन लॉक हो या कुछ मिनट के लिए कोई उपयोगकर्ता इंटरैक्शन नहीं हुआ है। – mahboudz

4

आपके पास कुछ विकल्प हैं। आप ऐप्पल की MFMailComposeViewController क्लास (नीचे देखें) का उपयोग कर सकते हैं जो आपको अपने ऐप में एक संदेश बनाने और मेल ऐप लॉन्च किए बिना या अपना छोड़ने के बिना आईफोन के मेल पर भेज सकता है। आप ई-मेल सीधे भेजने के लिए अपने ऐप में एसएमटीपी भी कार्यान्वित कर सकते हैं। आप अपने ईमेल को वेबसर्वर पर भी बंद कर सकते हैं और वेबसर्वर इसे भेज सकते हैं। सबसे आसान तरीका पहला तरीका है। दोष यह है कि आपको वास्तव में पता नहीं था कि संदेश भेजा गया था या नहीं, जो इस बात पर निर्भर करता है कि नेटवर्क परिचालन था या नहीं और अन्य कारक। बेशक, यदि आप अपने स्वयं के एसएमटीपी कोड के साथ जाते हैं, तो आपको नेटवर्क, या सर्वर अनुपलब्ध होने पर सभी कतारबद्ध और पुनः प्रयास करना होगा, और इसका मतलब है कि ऐसा करने के लिए आपके ऐप को चलाना होगा।

Apple's docs से

:

MFMailComposeViewController वर्ग एक मानक इंटरफ़ेस है कि संपादन और एक ई-मेल संदेश भेजने का प्रबंधन करता है प्रदान करता है। आप इस व्यू कंट्रोलर का उपयोग अपने एप्लिकेशन के अंदर एक मानक ईमेल व्यू प्रदर्शित करने के लिए कर सकते हैं और उस दृश्य के फ़ील्ड को प्रारंभिक मानों, जैसे कि विषय, ईमेल प्राप्तकर्ता, बॉडी टेक्स्ट और संलग्नक के साथ पॉप्युलेट कर सकते हैं। उपयोगकर्ता आपके द्वारा निर्दिष्ट प्रारंभिक सामग्री को संपादित कर सकता है और ईमेल भेजने या ऑपरेशन रद्द करने का चयन कर सकता है।

+0

धन्यवाद, मैं शायद मेल ऐप लॉन्च किए बिना पहले MFMailComposeViewController का उपयोग करने का प्रयास करूंगा ... – sami

9

यहां MFMailComposeViewController का उपयोग करके ईमेल भेजने के लिए एक नमूना कोड है। buildphases में

-(IBAction)showPicker:(id)sender { 
// This sample can run on devices running iPhone OS 2.0 or later 
// The MFMailComposeViewController class is only available in iPhone OS 3.0 or later. 
// So, we must verify the existence of the above class and provide a workaround for devices running 
// earlier versions of the iPhone OS. 
// We display an email composition interface if MFMailComposeViewController exists and the device can send emails. 
// We launch the Mail application on the device, otherwise. 

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 { 
// Displays an email composition interface inside the application. Populates all the Mail fields. 

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
picker.mailComposeDelegate = self; 

[picker setSubject:@"Hello from California!"]; 


// 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:@"rainy" ofType:@"png"]; 
NSData *myData = [NSData dataWithContentsOfFile:path]; 
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"]; 

// Fill out the email body text 
NSString *emailBody = @"It is raining in sunny California!"; 
[picker setMessageBody:emailBody isHTML:NO]; 

[self presentModalViewController:picker animated:YES]; 
[picker release]; 
} 


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of   the operation. 
message.hidden = NO; 
// Notifies users about errors associated with the interface 
switch (result) 
{ 
    case MFMailComposeResultCancelled: 
     message.text = @"Result: canceled"; 
     break; 
    case MFMailComposeResultSaved: 
     message.text = @"Result: saved"; 
     break; 
    case MFMailComposeResultSent: 
     message.text = @"Result: sent"; 
     break; 
    case MFMailComposeResultFailed: 
     message.text = @"Result: failed"; 
     break; 
    default: 
     message.text = @"Result: not sent"; 
     break; 
} 
[self dismissModalViewControllerAnimated:YES]; 
} 

-(void)launchMailAppOnDevice { 

// Launches the Mail application on the device. 
NSString *recipients = @"mailto:[email protected][email protected],[email protected]&subject=Hello from California!"; 
NSString *body = @"&body=It is raining in sunny California!"; 

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; 
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]]; 
} 
0

जोड़ें ढांचे MessageUI.framework

ViewController.h फ़ाइल

#import <MessageUI/MessageUI.h> 

    @interface ViewController() <MFMailComposeViewControllerDelegate> 

ViewController.m फ़ाइल

-(IBAction)emailButtonClicked:(id)sender{ 

     MFMailComposeViewController *mailComposer =[[MFMailComposeViewController alloc] init]; 
     if (mailComposer !=nil) { 
      mailComposer.mailComposeDelegate = self; 
      NSString *emailBody = @"Write the text here........"; 
      [mailComposer setMessageBody:emailBody isHTML:NO]; 
      [self presentModalViewController:mailComposer animated:YES]; 
     } 
     } 

     - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
      [self becomeFirstResponder]; 
      [self dismissModalViewControllerAnimated:YES]; 
     } 
संबंधित मुद्दे