Search code examples
iosswifttwitterembedwkwebview

Embedded Tweet doesn't load completely sometimes in WKWebView


I'm trying to load some embedded tweets in WKWebViews in Swift, on iOS. I get the html content with Twitter oembed API (https://developer.twitter.com/en/docs/twitter-for-websites/embedded-tweets/overview.html), since the native Twitter SDK is not supported anymore. Sometimes, the tweet doesn't load completely, and sometimes it does :

two muppets As you can see on this screenshot, the first tweet doesn't load images, buttons, fonts... but the second one does load successfully.

I have tried to load the tweets one by one in a queue, as I thought the simultaneous loading of all the tweets at once was the problem... but with no success unfortunately.

I get the tweet HTML like this :

func getEmbeddedTweet(url: String, completion: @escaping (_ html: String?) -> Void) -> Void {
        let twitterUrl = "https://publish.twitter.com/oembed?hide_thread=true&url="+url
        Alamofire.request(twitterUrl).responseJSON { response in
            switch response.result {
            case .success(let JSON):
                if let response = JSON as? NSDictionary, let html = response.object(forKey: "html") as? String {
                    completion(html)
                }
            case .failure(_):
                completion(nil)
            }
        }
    }

And I load it simply like this :

self.webView!.loadHTMLString(html, baseURL: nil)

I would like that every tweets load properly. Does someone already encounter this issue before ? Thanks !


Solution

  • The problem was Twitter-side...

    They fixed it some days ago : https://twittercommunity.com/t/embedded-tweet-doesnt-load-completely-sometimes-in-wkwebview/126169 https://twittercommunity.com/t/twttr-widgets-createtweet-is-not-able-to-load-tweets-more-than-once-due-to-cache-issue/126056/11