ios手机上的加载H5图片加载问题

705 阅读1分钟

同样的图片,安卓手机上可以正常展示,但是到ios手机上首次进入页面就不能正常显示图片,必须手动刷新一次页面才能正常加载。

我们网站访问强制切换https安全协议,而接口提供的图片资源时https协议的。于是就引发了上面的问题,因为http地址中,如果加载了https资源,浏览器将认为这是不安全的资源,将会默认阻止,这就会给你带来资源不全的问题了,比如:图片显示不了,样式加载不了,JS加载不了。修改为同一安全协议即可

ios端可以这样处理:

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {

NSURLProtectionSpace *protectionSpace = challenge.protectionSpace;

        NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;

        NSURLCredential *credential = nil;

        //处理HTTPS图片请求的,不受信任问题

        if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

            SecTrustRef serverTrust = protectionSpace.serverTrust;

            disposition = NSURLSessionAuthChallengeUseCredential;

            credential = [NSURLCredential credentialForTrust:serverTrust];
        }

        if(completionHandler){

            dispatch_async(dispatch_get_global_queue(0, 0), ^{

                completionHandler(disposition, credential);

            });
        }
}