我们正在开发一个 cordova ios 应用程序,我们最近安装了 xcode 8-beta 以查看我们的应用程序如何在 ios 10 下运行。
到目前为止,我们只注意到一个问题:未加载 svg 图像。这些图像使用 css 设置为各种元素的背景,它们在 9.3.2 及更低版本下都可以正常工作。
最佳答案
如果您的cordova 应用程序使用实现 URLProtocol
的插件,例如phonegap contentsync plugin,您可能会发现SVG 响应 header 的MIME 类型存在错误。
在 iOS 10 之前,MIME 类型是自动设置的,这可能是 API 的一项功能(尚未确认),但现在似乎不再如此。您可以通过在 Content-Type
期间显式设置响应的 MIME 类型 ( [URLProtocol startloading]
) 来解决此问题:
(改编自 phonegap contentsync 和这个 MIME Check )
- (void)startLoading {
NSData *data = [NSData dataWithContentsOfFile:self.request.URL.path];
NSString *fileExtension = self.request.URL.pathExtension;
NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExtension, NULL);
NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);
// add the MIME Type to the existing header.
NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithDictionary:self.request.allHTTPHeaderFields];
headers[@"Content-Type"] = contentType;
// create a response using the request and our new HEADERs
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL
statusCode:200
HTTPVersion:@"1.1"
headerFields:headers];
[self.client URLProtocol:self didReceiveResponse:response
cacheStoragePolicy:NSURLCacheStorageAllowedInMemoryOnly];
[self.client URLProtocol:self didLoadData:data];
[self.client URLProtocolDidFinishLoading:self];
}
关于cordova - iOS 10 测试版上的 UIWebView : not loading any svg images,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38318411/