I am currently developing a iOS app which can download PDF(Have form on it) from server to iPad, then user can fill in the form on the iPad.
The problem is we need to support chinese on the field input.
Here is the field.
When I use "Quick" input method to type any character, it repeat 3 times.
When I type one more time, it repeat again.
Do anyone have issues with typing Chinese in textfield using PDFKit on iOS?
Update, Add code work
For the server-side, just a endpoint can download pdf, the pdf already added textfield on it.
On client-side, pdf is downloaded in viewDidLoad() and set into pdfView as following code.
override func viewDidLoad() {
super.viewDidLoad()
if let code = self.code {
let url = URL(string : API.pdf + "/" + code)
let loading = self.showLoading()
var request = URLRequest(url: url!)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request, completionHandler: { (responseData: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async {
self.pdfView.document = PDFDocument(data : responseData!)
self.pdfView.scaleFactor = 1
self.pdfView.backgroundColor = UIColor.lightGray
self.pdfView.autoScales = true
loading.dismiss(animated: true, completion: nil)
}
})
task.resume()
}
}
In story board, it is just a UIView and set custom class as PDFView
Updated 2
After that, i do a simple testing.
I create a simple testing controller and load pdf with just a simple textfield annotation. Result is same.
import UIKit
import PDFKit
class PDFTestViewController : ViewController {
#IBOutlet weak var pdfView: PDFView!
override func viewDidLoad() {
super.viewDidLoad()
if let url = Bundle.main.url(forResource: "4547315264964", withExtension: "pdf"),
let doc = PDFDocument(url: url){
self.pdfView.document = doc
self.pdfView.scaleFactor = 1
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Related
I am downloading files from server and showing in my ios device. File type can be any thing like image, pdf etc. and all loading up fine. Now the problem is, some part of file or image is hiding behind the title of page. I want to add margin at top of file after it loads up.
Code I have written to show file:
func openFile(file:String) {
let myBlog = file
let url = NSURL(string: myBlog)
let request = NSURLRequest(url: url! as URL)
webView.load(request as URLRequest)
self.view.addSubview(webView)
let pdfVC = UIViewController()
pdfVC.view.addSubview(webView)
pdfVC.title = "File"
self.navigationController?.pushViewController(pdfVC, animated: true)
self.navigationController!.navigationBar.tintColor = colors.whiteColor
}
I have created webView property within Class like:
var webView : WKWebView!
And inside viewDidLoad(), I have written:
webView = WKWebView(frame: self.view.frame)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.isUserInteractionEnabled = true
webView.navigationDelegate = self
Result I am getting:
Here some part of image is behind the title. I have tried verious solutions given in link add margins in swift, but nothing is working.
My suggestion is to create instance of WKWebView in controller where you need to display it, in your case instance pdfVC
There is can be model, for example:
enum FileType {
case image(fileURL: URL)
case pdf(fileURL: URL)
}
Creating incase of FileViewController and assign fileType to it
func openFile(file:String) {
let pdfViewController = FileViewController()
pdfVC.fileType = FileType.pdf(fileURL: URL(string: "https://www.google.com")!) // example
pdfVC.title = "File"
navigationController?.pushViewController(pdfVC, animated: true)
}
And inside your VC where you need to display a file:
In this way, even if you do have navigationBar you won't have problem with navigationBar and topMargin
class FileViewController: UIViewController {
var fileType: FileType!
override func viewDidLoad() {
super.viewDidLoad()
var urlRequest: URLRequest!
switch fileType {
case .image(let url), .pdf(let url):
urlRequest = URLRequest(url: url)
#unknown default:
break
}
let webView = WKWebView(frame: view.frame)
webView.load(urlRequest)
self.view = webView
}
}
Hope this will help you!
use Bounds instead of Frame
webView = WKWebView(frame: self.view.Bounds)
I want to show a website in a WKWebView in my iOS app for which I need to set specific http cookies for the user's session. The cookies are already stored in the shared HttpCookieStorage of the app.
At the moment, I try to copy them from there to the WKWebsiteDataStore of the web view before loading the website with the following code:
class ViewerViewController: UIViewController {
// MARK: - Properties
var webView: WKWebView!
// MARK: - Life Cycle
override func loadView() {
webView = WKWebView()
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
addCookiesToWebView {
print("Load website")
if let url = Bundle.main.url(forResource: "MyWebsite", withExtension: "html"), let html = try? String(contentsOf: url) {
self.webView.loadHTMLString(html, baseURL: url)
}
}
}
// MARK: - Functions
func addCookiesToWebView(completionHandler: #escaping (() -> Void)) {
let sharedCookies = HTTPCookieStorage.shared.cookies!
let webViewCookieStore = webView.configuration.websiteDataStore.httpCookieStore
var count = 0
sharedCookies.forEach { cookie in
DispatchQueue.main.async {
webViewCookieStore.setCookie(cookie) {
count += 1
print("Added cookie \(cookie.name)")
if count == sharedCookies.count {
completionHandler()
}
}
}
}
}
}
Now, what is incomprehensible for me, is that this sometimes works and the user is authenticated in his user session, but sometimes it doesn't.
It seems like there might be a race condition that leads to the web view loading the website before all the cookies have been copied? When I researched this problem I noticed that other people have problems with cookies in WKWebView as well, but I didn't find any solution so far that actually solves my issue.
Is there a bug in my code or is there a better approach for copying multiple cookies from HttpCookieStorage to a WKWebView?
I am building an application with webView and Firebase as database. The url that should be used to load the webView should come from the database.
I have implemented both webView and Firebase correctly but the problem is that my webView is not loading with the url from the database but the url declared in the var declaration.
It seems that viewDidLoad call in priority the methods webView load before the closure inside Database Observe method
If I look at the console, I can clearly see that everything is working BUT the url that should be used to load the webview is retrieved from the database AFTER webview load...this should be done BEFORE given that I put the retrieve method BEFORE...
class ViewController: UIViewController {
var dbRef: DatabaseReference!
var nextUrl = "https://www.google.co.uk/"
func retrieveUrl () {
dbRef = Database.database().reference().child("EXAMPLE")
dbRef.observe(.value) { (snapshot) in
let value = snapshot.value as! NSDictionary
let url = value["url"]!
self.nextUrl = (url as! String)
print (nextUrl)
}
print ("function observe is called")
}
override func viewDidLoad () {
super.viewDidLoad()
retrieveUrl()
print ("webview is about to load")
let request = URLRequest (url: URL(string: nextUrl)!)
self.webView.load(request)
self.webView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
}
}
in the console I can see that "webview is about to load" is printed before nextUrl...which is very strange to me
You should load webView in callback of firebase observer.
Because you are loading webView right after firebase network call its like async work, so webView try to loads with nextUrl is empty or not, to prevent async like behavior you must wait for nextUrl or you should load webView in firebase callback
Do it like following
class ViewController: UIViewController {
var dbRef: DatabaseReference!
var nextUrl = "https://www.google.co.uk/"
func retrieveUrl () {
dbRef = Database.database().reference().child("EXAMPLE")
dbRef.observe(.value) {
(snapshot) in
let value = snapshot.value as! NSDictionary
let url = value["url"]!
self.nextUrl = (url as! String)
print (nextUrl)
print ("webview is about to load")
let request = URLRequest (url: URL(string: nextUrl)!)
self.webView.load(request)
self.webView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
}
print ("function observe is called")
}
override func viewDidLoad () {
super.viewDidLoad()
retrieveUrl()
UIDocumentInteractionController is not working with large pdf files with multiple pages.
Here in my code,
var docController:UIDocumentInteractionController!
...
DispatchQueue.main.async (execute: { [weak self] in
self?.docController = UIDocumentInteractionController(url: pdfFileURL!)
self?.docController.delegate = self
self?.docController.name = pdfFileURL?.lastPathComponent
self?.docController.presentPreview(animated: true)
})
and delegate method,
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
This is the warning in console,
2017-05-26 12:46:51.178894 MyApp [3350:1136818] [default] View service did terminate with error: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted} #Remote
Below attached is the blank image,
Please help me out thanks.
Try Like This:
Declare : var interaction: UIDocumentInteractionController?
Then add
interaction = UIDocumentInteractionController(url: URL(string: "<PDF FILE PATH>")!)
interaction.delegate = self
interaction.presentPreview(animated: true) // IF SHOW DIRECT
OR if need any suggestion popup
interaction.presentOpenInMenu(from: /*<SOURCE BUTTON FRAME>*/, in: self.view, animated: true)
Implement Delegate -> UIDocumentInteractionControllerDelegate
public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
public func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
interaction = nil
}
Be sure that your file is actually pdf file by checking its extension or if url path contains ".pdf"
Otherwise it will recognize it as text file and will not open it.
I just implemented UIDocumentInteractionController last week and it's working fine.
Do one thing declare as UIDocumentInteractionController as var below to class
then do allocation and initialization in viewDidLoad()as I did
documentInteractionController = UIDocumentInteractionController.init()
documentInteractionController?.delegate = self
and at the time of presenting I make it like this
documentInteractionController?.url = url
documentInteractionController?.presentPreview(animated: true)
Hope it will resolve your problem.
(1) This issue usually occur in simulator. Check it on real device.
If that not the case
(2) Try this,
class ViewController: UIViewController, UIDocumentInteractionControllerDelegate {
var docController:UIDocumentInteractionController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let fileURL = NSBundle.mainBundle().pathForResource("SamplePDF1", ofType: "pdf") { // Use if let to unwrap to fileURL variable if file exists
docController = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: fileURL))
docController.name = NSURL(fileURLWithPath: fileURL).lastPathComponent
docController.delegate = self
docController.presentPreviewAnimated(true)
}
}
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
return self
}
func documentInteractionControllerDidEndPreview(controller: UIDocumentInteractionController) {
docController = nil
}
If you're loading a file from Bundle, this is a known bug in iOS 11.2.x. A workaround is copying the file to tmp:
let docUrl = Bundle.main.url(forResource: "Doc", withExtension: "pdf")!
// copy to tmp, because there is a bug in iOS 11.2.x,
// which will make pdf in UIDocumentInteractionController blank
let temporaryDirectoryPath = NSTemporaryDirectory()
let tmpDir = URL(fileURLWithPath: temporaryDirectoryPath, isDirectory: true)
let tmpUrl = tmpDir.appendingPathComponent("Doc.pdf")
do {
if FileManager.default.fileExists(atPath: tmpUrl.path) {
try FileManager.default.removeItem(at: tmpUrl)
}
try FileManager.default.copyItem(at: docUrl, to: tmpUrl)
let reader = UIDocumentInteractionController(url: tmpUrl)
reader.delegate = self
reader.presentPreview(animated: true)
} catch let error {
print("Cannot copy item at \(docUrl) to \(tmpUrl): \(error)")
}
Reference: https://forums.developer.apple.com/thread/91835
It does not issue with UIDocumentInteractionController.
In my case the PDF file is corrupted, that's why it is showing me the same screen.
Try to upload/open verified pdf file and you can preview same with UIDocumentInteractionController
In an app I'm writing, I need to download a PDF from a server and display it in a UIWebView. To this end, I've got a bit of code that retrieves the PDF from an endpoint (it's not a URL, and on a desktop computer, opens up a dialogue for saving as in a browser) and loads it onto the device by grabbing the data in a class called PDFGrabber:
func getPDF(completionHandler:#escaping (URL) -> Void)
{
let theURL:String = "https://mywebsite.com/Endpoint"
let fileURL:URL = URL(string: theURL)!
var request = URLRequest(url: fileURL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: {data, response, error -> Void in
var documentURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last
documentURL = documentURL?.appendingPathComponent("MyDocument.pdf")
do
{
try data?.write(to: documentURL!, options: .atomic)
completionHandler(documentURL!)
}
catch
{
print(error)
}
})
task.resume()
}
Then, in the table view controller showing the PDFs (let's call it PDFTableView, I can use the documentURL from the PDFGrabber when a PDF is requested (by tapping a cell in the table):
PDFGrabber.getPDF(){fileURL -> () in
DispatchQueue.main.async
{
if let resultController = self.storyboard!.instantiateViewController(withIdentifier: "PDFViewer") as? PDFViewer
{
resultController.thePDFPath = stringURL
self.present(resultController, animated: true, completion: nil)
}
}
Finally, I have another View Controller with a UIWebView called "WebView" and the attribute "thePDFPath" as a string in the PDFViewer view controller. In the viewDidLoad() method, I can say:
override func viewDidLoad()
{
let pathToPDF:URL = URL(string: thePDFPath)!
WebView.loadRequest(URLRequest(url: pathToPDF))
}
Together, this loads the PDF into the web view. However, the loading times can be a little slow, and I'd like to be able to calculate how much of the PDF has been loaded onto a user's device using a progress bar and a string. From other questions, I gather than I'd need to have my PDFGrabber class extend URLSessionDownloadDelegate, and then implement the functions. To get the amount of bytes downloaded is straightforward, since I can simply go:
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten writ: Int64, totalBytesExpectedToWrite exp: Int64)
{
percentDownloaded = (Float(writ)/Float(exp)) * 100
print("Progress: " + String(describing: percentDownloaded))
}
But I'd also like to be able to open the PDFView only after the PDF is wholly downloaded after displaying its progress as a percent (I've got an overlay view that does this). Before, I could use a completion handler and wait until the PDF finished downloading, then open it.
However, this method does not allow me to access the amount of bytes downloaded; would I go about opening the view from the
urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
function, or is there something else I need to do?
Suggestions would be much appreciated; thanks!
import WebKit
here you can load your pdf in your application
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let pdfURL = Bundle.main.url(forResource: "food-and-drink-menu-trafalgar-tavern", withExtension: "pdf", subdirectory: nil, localization: nil) {
do {
let data = try Data(contentsOf: pdfURL)
let webView = WKWebView(frame: CGRect(x:0,y:NavigationView.frame.size.height,width:view.frame.size.width, height:view.frame.size.height-NavigationView.frame.size.height))
webView.load(data, mimeType: "application/pdf", characterEncodingName:"", baseURL: pdfURL.deletingLastPathComponent())
view.addSubview(webView)
}
catch {
// catch errors here
}
}
}