您好我有简单的应用程序我试图创建HTML到pdf到webview并寻找成功,但我想用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:
感谢您的帮助。
答案 0 :(得分:1)
您是否尝试在调用presentViewController之前指定源视图?
let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = sender
presentViewController(activityViewController, animated: true, completion: nil)
答案 1 :(得分:1)
错误可能是别的,
任何方式,您都希望使用UIDocumentInteractionController而不是UIActivityController
将uti设置为&#34; com.adobe.pdf&#34;