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)
Related
Need to show HTML pages within app using WebView in WebKit framework.
This code should do it, but only get a blank white screen, no error messages. Thanks for any help of alternate solutions.
#property (weak) IBOutlet WKWebView *webView;
NSURL * myURL = [NSURL URLWithString:#"http://www.jzmobile.com"];
[_webView loadFileURL:myURL allowingReadAccessToURL:myURL];
[_webView.navigationDelegate self];
There is an Entitlement called: com.apple.security.network.client, that must be set to YES and it works! In iOS there is a plist called: App Transport Security Settings where you list the URL's you wish to access in the app. Thanks to theNextMan!
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)
}
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
I am trying to request a website's html code and use it in an app in Xcode (Swift 3.0) and the pod Alamofire. In the html code online, the data contents that I want to scrape are in a div class that returns data from an Events calendar, in the form of a javascript web plugin. Since the website is not static, when I request the html and print the resulting response as a string, the data I want is not contained in the string. A message appears that says:
<noscript>Your browser must support JavaScript to view this content.
Please enable JavaScript in your browser settings then try again.
Events calendar powered by Trumba
</noscript>
My code using Alamofire looks like:
func downloadCalendar(){
Alamofire.request(urlString).responseString { (AlamofireResponse) in
print(AlamofireResponse.result.value!)
}
}
The urlString is a variable for the actual webpage's url.
Is there a way to get all of the html that appears in the html online into Xcode using Alamofire? If it's not possible with Alamofire is there another way to do this using Swift?
I've tried to accomplish a similar thing, unfortunately to no avail...
It seams AlamoFire grabs the first response it gets....
There is a workaround - use UIWebView:
static let webView = UIWebView()
self.webView.loadRequest(URLRequest.init(url: URL.init(string:"http://example.com")!)
DispatchQueue.main.asyncAfter(deadline: .now()+10.0) {[unowned self] in
if let html = self.webView.stringByEvaluatingJavaScript(from: "document.documentElement.outerHTML")
{print(html)}
}
Where 10.0 is the approx number of seconds required for javascript to finish loading the webpage data.
However since: it's not thread safe, you must use a singleton webView,
import UIKit and can't do it in the background - it's far from the perfect solution...
It might be easier to setup a proxy webserver in between to do the parsing for you.
Cheers!
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>