开发者

Show waiting view when sending mail using smtp connection ios?

I am working on a app in which I am sending mail using mail-core engine. I have created my own viewController to send mail. I want to show a waiting view when mail sending is in process. My waiting view always displays after mail sending has been done. Is it so开发者_如何学编程me kind of threading issue?

Here is the code which I'm using to send mail.

- (IBAction) sendTapped:(id) sender {

[txtfSubject resignFirstResponder];
[txtfReceptient resignFirstResponder];
[txtvMessageBody resignFirstResponder];

[self setTo:txtfReceptient.text];
[self setFrom:username];
[self setSubject:txtfSubject.text];
[self setBody:txtvMessageBody.text];

[self performSelector:@selector(prepareAndSendMail) withObject:nil afterDelay:0.34];
}


- (void) prepareAndSendMail {


[WNAppDelegate performSelectorOnMainThread:@selector(showWaitingView) withObject:nil waitUntilDone:NO];

//TODO: send mail here
CTCoreMessage *msg = [[CTCoreMessage alloc] init];
[msg setTo:[myMessage to]];
[msg setFrom:[myMessage from]];

//Encode message here
NSString *encodedMessage = nil;

@try {
    encodedMessage = [self encodeMessage:txtvMessageBody.text];
}
@catch (NSException * e) {
    NSLog(@"An exception occurred while encoding message");
}
@finally {
    if(encodedMessage){
        [msg setBody:encodedMessage];
    }
}

[msg setSubject:[myMessage subject]];

BOOL success =  [self sendMailOnAnotherThread:msg];

[msg release];

[WNAppDelegate performSelectorOnMainThread:@selector(removeWaitingView) withObject:nil waitUntilDone:NO];
//[appDelegate removeWaitingView];
if(!success) {
    UIAlertView * empty_alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                           message:@"Could not send." 
                                                          delegate:nil 
                                                 cancelButtonTitle:@"OK" 
                                                 otherButtonTitles:nil];
    [empty_alert show];
    [empty_alert autorelease];
    return;
}   
else {
    //Message sent successfully
    if(self.target && [self.target respondsToSelector:@selector(messageSentSuccessfully)]){
        [self.target messageSentSuccessfully];
    }

    WN_POST_NOTIFICATION(kMessageSentSuccessfully,nil);
}

[self dismissModalViewControllerAnimated:YES];  
 }

- (BOOL) sendMailOnAnotherThread:(CTCoreMessage*)message {


BOOL success = YES;

BOOL auth = YES;
BOOL tls = YES;

@try {
    [CTSMTPConnection sendMessage:message server:GMAIL_SERVER  username:username
                         password:password port:GMAIL_PORT_Number useTLS:tls useAuth:auth];
}
@catch (NSException * e) {
    //Msg failed to send;
    success = FALSE;
}
return success;     
}


Ok guys,Thanks for all the info you have provided. The problem is now solved.

I'M posting here my code in case some one need it.

- (IBAction) sendTapped:(id) sender {

[txtfSubject resignFirstResponder];
[txtfReceptient resignFirstResponder];
[txtvMessageBody resignFirstResponder];

[self setTo:txtfReceptient.text];
[self setFrom:username];
[self setSubject:txtfSubject.text];
[self setBody:txtvMessageBody.text];

[self performSelector:@selector(prepareAndSendMail) withObject:nil afterDelay:0.34];
}


- (void) prepareAndSendMail {

//[((WalnutAppDelegate*)WNAppDelegate) performSelectorOnMainThread:@selector(showWaitingView) withObject:nil waitUntilDone:NO];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSThread *aNewThread = [[[NSThread alloc] initWithTarget:((WalnutAppDelegate*)WNAppDelegate) selector:@selector(showWaitingView) object:nil] autorelease];
[aNewThread start];


//[NSThread detachNewThreadSelector: toTarget:((WalnutAppDelegate*)WNAppDelegate) withObject:nil];

//TODO: send mail here
CTCoreMessage *msg = [[CTCoreMessage alloc] init];
[msg setTo:[myMessage to]];
[msg setFrom:[myMessage from]];

//Encode message here
NSString *encodedMessage = nil;

@try {
    encodedMessage = [self encodeMessage:txtvMessageBody.text];
}
@catch (NSException * e) {
    NSLog(@"An exception occurred while encoding message");
}
@finally {
    if(encodedMessage){
        [msg setBody:encodedMessage];
    }
}

[msg setSubject:[myMessage subject]];

BOOL success =  [self sendMailOnAnotherThread:msg];

[msg release];

//[NSThread detachNewThreadSelector:@selector(removeWaitingView) toTarget:((WalnutAppDelegate*)WNAppDelegate) withObject:nil];

[((WalnutAppDelegate*)WNAppDelegate) performSelectorOnMainThread:@selector(removeWaitingView) withObject:nil waitUntilDone:NO];
[pool drain];


if(!success) {
    UIAlertView * empty_alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                           message:@"Could not send." 
                                                          delegate:nil 
                                                 cancelButtonTitle:@"OK" 
                                                 otherButtonTitles:nil];
    [empty_alert show];
    [empty_alert autorelease];
    return;
}   
else {
    //Message sent successfully
    if(self.target && [self.target respondsToSelector:@selector(messageSentSuccessfully)]){
        [self.target messageSentSuccessfully];
    }

    WN_POST_NOTIFICATION(kMessageSentSuccessfully,nil);
}

[self dismissModalViewControllerAnimated:YES];  
}

- (BOOL) sendMailOnAnotherThread:(CTCoreMessage*)message {


BOOL success = YES;

BOOL auth = YES;
BOOL tls = YES;

@try {
    [CTSMTPConnection sendMessage:message server:GMAIL_SERVER  username:username
                         password:password port:GMAIL_PORT_Number useTLS:tls useAuth:auth];
}
@catch (NSException * e) {
    //Msg failed to send;
    success = FALSE;
}
return success;     
}

- (void)showWaitingView {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

CGRect frame = CGRectMake(90, 190, 32, 32);
UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[progressInd startAnimating];
progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;

frame = CGRectMake(130, 193, 140, 30);
UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
waitingLable.text = @"Processing...";
waitingLable.textColor = [UIColor whiteColor];
waitingLable.font = [UIFont systemFontOfSize:20];;
waitingLable.backgroundColor = [UIColor clearColor];
frame = [[UIScreen mainScreen] applicationFrame];
UIView *theView = [[UIView alloc] initWithFrame:frame];
theView.backgroundColor = [UIColor blackColor];
theView.alpha = 0.7;
theView.tag = 999;
[theView addSubview:progressInd];
[theView addSubview:waitingLable];

[progressInd release];
[waitingLable release];

[window addSubview:[theView autorelease]];
[window bringSubviewToFront:theView];
[pool drain];
}

- (void)removeWaitingView {
UIView *v = [window viewWithTag:999];
if(v) [v removeFromSuperview];

}


Yes it is. You need to return to run loop in order to get UI updated. So it is best to display the waiting view in main thread, send mail in background thread and then again hide and remove waiting view in main thred. You should only update UI from main thread. You can use performSelectorInBackground and performSelectorOnMainThread to do it the easy way without creating threads manually. You can also use dispatch_async like this:

//show waiting view
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //prepare mail here

    dispatch_async(dispatch_get_main_queue(), ^{
        //send mail
        //hide waiting view
    });
});


Since you are executing prepareAndSendMail on the main thread, WNAppDelegate performSelectorOnMainThread:@selector(showWaitingView) withObject:nil waitUntilDone:NO]; will call showWaitingView after the current run loop has ended by which time you will have sent the mail. Setting waitUntilDone: to YES will show the waiting view at the time you intend it to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜