2012-06-19 83 views
0

我正在寫一個iPhone應用程序,我需要以一定的時間間隔自動發送電子郵件和短信到一個號碼和電子郵件ID。但每次它在某個間隔後都會進入該部分,彈出式窗口會顯示電子郵件和短信,而當我按下發送按鈕時,則只會發送電子郵件或短信。自動發送電子郵件和短信iPhone,xcode

我怎樣才能讓它自動運行,這樣我就不必再一次按下發送按鈕,並自動執行操作。我發現這段代碼用於發送電子郵件和短信,並且我僅將這些代碼用於我的目的。我如何修改它以自動完成此操作。

// 
// MessageViewController.m 
// EmailExample 
// 
// Copyright http://iphoneapp-dev.blogspot.com. All rights reserved. 
// 

#import "MessageViewController.h" 


@implementation MessageViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.title = @"Message sending"; 
} 

- (IBAction)btnEmail_Clicked:(id)sender 
{ 
    MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; 
    controller.mailComposeDelegate = self; 
    [controller setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 
    [controller setSubject:@"iPhone Email Example Mail"]; 
    [controller setMessageBody:@"http://iphoneapp-dev.blogspot.com" isHTML:NO]; 
    [self presentModalViewController:controller animated:YES]; 
    [controller release]; 
} 

- (IBAction)btnMessage_Clicked:(id)sender 
{ 
    MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease]; 
    if([MFMessageComposeViewController canSendText]) 
    { 
     controller.body = @"Hello Friends this is sample text message."; 
     controller.recipients = [NSArray arrayWithObjects:@"+919999999999", nil]; 
     controller.messageComposeDelegate = self; 
     [self presentModalViewController:controller animated:YES]; 
    } 
} 

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result { 

    switch (result) { 
     case MessageComposeResultCancelled: 
      NSLog(@"Cancelled"); 
      break; 
     case MessageComposeResultFailed: 
      NSLog(@"Failed"); 
      break; 
     case MessageComposeResultSent: 
      NSLog(@"Send"); 
      break; 
     default: 
      break; 
    } 

    [self dismissModalViewControllerAnimated:YES]; 
} 

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 

    if (result == MFMailComposeResultSent) { 
     NSLog(@"It's away!"); 
    } 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (IBAction)btnCall_Clicked:(id)sender 
{ 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"+919999999999"]]; 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 
+0

你不能。您將獲得最接近的信息是將信息發送到REST Web服務,並且這僅在您的應用程序運行時才起作用。 – danielbeard

+0

可能的重複http://stackoverflow.com/questions/2827014/iphone-sdk-send-email-in-background-in-iphone-app –

回答

3

你不能,如果在可能情況下,你可以發送一些SMS到優質的服務和用戶可以具有高話費的結束。這就是爲什麼蘋果不允許這樣做。

如果您通過服務器發送了SMS /電子郵件,或者通過SMTP直接發送電子郵件,您可能會這樣做。

1
// Build request 
NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID]; 
NSURL *url = [NSURL URLWithString:urlString]; 


ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

[request setPostFormat:NSUTF8StringEncoding]; 
[request setPostValue:kFromNumber forKey:@"From"]; 
[request setPostValue:kToNumber forKey:@"To"]; 
[request setPostValue:kMessage forKey:@"Body"]; 

[request setRequestMethod:@"POST"]; 
[request setDelegate:self]; 
[request setDidFinishSelector:@selector(requestDone:)]; 
[request setDidFailSelector:@selector(requestFail:)]; 

hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
//hud.mode = MBProgressHUDModeAnnularDeterminate; 
hud.labelText = @"Please wait..."; 
hud.detailsLabelText = @"Sending message"; 
hud.delegate = self; 
hud.square = NO; 
hud.dimBackground = YES; 

[request startAsynchronous]; 

您可以使用此代碼並根據需要更改參數,我希望這可以幫助您發送短信。