我正在编写应用程序聊天,我想在用户从后台终止应用程序时注销。为此,我发送请求帖子从数据库中删除用户。但是,当我在applicationWillTerminate:(UIApplication *)application
这是我尝试的整个代码:
NSString *User_To_Be_Deleted = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
NSMutableURLRequest *DeleteRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.8/Chatiw/Logout_Delete.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
NSString *data_to_be_sent = [NSString stringWithFormat:@"nickname=walidoss"];
[DeleteRequest setHTTPMethod:@"POST"];
[DeleteRequest setHTTPBody:[data_to_be_sent dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *myconnection = [[NSURLConnection alloc] initWithRequest:DeleteRequest delegate:self];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"isLogin"];
当应用终止任何建议或帮助时,NSURLCONNECTION
无效。
答案 0 :(得分:0)
替换:
NSURLConnection *myconnection = [[NSURLConnection alloc] initWithRequest:DeleteRequest delegate:self];
为此:
NSURLConnection *myconnection = [[NSURLConnection alloc] initWithRequest:DeleteRequest delegate:self startImmediately:YES];
修改强>
试试这段代码:
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(!error){
//your code here on success
}
}] resume];
答案 1 :(得分:0)
我有同样的问题并找出解决方案。请执行以下操作。
NSURLResponse* response = nil;
NSData *data = [data_to_be_sent dataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnection sendSynchronousRequest:DeleteRequest returningResponse:&response error:nil];
希望它适合你。