Skip to content

Commit

Permalink
Add Print and Save as PDF to Share Sheet (mozilla-mobile#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyg authored and boek committed Oct 2, 2017
1 parent 1fc7f84 commit 5dd6e0c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Blockzilla/Browser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ extension Browser: UIWebViewDelegate {
canGoForward = webView.canGoForward
}
}

func getPrintFormatter() -> UIViewPrintFormatter? {
return self.webView?.viewPrintFormatter()
}
}

extension Browser: LocalContentBlockerDelegate {
Expand Down Expand Up @@ -298,3 +302,4 @@ extension Browser: UIScrollViewDelegate {
return delegate?.browserShouldScrollToTop(self) ?? true
}
}

2 changes: 1 addition & 1 deletion Blockzilla/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ extension BrowserViewController: BrowserToolsetDelegate {

func browserToolsetDidPressSend(_ browserToolset: BrowserToolset) {
guard let url = browser.url else { return }
present(OpenUtils.buildShareViewController(url: url, anchor: browserToolset.sendButton), animated: true, completion: nil)
present(OpenUtils.buildShareViewController(url: url, browser: browser, anchor: browserToolset.sendButton), animated: true, completion: nil)
}

func browserToolsetDidPressSettings(_ browserToolbar: BrowserToolset) {
Expand Down
16 changes: 14 additions & 2 deletions Blockzilla/OpenUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ class OpenUtils {
app.openURL(url)
}

static func buildShareViewController(url: URL, anchor: UIView) -> UIActivityViewController {
static func buildShareViewController(url: URL, browser: Browser, anchor: UIView) -> UIActivityViewController {
var activities = [UIActivity]()
var activityItems: [Any] = [url]

if canOpenInFirefox {
activities.append(OpenInFirefoxActivity(url: url))
Expand All @@ -68,8 +69,19 @@ class OpenUtils {
}

activities.append(OpenInSafariActivity(url: url))

if let printFormatter = browser.getPrintFormatter() {
let printInfo = UIPrintInfo(dictionary: nil)
printInfo.jobName = url.absoluteString
printInfo.outputType = .general
activityItems.append(printInfo)

let renderer = UIPrintPageRenderer()
renderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
activityItems.append(renderer)
}

let shareController = UIActivityViewController(activityItems: [url], applicationActivities: activities)
let shareController = UIActivityViewController(activityItems: activityItems, applicationActivities: activities)

shareController.popoverPresentationController?.sourceView = anchor
shareController.popoverPresentationController?.sourceRect = anchor.bounds
Expand Down

0 comments on commit 5dd6e0c

Please sign in to comment.