UIWebView加载进度并调整网页以适应视图页面?
我正在使用UIWebView加载网页。
I am using UIWebView to load a web page.
有3个问题:
1。它可以跟踪UIWebView加载页面时的百分比进度吗?
1.It it possible to track the percentage progress when UIWebView is loading the page?
2.我注意到当Safari加载网页时,URL文本字段显示蓝色背景进度指示器告诉用户加载网页的百分比。这是什么技术?
2.I noticed that when Safari loading a web page, the URL textfield displays a blue background progress indicator to tell user the percentage of loading a web page. What is the technology for this?
3.我知道有属性scalesPageToFit
3.I know there is property scalesPageToFit
scalesPageToFit
确定网页是否为布尔值缩放以适应视图,用户可以更改比例。
scalesPageToFit A Boolean value determining whether the webpage scales to fit the view and the user can change the scale.
我尝试将其设置为YES,但看起来它不在公共API和我的应用程序中黑屏停止,我不确定是什么问题?
I try to set it to YES, but it looks like that it is not in public API and my app stopped with black screen, I am not sure what is wrong?
回答#1)
您可以使用NSURLConnection将网页作为NSData对象拉出,而不是使用UIWebView。当你收到来自你的请求的初始回复
Instead of using a UIWebView, you can pull the webpage down as an NSData object using an NSURLConnection. When you get the initial response from your request from
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
网络服务器应返回预期内容大小的值(应包含在响应)。然后,每次收到数据时,您将继续调用以下方法:
the webserver should return a value of "expected content size" (which should be included in the response). Then you will keep getting the following method called each time you receive data:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
继续将数据附加到现有的NSMutableData对象。然后,您可以根据预期的响应大小检查当前数据对象的大小(NSMutableData.length)。
Keep appending the data to an existing NSMutableData object. Then you can check the size of your current data object (NSMutableData.length) against the expected response size.
percentage = (myData.length*100)/theResponse.expectedContentSize;
然后您可以使用该百分比更新进度条!当
Then you can update a progress bar with that percentage! When
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
运行,使用您的数据来调用
runs, use your data to call
[myWebView loadData:myData MIMEType:myMimeType textEncodingName:myEncoding baseURL:baseURL];
它会将您下载的所有内容加载到网络视图中。
and it will load everything you pulled down into your web view.