UIWebView in UICollectionViewCell - html

I am using UIwebView inside UICollectionviewCell to display some dynamic html content coming from server. In my UICollectionViewCell subclass I am loading html content as following
let mainbundle = Bundle.main.bundlePath
let bundleURL = NSURL(fileURLWithPath: mainbundle)
webView.loadHTMLString(content, baseURL: bundleURL as URL)
Inside webViewDidFinishLoad() function
let fittingSize = webView.sizeThatFits(.zero)
webView.frame.size.height = fittingSize.height
But my UICollectionViewCell is not scaling itself according to the webView height and webView starts overlapping other views inside the cell. I tried almost every solution available over internet but none of them worked for me.
I also tried loading my html content in UITextView using NSAttributedString but it seems like it does not support all css properties.
Any help/hint would be appreciated. Thanks

Related

wkwebview - display image from documents directory into local html

I have an app with downloaded images to be displayed in local html. I am still using Objective-C because this app is 4 years old.
When first launch the app, it will download the images and other dynamic content (which I check, it is in Documents/content/ directory).
I have this code to get the content location:
NSString *contentBasePath = [app.contentManager contentBasePath];
which I get:
/var/mobile/Containers/Data/Application/4AFD30E2-F4F7-405A-9FE9-1857EEC11CC7/Documents/content
Then I have a few html pages that will call the downloaded images dynamically:
<div class="box-round benefits-thumbnail" style="background-image:url('file://{{../contentBasePath}}/{{filename}}');"></div>
Which I check, {{../contentBasePath}} will get:
/var/mobile/Containers/Data/Application/4AFD30E2-F4F7-405A-9FE9-1857EEC11CC7/Documents/content
and {{filename}} will get
example.jpg
which is all correct.
All this works with uiwebview. However I need to use wkwebview, the image did not show up.
I tried:
<div class="box-round benefits-thumbnail" style="background-image:url('{{../contentBasePath}}/{{filename}}');"></div>
and it still not working.
I googled and read around and it seems like wkwebview do not allow absolute path. so I tried as per this suggest:
[_webConfig.preferences setValue:#YES forKey:#"allowFileAccessFromFileURLs"];
and still not working.
How can I resolve this?
Seems like this is a limitation of the WebKit. As a workaround, you can write the HTML contents into a file in the documents directory and load it using [webView loadFileURL:fileUrl allowingReadAccessToURL:dirUrl]
Link to docs: https://developer.apple.com/documentation/webkit/wkwebview/1414973-loadfileurl
The files must be in the document directory.
I implemented the following to retrieve a document:
let documentDirUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileNameWithExtension = "IMG_0002.PNG"
let indexFileUrl = documentDirUrl.appendingPathComponent(fileNameWithExtension)
if FileManager.default.fileExists(atPath: indexFileUrl.path) {
webView.loadFileURL(indexFileUrl, allowingReadAccessTo: documentDirUrl)
}

WKWebView doesn't present the HTML string in Mac OSX

In new xCode 9.0:
I tried simply to load a HTML string inside webView (WKWebView) but I got a blank screen!
The strange thing is the same code is works in my other computer!
Is this a bug?
Here is the code
let filePath = Bundle.main.path(forResource: "my HTML file name", ofType: "html")
let htmlStr = try! NSString.init(contentsOfFile: filePath!, encoding: String.Encoding.utf8.rawValue)
webView.loadHTMLString(htmlStr as String, baseURL: nil)
You have to allow 'Outgoing Connections (Client)' in the project's capabilities. Make sure these are added to the entitlements file as well.
I know, it does not make any sense if you don't make any outgoing connections and just pure HTML, but this made it work for me.

Load Images from https URL within HTML on WebView

I am trying to load an HTML file on WebView. The HTML contains image URLs. When I try to run that image URL on Web Browser, it works. The image gets displayed. But the same image does not get loaded in WebView in my app.
I found this question on Apple Developer Forum, it is still unanswered. Any help would be appreciated. Thanks
I faced a similar issue a few months ago, and I fixed it by replacing UIWebView with WKWebView.
WKWebView is the official replacement of UIWebView, take a look at its documentation:
Starting in iOS 8.0 and OS X 10.10, use WKWebView to add web content
to your app. Do not use UIWebView or WebView.
Please try to fetch your images using WKWebView.
Step 1: import WebKit in your class
import WebKit
Step 2: Add WebKit delegate to your controller
class ViewController: UIViewController, WKUIDelegate
Step 3: create variable of WKWebView type
var webView: WKWebView!
Step 4: Configure WKWebView. Override loadView() function with following line of code
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
Step 5: In viewDidLoad() pass URLRequest to webView()
let url = "your URL"
let request = URLRequest(url: url!)
webView.load(request)

iOS WebView: open single hyperlink in external browser

There's a simple UIWebView in my iOS app. Hyperlinks open normally in my app itself. But there's a single hyperlink I want to open in external standard browser (Safari).
I image something like <a href="http://example.com" target="_safari">.
Is there anything like this?
I'm not able to make changes in the app itself. I have to do that on my website!
You can Use UIWebViewDelegate method for achieving that. Implement following method of the delegate
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let urlStr = request.url?.absoluteString
if //urlStr is string you want {
//launch external browser
return false
}
return true
}
This method will be called every time you try to load a url in web view. You can compare if its URL you want to load, if it matches then launch external browser and return false.
try this way
<a onclick="window.open('https://www.google.co.in/', '_system');" href="#">Google</a>

UIWebview - Text Cutting

I am using the UIWebview for displaying the content. But I got the text cutting issue in web view. UIWebview content size get changes dynamically based on the image size and content.But that Webview frame is constant,
How to avoid it? Please refer the attachment image.
THanks in advance
Try to use the delegate that indicates that the content is loaded completely then get the height by a javascript line of code, then adjust the frame of your uiwebview accordingly:
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *output = [webView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"];
[webView setFrame:CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.frame.size.width, [output intValue])];
}
Enjoy :)