【问题标题】:iOS: Open UIWebView with local javascript filesiOS:使用本地 javascript 文件打开 UIWebView
【发布时间】:2013-06-22 15:00:23
【问题描述】:

我想在UIWebView 中打开一个网站,但我不想从应用程序的 Documents 文件夹中加载 javascript 文件(因为带宽问题)。这可能吗?

【问题讨论】:

    标签: javascript ios objective-c uiwebview


    【解决方案1】:

    是的,您需要创建一个自定义 NSURLProtocol,如这篇文章中所示:https://stackoverflow.com/a/5573155/244160。在canInitWithRequest: 中进行适当的检查,并根据示例提供具有正确内容类型的 Javascript。

    更新:

    这里是一个示例实现的快速截图:

    @interface LocalJSURLProtocol : NSURLProtocol    
    @end
    
    @implementation LocalJSURLProtocol
    
    + (BOOL)canInitWithRequest:(NSURLRequest *)request
    {
    
        return [request.URL.scheme caseInsensitiveCompare:@"http"] == NSOrderedSame && [request.URL.lastPathComponent hasSuffix:@"js"]);
    }
    
    + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
    {
        return request;
    }
    
    - (void)startLoading
    {
        NSURLRequest *request = self.request;
    
        NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[request URL]
                                                            MIMEType:@"text/javascript"
                                               expectedContentLength:-1
                                                    textEncodingName:nil];
    
        NSString *localFilePath = [[NSBundle mainBundle] pathForResource:@"sample.js" ofType:nil];
        NSData *data = [NSData dataWithContentsOfFile:localFilePath];
    
        [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
        [self.client URLProtocol:self didLoadData:data];
        [self.client URLProtocolDidFinishLoading:self];
    
    }
    
    - (void)stopLoading
    {
    }
    
    @end
    

    然后您在开始加载之前注册这样的协议 [NSURLProtocol registerClass:[LocalJSURLProtocol class]];。这将在您的 UIWebView 中拦截请求,您有机会为请求文件注入自己的 Javascript 代码。

    【讨论】:

    • 这非常有效。我使用[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: jsFile] 来获取localFilePath。我将js 文件添加到“复制捆绑资源”中,它们不是自动添加的。谢谢!
    【解决方案2】:

    (请参阅下面我的编辑 - 通过使用自定义协议,可能可以使用本地资产和远程 html 文件)

    不能在 Internet 文件上使用本地 js 文件(或任何本地文件)。这类似于您无法从常规桌面浏览器打开网站上的本地 javascript 文件。

    您可以做的是调用您网站的页面,将响应的 html 保存为本地 html 文件(在您的文档文件夹中),并将 js url 也更改为本地。 url 应该是相对的。 例如:

    documents
    - myapp
    -- index.html
    -- scripts.js
    

    在 index.html 中你可以将 js src 更改为:

    <script src="scripts.js" />
    
    • cmets:
      1. 我假设您可以访问和编辑该网页。
      2. 如果没有下载本地 js 文件,您可以做一个很好的后备。类似于 jQuery 的 cdn 回退到本地文件,我们可以做相反的事情并回退到服务器的文件(jQuery 只是一个例子。它可以通过测试命名空间的存在来处理任何 js 文件:
        <script src="jquery-2.0.0.min.js"></script>
    <script>
    if (typeof jQuery == 'undefined') {
        document.write(unescape("%3Cscript src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min.js'
    

    type='text/javascript'%3E%3C/script%3E")); }

    希望有帮助!

    编辑

    查看此post 后,您可能能够从远程 html 文件访问本地文件(在他的示例中,他正在使用本地 html 文件,但它也可以在远程打开时使用)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 2017-06-05
      • 2018-03-19
      • 1970-01-01
      • 2012-08-15
      • 2011-04-04
      相关资源
      最近更新 更多