【问题标题】:UIWebView Trigger Load more with Ajax websiteUIWebView 触发器加载更多与 Ajax 网站
【发布时间】:2015-01-30 05:35:52
【问题描述】:

我正在制作一个数据来自网站的 iOS 应用程序。我正在弄清楚当用户将 tableView 滚动到底部时如何加载更多数据。

注意:我创建的 UIWebView 是在后台,所以我可以通过这种 hack 方式获取 webView 显示的内容,但是用户只能交互的是 UITableView。

因为我的 APP 需要的数据来自网站(Ajax)。我使用这种方法来获取网站的内容。是的,我在 webView:shouldStartLoadWithRequest 委托方法中使用 UIWebView 完成了此操作。

NSString *htmlString = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];

这很完美,我得到了内容。

但是正如我之前所说的那样,当用户滚动到底部时,tableView 需要加载更多数据。所以,我的UIWebView 也需要加载更多的内容,以便从内容中解析更多的数据,这样tableView 才能继续运行。 注意:我无法控制服务器上的页面,所以我无法获取 API。 (可以跨域请求吗?)

我想过这两种方法:

方法1.模拟向上滑动手势触发加载更多(当webView滚动到底部时,会自动触发加载更多动作)

方法 2. 注入 javascript 告诉UIWebView 加载更多数据。

我在 Chrome 中使用了开发者工具来捕获加载更多请求。 这是我得到的。

Remote Address:54.165.215.138:443
Request URL:https://color.adobe.com/api/v2/themes?filter=public&startIndex=144&maxNumber=36&sort=like_count&time=week
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, sdch
accept-language:zh
accept-version:2.0.0
Cache-Control:max-age=0
Connection:keep-alive
Content-Type:application/json
Cookie:AMCV_9E1005A551ED61CA0A490D45@AdobeOrg=T; international=cn; storeregion=cn; __lc.visitor_id.1031448=S1421040023.3b40036efc; 57472748-VID=111520816012303; sfdc_session=-; mbox=PC#1422579254596-83420.19_07#1423794285|session#1422583820268-575696#1422586545|check#true#1422584745; kuler-loc=zh; cookies-enabled=1; s_pers=%20s_fid%3D35A9926601E31AD1-1D7E51664BFBD9C5%7C1485743101094%3B%20s_vs%3D1%7C1422586501096%3B%20gpv%3Dcolor.adobe.com%2520%253A%2520Explore%2520%253A%2520Most%2520Popular%2520%253A%2520Week%7C1422586501098%3B%20s_nr%3D1422584701102-Repeat%7C1454120701102%3B; s_sess=%20s_ppv%3D-%252C100%252C18%252C8054%3B%20s_cpc%3D0%3B%20s_cc%3Dtrue%3B%20s_sq%3D%3B; s_vi=[CS]v1|2A3C2EE20548B5C7-40000105000065EB[CE]; aam_uuid=33030698550757492143013270951098470183
Host:color.adobe.com
If-Modified-Since:Sat, 1 Jan 2000 00:00:00 GMT
Referer:https://color.adobe.com/zh/explore/most-popular/?time=week
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36
x-api-key:7810788A1CFDC3A717C58F96BC4DD8B4
X-NewRelic-ID:UQAPV1JRGwEGV1RVBgQ=
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
filter:public
startIndex:144
maxNumber:36
sort:like_count
time:week
Response Headersview source
Access-Control-Allow-Headers:Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Content-Language, Date, Api-Version, Response-Time, X-API-Key
Access-Control-Allow-Methods:GET, POST
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:Api-Version, Request-Id, Response-Time, X-API-Key
Api-Version:2.0.0
Cache-Control:no-cache, must-revalidate
Connection:keep-alive
Content-Encoding:gzip
Content-Language:zh
Content-MD5:Hbz5uI3q/P2aEQ0+0TfZgg==
Content-Security-Policy:default-src 'self'; img-src 'self' *.behance.net *.s2stagehance.com data: *.typekit.net *.adobe.com *.newrelic.com *.nr-data.net; font-src 'self' data: *.typekit.net; style-src 'self' 'unsafe-inline' *.typekit.net *.adobe.com adobe.com; script-src 'self' 'unsafe-inline' 'unsafe-eval' *.services.adobe.com *.adobe.com *.adobetag.com *.typekit.net *.tt.omtrdc.net adobe.demdex.net *.newrelic.com *.adobelogin.com *.behance.net net.s2stagehance.com *.adobesunbreak.com; frame-src 'self' *.services.adobe.com *.adobelogin.com *.adobe.com adobe.com *.adobeku.com *.facebook.com *.adobe.demdex.net adobe.demdex.net; report-uri /api/v2/csp-reports; connect-src 'self' accounts.adobe.com www-stage.adobesunbreak.com *.services.adobe.com;
Content-Type:application/json
Date:Fri, 30 Jan 2015 02:25:26 GMT
Expires:-1
Request-Id:4043bed0-a827-11e4-98a0-81fb44ac31c8
Response-Time:20
Server:kuler-prod
Strict-Transport-Security:max-age=31104000
transfer-encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1; mode=block

所以我想知道有没有办法在 UIWebView 中触发 Load More。有什么黑客方法可以做到这一点吗?如果我的问题不清楚,请随时提问。谢谢。

【问题讨论】:

  • datum 的复数形式是data,不是datas。数据可以接受单数和复数形式。

标签: ios ajax uiwebview


【解决方案1】:

在网页的 HTML 中确定当您滚动到底部时调用的 javascript 函数,然后只需使用 stringByEvaluatingJavaScriptFromString: 调用该函数,此时 UIWebView 将触发该函数。然而,不利的一面是,当 AJAX 加载新内容时,您不会收到任何通知,因此您必须为此想出另一种解决方法。

另外,你提到:

我无法控制服务器上的页面

我读为:

如果页面更改,我的应用程序可能会崩溃

为什么要围绕可能发生变化的网页构建应用,而不是直接与文档化的 API 通信?

【讨论】:

    【解决方案2】:

    这完全取决于网页如何加载更多内容(即 JS 调用是什么以及他们如何知道何时调用它)。但总的来说,让您的应用程序依赖于您无法控制的某些网站内容并不是一个好主意。如果他们更改格式,您的应用将中断,您需要提交应用更新。

    如果您绝对需要来自本网站的数据,那么我将创建您自己的包装网络服务,该服务接受您需要的参数(即页码)并返回您想要的数据。这样做的好处是:

    1. 如果网站更改格式,您无需重新提交应用即可快速部署服务更新。
    2. 即使网站更改了格式,您的旧版本应用也将继续运行。

    【讨论】:

      猜你喜欢
      • 2013-02-08
      • 2015-12-04
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多