微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么此Instagram oEmbed html不适合Swift WKWebView?

如何解决为什么此Instagram oEmbed html不适合Swift WKWebView?

我在使整个Instagram嵌入html呈现在此WKWebView中时遇到麻烦。 以下网址用于查询api:

/instagram_oembed?url=https://www.instagram.com/p/B4x4RtagRoH/?igshid=rmiahm71silp&maxwidth=658

Instagram的文档中描述了maxWidth:Instagram oEmbed Docs

注意:我正在使用的WKWebView的框架的宽度为394(转换为像素将为1182;这就是为什么我正在使用API​​的最大可能宽度值为658)。 另外,WKWebView已添加到容器UIView中,并限制在其边缘。

调用返回一个json对象,该对象带有一个由帖子组成的html键。它以<blockquote>开头,并且无法正确呈现,因此我将其包裹在<HTML><HEAD>标签中,如下所示:

func embed(html: String,baseURL: URL? = nil) {
    let htmlStart = "<HTML><HEAD><Meta name=\"viewport\" content=\"width=658,shrink-to-fit=yes\"></HEAD><BODY>"
    let htmlEnd = "</BODY></HTML>"
    let htmlString = "\(htmlStart)\(html)\(htmlEnd)"

    let script = """
                            var script = document.createElement('script');
                            script.src = 'https://platform.instagram.com/en_US/embeds.js';
                            document.head.appendChild(script);
                         """
            
    let userScript = WKUserScript(source: script,injectionTime: .atDocumentEnd,forMainFrameOnly: true)
            
    configuration.userContentController.addUserScript(userScript)
            
    debugPrint("htmlString: \(htmlString)")
    loadHTMLString(htmlString,baseURL: baseURL)
    
}

这是当前渲染的方式:

enter image description here

为什么html的内容不能正确调整高度?

我该怎么做才能将整个html放入WKWebView中?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。