gpt4 book ai didi

ios - 为什么 UIWebView 不在 applicationWillTerminate : in my app delegate? 加载 NSURLRequest

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

我正在尝试在应用程序即将终止/进入后台时进行清理。

但是,当下面的代码被调用时,

- (void)applicationWillTerminate:(UIApplication *)application
{
NSURL* url = [NSURL URLWithString:@"http://www.mysite.com/mobile/home?sysmethod=proclogout&systype=mobile&pagetype=jquerymobile"];
[self.webView loadRequest:[[NSURLRequest alloc] initWithURL:logoutURL]];
}

...请求开始加载,但从未完成。我在 Apple 文档中读到,一旦 applicationWillTerminate: 被调用,应用程序有大约 5 秒的时间来完成他们正在做的事情。

我不相信这个过程会花费那么长时间,因为我在应用程序的其他地方加载了同样的请求,而且它总是在几毫秒内发生。

我已经尝试过同步和异步,但都没有成功两种方式

感谢您的任何想法!

最佳答案

UIWebView 中的请求加载是异步的。这解释了为什么请求开始加载但从未完成。如果您想确保某个 URL 被调用或加载,您将不得不以某种方式防止 applicationWillTerminate: 方法退出。如果您不需要真正在 Web View 中加载请求,您可以使用 [NSURLConnection sendSynchronousRequest:returningResponse:error:] 代替。

此外,一种 hacky 的方式可能是这样的:

[_webview stringByEvaluatingJavaScriptFromString:@"window.location.href=\"http://example.com/logout\""];

stringByEvaluatingJavaScriptFromString: 是一种阻塞的同步方法。但是,我还没有尝试过它是否真的只在请求完全加载后才返回。它可能在设置位置后立即返回。

关于ios - 为什么 UIWebView 不在 applicationWillTerminate : in my app delegate? 加载 NSURLRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20712117/

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