ios - 使用自定义 NSURLProtocol 在 UIWebView 中通过 AJAX 调用加载文本文件失败

标签 ios uiwebview

我想使用 UiWebView 在 iOS 应用程序上显示网站。站点的某些组件(即使用 AJAX 调用加载的网络服务结果)应替换为本地数据。

考虑以下示例:

文本.txt:

foo

page1.html:

<html><head>
    <title>test</title>
    <script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<div id="target"></div>
<script type="text/javascript">
    function init(){
        $.get("text.txt",function(text){    
            $("#target").text(text);
        });
    }
    $(init);
</script>
</body></html>

View Controller :

@interface ViewController : UIViewController <UIWebViewDelegate>
    @property (nonatomic,assign) IBOutlet UIWebView *webview;
@end


@implementation ViewController
    @synthesize webview;
    //some stuff here
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [NSURLProtocol registerClass:[MyProtocol class]];
        NSString *url = @"http://remote-url/page1.html";
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
        [request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
        [webview loadRequest:request];
    }
@end

我的协议(protocol):

@interface MyProtocol : NSURLProtocol

@end

@implementation MyProtocol

+ (BOOL) canInitWithRequest:(NSURLRequest *)req{
    NSLog(@"%@",[[req URL] lastPathComponent]);
    return [[[req URL] lastPathComponent] caseInsensitiveCompare:@"text.txt"] == NSOrderedSame;
}

+ (NSURLRequest*) canonicalRequestForRequest:(NSURLRequest *)req{
    return req;
}

- (void) startLoading{

    NSLog(@"Request for: %@",self.request.URL);
    NSString *response_ns = @"bar";
    NSData *data = [response_ns dataUsingEncoding:NSASCIIStringEncoding];
    NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[self.request URL] MIMEType:@"text/plain" expectedContentLength:[data length] textEncodingName:nil];

    [[self client] URLProtocol: self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    [[self client] URLProtocol:self didLoadData:data];
    [[self client] URLProtocolDidFinishLoading:self];
    [response release];
}

- (void) stopLoading{
    NSLog(@"stopLoading");
}

@end

如果我不注册我的自定义 URLProtocol,页面将正确显示。如果我调用了 startLoading(),则会加载内容并随后触发 stopLoading()。但是在 UIWebView 上什么也没有发生。我尝试进行一些错误处理,但既没有抛出 JS AJAX 错误,也没有调用 UIWebViewDelegatedidFailLoadWithError

我尝试了另一种情况并创建了一个只加载图像的 HTML 页面:

<img src="image.png" />

并修改我的 URLProtocol 以仅处理图像的加载 - 这工作正常。也许这与 AJAX 调用有关?

你知道问题出在哪里吗?

提前致谢!

最佳答案

我遇到了同样的问题,经过几天的拉扯终于解决了:

您的问题来自您创建响应的方式,您必须创建状态 200 响应并在必要时强制 WebView 允许跨域请求:

NSDictionary *headers = @{@"Access-Control-Allow-Origin" : @"*", @"Access-Control-Allow-Headers" : @"Content-Type"};
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:request.URL statusCode:200 HTTPVersion:@"1.1" headerFields:headers];

您可以在我的回答中看到我完整的工作实现:

How to mock AJAX call with NSURLProtocol?

希望对您有所帮助, 文森特

关于ios - 使用自定义 NSURLProtocol 在 UIWebView 中通过 AJAX 调用加载文本文件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11794385/

相关文章:

iphone - UIPickerView 不显示数据

ios - 让我们缩短 cell.textLabel 的长度

ios - 我可以使用UIWebView(iOS)自动登录youtube吗?

objective-c - 类似于 Mobile Safari 的可滚动 UINavigationBar

objective-c - 当我使用基于 webview 的应用程序并且服务器在 iphone sdk 中失败时,如何找到 404 错误原因

ios - 定位自定义 MKAnnotationView

iOS:销毁viewDidDisappear中的 subview ?

ios - SwiftUI 中的后缘和前缘是什么意思?

ios - UIWebView iOS 8 Action 扩展在 iOS 8 移动 safari 中崩溃

javascript - 如何将实时数据发送到从 iOS 应用程序加载到 UIWebView 中的页面