gpt4 book ai didi

ios - 应用程序终止时发送 HTTP POST

转载 作者:行者123 更新时间:2023-11-28 09:01:25 25 4
gpt4 key购买 nike

我正在编写一个应用程序聊天程序,我想在用户从后台终止应用程序时注销。为此,我发送请求帖子以从数据库中删除用户。但是,当我在 applicationWillTerminate:(UIApplication *)application

中使用它时,HTTP POST 没有发送

这是我试过的全部代码:

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 不工作,请提供任何建议或帮助。

最佳答案

替换:

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];

关于ios - 应用程序终止时发送 HTTP POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31939001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com