>您好,我有一个简单的应用程序,我试图将 html 创建到 pdf 到 web 视图并看起来很成功,但我想添加与 UIActivityViewController 的按钮共享(进入打印、邮件等)
我的代码在这里。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createPDF();
loadPDF("file.pdf");
// ActivityViewController
btn!.setTitle("Open Menu", forState: .Normal)
btn!.addTarget(self, action: "pressBtn:", forControlEvents: .TouchUpInside)
btn!.sizeToFit()
btn!.center = view.center
view.addSubview(btn!)
}
func createPDF() {
let html = "<b>Hello <i>World!</i></b> <p>Generate PDF file from HTML in Swift</p>"
let fmt = UIMarkupTextPrintFormatter(markupText: html)
// 2. Assign print formatter to UIPrintPageRenderer
let render = UIPrintPageRenderer()
render.addPrintFormatter(fmt, startingAtPageAtIndex: 0)
// 3. Assign paperRect and printableRect
let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi
let printable = CGRectInset(page, 0, 0)
render.setValue(NSValue(CGRect: page), forKey: "paperRect")
render.setValue(NSValue(CGRect: printable), forKey: "printableRect")
// 4. Create PDF context and draw
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil)
for i in 1...render.numberOfPages() {
UIGraphicsBeginPDFPage();
let bounds = UIGraphicsGetPDFContextBounds()
render.drawPageAtIndex(i - 1, inRect: bounds)
}
UIGraphicsEndPDFContext();
// 5. Save PDF file
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
pdfData.writeToFile("(documentsPath)/file.pdf", atomically: true)
}
func loadPDF(filename: String) {
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let filePath = "(documentsPath)/file.pdf"
let url = NSURL(fileURLWithPath: filePath)
let urlRequest = NSURLRequest(URL: url)
webView!.loadRequest(urlRequest)
}
func pressBtn(sender:UIButton){
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let filePath = "(documentsPath)/file.pdf"
let activityItems = [filePath]
let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
当单击按钮出现此错误时
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x79b601b0>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
*** First throw call stack:
感谢您的帮助。
您是否尝试过在调用 presentViewController 之前指定源视图?
let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = sender
presentViewController(activityViewController, animated: true, completion: nil)
错误可能是其他原因,
无论如何,您都希望使用UIDocumentInteractionController而不是UIActivityController
。将 uti 设置为 "com.adobe.pdf"