简体   繁体   English

iPhone上的NSOperation,同步NSURLConnection和UIWebView

[英]NSOperation, Synchronous NSURLConnection, & UIWebView on iPhone

I have a weird issue. 我有一个奇怪的问题。 Here is the setup: 这是设置:

  • I have a NSOperationQueue which I add NSOperation subclass objects to. 我有一个NSOperationQueue,我向其中添加了NSOperation子类对象。 These do networking using NSURLConnection sendSynchronousRequest method. 这些使用NSURLConnection sendSynchronousRequest方法进行联网。 As I understand it, these are completely asynchronous, since they are NSOperations which each get their own thread. 据我了解,它们是完全异步的,因为它们是NSOperation,每个都有各自的线程。
  • If I launch my app WITHOUT any background networking operations, loading a URL into a UIWebView works just fine. 如果我在没有任何后台联网操作的情况下启动我的应用程序,则将URL加载到UIWebView中就可以了。
  • If I launch my app, and run some background networking operations, loading a URL into a UIWebView results in this error: 如果启动我的应用程序并运行一些后台联网操作,则将URL加载到UIWebView中会导致以下错误:

    Error Domain=WebKitErrorDomain Code=102 UserInfo=0x1996d60 "Frame load interrupted" 错误域= WebKitErrorDomain代码= 102 UserInfo = 0x1996d60“帧加载中断”

and any subesequent attempts to load a UIWebView fails with the same error. 并且任何随后尝试加载UIWebView的尝试都会失败,并显示相同的错误。

Is there some weird interaction between UIWebView and NSURLConnection that I need to be aware of? 我需要注意UIWebView和NSURLConnection之间是否存在一些奇怪的交互? My next step is to replace the synchronous calls with a fully async NSOperation, but it is such a weird bug that I figured I would ask the experts. 我的下一步是用完全异步的NSOperation替换同步调用,但这是一个怪异的错误,我以为我会问专家。

I have isolated the error down to the NSURLConnection sendSynchronousRequest method. 我已将错误隔离到NSURLConnection sendSynchronousRequest方法。 If I call this from an NSOperation, UIWebView breaks horribly. 如果我从NSOperation调用此方法,则UIWebView会严重中断。

The 102 error has to do with some "Policy Changed" error in WebKit, which is poorly/non-documented anywhere. 102错误与WebKit中的某些“策略更改”错误有关,该错误在任何地方都很少/没有记录。

Anybody have any idea? 有人知道吗 Any help is greatly appreciated! 任何帮助是极大的赞赏!

EDIT: Looks like this is a cookie issue of some kind. 编辑:看起来这是某种cookie问题。 Any reason why UIWebView would fail to load if the web server is setting a cookie via a background HTTP Request? 如果Web服务器通过后台HTTP请求设置Cookie,为什么UIWebView无法加载的任何原因?

OK, I have figured this out. 好,我知道了。

Apparently the UIWebView does not like URLConnection HTTPRequests (or even ASIHTTPRequest) using the global cookie store until it does. 显然,UIWebView不喜欢使用全局cookie存储的URLConnection HTTPRequests(甚至ASIHTTPRequest)。 So now my app throws up a simple HTML page to allow the cookie to be set by the UIWebView, and everyone "downstream" from that is an apparently happy camper, both API requests via NSOperation, and UIWebViews. 因此,现在我的应用程序抛出一个简单的HTML页面,以允许由UIWebView设置cookie,并且从此“下游”的每个人显然都很高兴,无论是通过NSOperation的API请求还是UIWebViews。

Hooray! 万岁!

I'm not clear on what you are trying to accomplish here, a UIWebView will load an NSRequest asynchronously on its own. 我不清楚您要在这里完成什么,UIWebView将自行异步加载NSRequest。 One thing to check is to make sure any interaction you are doing in code with the UIWebView is on the main thread by using one of the performSelectorOnMainThread: methods from within your NSOperation. 要检查的一件事是通过使用NSOperation内部的performSelectorOnMainThread:方法之一,确保与UIWebView在代码中进行的任何交互都在主线程上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM