簡體   English   中英

強制關閉應用程序時如何執行方法

[英]How can I execute a method when I force close an application

我想在用戶強制退出應用程序時將注銷詳細信息發送到服務器。 我在“ applicationWillTerminate”中編寫代碼。方法正在執行,但未調用委托,並且我無法注銷。 誰能解釋一下我該如何解決這個問題?

碼:

- (void)applicationWillTerminate:(UIApplication *)application {

    [self sendingLogoffDatatoServer];
}

- (void)sendingLogoffDatatoServer
{
    [self.connection cancel];

 //intialize new mutable Data
 NSMutableData *data = [[NSMutableData alloc]init];
 self.receiveData = data;

 //initialize URL that is Going to be Fetched
 NSURL * url = [NSURL URLWithString:@"http://service/logout"];

 //initialize a request from URL
 NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];

//set HTTP Method
[request setHTTPMethod:@"POST"];

//initalize a post data


NSString *postData = [NSString stringWithFormat:@"email=%@",emailString];
NSLog(@"%@",postData);
//set Request Content Type
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

//set POST Data of Request
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];

//initialize a Connection from Request
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
self.connection = conn;

//strat the Connection
[conn start];
NSLog(@"Connection Started");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  [self.receiveData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
 if(error)
{
    UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"TITLE" message:@"Couldn't Connect to the Server. Please Try Again."  delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [errorAlert show];
 }
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  NSLog(@"Posted");
}

在這行之后嘗試一下

//initialize a Connection from Request
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
self.connection = conn;

//strat the Connection
[conn start];
NSLog(@"Connection Started");

[NSThread sleepForTimeInterval:2.0];  // call the sleeper time interval


-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
 NSLog(@"Posted");
 [[NSThread mainThread] exit];   // call the thread break or use exit (0)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM