Created
August 29, 2018 07:07
-
-
Save dilipiOSDeveloper/35548ddb6d1f7a57c1d442e7babcb96e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
func printItem(_ data: Data, withFilePath filePath: String){ | |
printController = UIPrintInteractionController.shared | |
printController?.delegate = self | |
let printInfo = UIPrintInfo.printInfo() | |
printInfo.outputType = .general | |
printInfo.jobName = "QR Code Image" | |
printInfo.duplex = .longEdge | |
printController?.printInfo = printInfo | |
printController?.showsPageRange = true | |
//If NSData contains data of image/PDF | |
if (printController != nil) && UIPrintInteractionController.canPrint(data) { | |
printController?.printingItem = data | |
} | |
else { | |
let webView = UIWebView() | |
printInfo.jobName = webView.request?.url?.absoluteString ?? "" | |
webView.loadRequest(URLRequest(url: NSURL.fileURL(withPath: filePath))) | |
printController?.printFormatter = webView.viewPrintFormatter() | |
} | |
let completionHandler: ((UIPrintInteractionController, Bool, Error) -> Void)? = { printController, completed, error in | |
if !(completed && error as! Bool) { | |
//NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code); | |
print("Printing could not complete because of error: \(error)") | |
} | |
} | |
if UIDevice.current.userInterfaceIdiom == .pad { | |
//printController?.present(from: view.frame, in: view, animated: true, completionHandler: completionHandler as! UIPrintInteractionCompletionHandler?) | |
print(completionHandler as Any) | |
printController?.present(from: self.view.frame, in: self.view, animated: true, completionHandler: nil) | |
} | |
else { | |
printController?.present(from: self.view.frame, in: self.view, animated: true, completionHandler: nil) | |
} | |
} | |
@IBAction func printQRCode(_ sender: Any) { | |
// stringQRCode is url of image which shows on first screen above print button | |
let myStringData = stringQRCode?.data(using: String.Encoding.utf8) | |
print("URL NSDATA : \(myStringData)") | |
print(stringQRCode!) | |
printItem(myStringData!, withFilePath: stringQRCode!) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment