i have an app to download pdf file. If the download completed it have a path like
file:///Users/developer/Library/Developer/CoreSimulator/Devices/F9AB746D-20C3-4333-AE70-2840538F4AED/data/Containers/Data/Application/861FA32B-06AA-4176-A499-B4BB8DF224C1/Library/Caches/pdffile.pdf
I try to view using this
#IBOutlet weak var webView: UIWebView!
var path = ""
var receiveData: String = "" //this contain file:///Users/developer.../Caches/pdffile.pdf
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL.fileURLWithPath(receiveData)
print(url)
self.webView.loadRequest(NSURLRequest(URL: url))
}
the build succeded and no error butstill can't view the pdf file
what's wrong?
Change your NSURL path, you need to use receiveData, because that already contain the path of the file, NSBundle is use when you have file inside your project directory.
let url = NSURL.fileURLWithPath(receiveData)
Related
I need to embed a web inside a MAC executable, so that the html is inside the executable (and not loaded from an external server). It should also be possible to print the contents of the webview.
I have tried with Xcode and Swift, but when printing the webview it looks blank, there are lots of entries in Stack Overflow and seems to be no solution for this in swift.
I have also tried Xojo.com, but the contents are outside the executable visible to the user and must be hidden inside.
Any guidance?
CODE:
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var WEBVW: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
if let url = Bundle.main.url ( forResource: "index"
, withExtension: "html"
, subdirectory: "www")
{
let path = url.deletingLastPathComponent();
self.WEBVW.loadFileURL ( url
, allowingReadAccessTo: path);
WEBVW.printView(self)
}
}
}
IMAGE: Program running and printing: https://postimg.cc/Lqsddv1T
Many thanks
I placed a folder with images into my project as Folder Reference. Now I can see it in my Package in Contents/Resources/Images/foo.jpg
I try to populate an NSImage like this:
#IBOutlet var imageView: NSImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = NSImage (named: "Images/foo.jpg")
}
But no image is shown when the app is loaded. I am using interface builder not XIB. Does it make a difference? If I add the images as Group I can use named:"foo.jpg" without any issue.
I understand now the inner workings. This is my solution:
#IBOutlet var imageView: NSImageView!
override func viewDidLoad() {
super.viewDidLoad()
let image = Bundle.main.path(forResource: "foo", ofType: "jpg", inDirectory: "Images")
imageView.image = NSImage (contentsOfFile: image)
}
The reference files are not named in my bundle so they are never found. With the full path it works well.
I have been working on ios app which now i am facing some problems. I want to load some files like docx, xls using UIWebView. It gets me white screen on my actual device.
#IBOutlet weak var fileServerWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
if let path = Bundle.main.path(forResource: "easychair", ofType: "docx"){
let targetUrl: URL = URL(fileURLWithPath: path)
let request = URLRequest(url: targetUrl as URL)
fileServerWebView.delegate = self
fileServerWebView.scalesPageToFit = true
fileServerWebView.loadRequest(request)
}else{
print("cannot get path")
}
// Do any additional setup after loading the view.
}
That is code that i tried to load that file from local. Thank you for your help.
I've made an app that would call data from Database via webservice, create an XML page and parse it. After that it would post a text in WebView but for some reason it can't recognise line breaks and just post the text ignoring all "\n".
Is there something I've missed?
Thanks!
This is how I add text to my WebView:
import UIKit
class PubViewController: UIViewController {
#IBOutlet weak var myWebView1: UIWebView!
var selectedFeedTitle = String()
var selectedFeedFeedContent = String()
var selectedFeedURL = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let feedContent:String! = "<h3>\(selectedFeedTitle)</h3>\(selectedFeedFeedContent)"
myWebView1.loadHTMLString(feedContent, baseURL: nil)
}
Line breaks are ignored in HTML. You have to use the <br/> tag:
let feedContent:String! = "<h3>\(selectedFeedTitle)</h3>\(selectedFeedFeedContent)"
.stringByReplacingOccurrencesOfString("\n", withString: "<br/>")
I've seen a few options on here using just Objective-C, but I'm having trouble doing this with Swift iOS8 in XCode 6. I'm using the uiwebview to load a website. For sake of example, let's say its google.com.
#IBOutlet var website: UIWebView!
var url = "http://www.google.com"
func loadUrl() {
let requestURL = NSURL(string: url)
let request = NSURLRequest(URL: requestURL!)
website.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
website.delegate = self
loadUrl()
}
func webViewDidFinishLoad(website: UIWebView) {
var jsscript = "some script here"
website.stringByEvaluatingJavaScriptFromString(jsscript)
}
Using Swift, how would I inject a whole new stylesheet into the website so I can overwrite many of the styles?
Also, I'd like the new stylesheet to exist in one of my app's directories. Where is the best directory for that to be? Under "Supporting files?" And, how would I call that path in my code?
It would also be nice if the website wouldn't load until the styles had been applied.
So, I think I found at least one way to accomplish appending styles to the webpage being loaded using Swift:
var loadStyles = "var script =
document.createElement('link');
script.type = 'text/css';
script.rel = 'stylesheet';
script.href = 'http://fake-url/styles.css';
document.getElementsByTagName('body')[0].appendChild(script);"
website.stringByEvaluatingJavaScriptFromString(loadStyles)
Using the now preferred WKWebView you can put the external css file, say tweaks.css in the root of your project or in a sub-folder, then you can inject it into your page like this:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
guard let path = Bundle.main.path(forResource: "tweaks", ofType: "css") else { return }
let css = try! String(contentsOfFile: path).replacingOccurrences(of: "\\n", with: "", options: .regularExpression)
let js = "var style = document.createElement('style'); style.innerHTML = '\(css)'; document.head.appendChild(style);"
webView.evaluateJavaScript(js)
}
To make the file available:
Click your project
Click your target
Select Build Phases
Expand Copy Bundle Resources
Click '+' and select your file.
Also make sure that your controller implements WKNavigationDelegate:
class ViewController: UIViewController, WKNavigationDelegate