0

I want to load HTML String in UICollectionview. Each collectionviewcell has WKWebview. When we try load more uicollectionviewcell apps get crashed without any error displayed. I further looking in memory tab and in memory page other process memory get growing every wkwebview loadhtmlstring is fired. How to remove WKWebview memory in swift4... Or other than WKWebview is there any control to show and edit HTML content in iOS? Please help me on this. Thanks in advance..

Note: I already tried this method suggested in stack overflow WKWebView causes my view controller to leak

Please give any other suggestion on this.

vinoth87
  • 200
  • 1
  • 2
  • 16
  • You can load HTML strings using `NSAttributedString` [For example](https://www.hackingwithswift.com/example-code/system/how-to-convert-html-to-an-nsattributedstring). – shim May 28 '21 at 15:21
  • Yes. But I want to display some .css style and .js file to load into HTML... So I thought NSAttributedString is not sufficient right? – vinoth87 May 28 '21 at 16:08
  • Well you could put the css styling inline. Not sure what JS you'd want, but putting that in a collection view cell seems like a bad idea to me. – shim May 28 '21 at 16:14
  • @shim WKWebview used like HTML content editor so my app need JS too... I used this library in my project https://github.com/cjwirth/RichEditorView.... In this library I changed all UIWebView to WKWebview... But loading WKWebview into collection view cells increased memory more when more collection view cells are loading... – vinoth87 May 29 '21 at 07:12

2 Answers2

0

You can cache the response of the HTML page and after the cell gets disappear or not in view you can remove those cells from view and again when you view that cell you can pick cell info from cache, so that you can save memory.d

msusare
  • 473
  • 2
  • 7
0

I got the same work for do. you can use this below mentioned code.

First you just need to "import WebKit" framework.

class SpecificLessonDetailCVC: UICollectionViewCell {
    //MARK:- IBOUTLET & PROPERTIES
    @IBOutlet weak var containerView: UIView!
    let webView = WKWebView()
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt  indexPath: IndexPath) -> UICollectionViewCell {

guard let cell = collVwLesson.dequeueReusableCell(withReuseIdentifier: "SpecificLessonDetailCVC", for: indexPath) as? SpecificLessonDetailCVC else { return UICollectionViewCell()}
        
        cell.webView.scrollView.showsHorizontalScrollIndicator = false
        cell.webView.scrollView.showsVerticalScrollIndicator = false
        cell.webView.scrollView.indicatorStyle = .white
        cell.webView.scrollView.delegate = self
        cell.webView.navigationDelegate = self
        cell.webView.loadHTMLStringWithMagic(content: objLessonDetailsVM.responseModel?.data.data[indexPath.row].dataDescription ?? "", baseURL: nil)
        cell.webView.frame = CGRect(x: 0, y: 0, width: cell.vwLesson.frame.width, height: cell.vwLesson.frame.height)
        cell.vwLesson.addSubview(cell.webView)
        
        return cell

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if let cell = cell as? SpecificLessonDetailCVC {
            cell.webView.becomeFirstResponder()
        }
     }
V.Kumar
  • 139
  • 2
  • 5