gpt4 book ai didi

ios - Swift UIWebView Pdf 查看 UIActivityViewController 错误

转载 作者:行者123 更新时间:2023-11-28 12:56:36 25 4
gpt4 key购买 nike

你好,我有一个简单的应用程序,我正在尝试创建 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:

感谢您的帮助。

最佳答案

您是否尝试过在调用 presentViewController 之前指定源 View ?

let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = sender
presentViewController(activityViewController, animated: true, completion: nil)

关于ios - Swift UIWebView Pdf 查看 UIActivityViewController 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34905825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com