2015-02-02 4 views
1

사용자가 응용 프로그램을 종료 할 때 서버에 로그 오프 정보를 보내려고합니다. "applicationWillTerminate".method에서 코드를 작성하지만 대리자가 호출되지 않고 로그 오프 할 수 없습니다. 아무도 설명해 주시겠습니까? 어떻게이 문제를 해결할 수 있습니까?강제로 응용 프로그램을 닫을 때 메서드를 수행 할 수 있습니다.

CODE : 우리는 다음과 같이 할 경우

- (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"); 
} 

답변

0

지금은 괜찮아요 알고 싶은 노력이 줄을

//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) 
} 
+0

후,이 시도는 미래에 어떤 문제가있다. – user3825276

+0

아니 아무것도 영향을 미치지 않습니다, 이것은 한 가지 유형의 사용법입니다 어떤 식 으로든 내 동생이 내 대답이 유용하다면 대답에 대한 표를 던지십시오 –

+0

감사합니다. 그렇습니다.하지만 나는 평판이 없습니다. ur 대답을 수락합니다. – user3825276