Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Implement send to Firefox/Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
thebnich committed Oct 13, 2016
1 parent cb25b16 commit 79651da
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Blockzilla.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
D34E70331DA8736300BABDCC /* AutocompleteTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34E70321DA8736300BABDCC /* AutocompleteTextField.swift */; };
D34E70351DA874AA00BABDCC /* StringExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D34E70341DA874AA00BABDCC /* StringExtensions.swift */; };
D362D2AB1C4EAF940041980A /* rights-Focus.html in Resources */ = {isa = PBXBuildFile; fileRef = D362D2AA1C4EAF940041980A /* rights-Focus.html */; };
D36C1BAA1DB02EBB0073C1AB /* OpenUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = D36C1BA91DB02EBB0073C1AB /* OpenUtils.swift */; };
D37DE55D1BCDBD6100906364 /* WaveView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D37DE55C1BCDBD6100906364 /* WaveView.swift */; };
D38D693D1C471A98003EF211 /* GCDWebServers.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D38D693B1C471A98003EF211 /* GCDWebServers.framework */; };
D38D693E1C471A98003EF211 /* SnapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D38D693C1C471A98003EF211 /* SnapKit.framework */; };
Expand Down Expand Up @@ -119,6 +120,7 @@
D34E70341DA874AA00BABDCC /* StringExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringExtensions.swift; sourceTree = "<group>"; };
D362D26A1C47326A0041980A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
D362D2AA1C4EAF940041980A /* rights-Focus.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "rights-Focus.html"; sourceTree = "<group>"; };
D36C1BA91DB02EBB0073C1AB /* OpenUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenUtils.swift; sourceTree = "<group>"; };
D37DE55C1BCDBD6100906364 /* WaveView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WaveView.swift; sourceTree = "<group>"; };
D38D693B1C471A98003EF211 /* GCDWebServers.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GCDWebServers.framework; path = Carthage/Build/iOS/GCDWebServers.framework; sourceTree = "<group>"; };
D38D693C1C471A98003EF211 /* SnapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SnapKit.framework; path = Carthage/Build/iOS/SnapKit.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -278,6 +280,7 @@
D3E251E71DAD714C005918DC /* InstructionsView.swift */,
D3E251E91DAD714C005918DC /* IntroSlideWelcome.swift */,
D3E2C8F91D9F170200DEBE3D /* LocalContentBlocker.swift */,
D36C1BA91DB02EBB0073C1AB /* OpenUtils.swift */,
D3E251E81DAD714C005918DC /* SafariInstructionsViewController.swift */,
D33A1AB01BC48FAC0003D929 /* SettingsViewController.swift */,
D34E70341DA874AA00BABDCC /* StringExtensions.swift */,
Expand Down Expand Up @@ -474,6 +477,7 @@
E4BF2DD71BACE8CA00DA9D68 /* AppDelegate.swift in Sources */,
D3E2C9691DA3024800DEBE3D /* InsetButton.swift in Sources */,
D33A1AB11BC48FAC0003D929 /* SettingsViewController.swift in Sources */,
D36C1BAA1DB02EBB0073C1AB /* OpenUtils.swift in Sources */,
D392881D1BC5CF180016A9A0 /* UIColorExtensions.swift in Sources */,
D3A8B1BE1DA5B56A00F450AD /* URLBarContainer.swift in Sources */,
D3E3CA7D1DA827AD0079C94B /* HomeView.swift in Sources */,
Expand Down
5 changes: 3 additions & 2 deletions Blockzilla/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ extension BrowserViewController: URLBarDelegate {
var url = URIFixup.getURL(entry: text)

if url == nil {
guard let escaped = text.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
guard let escaped = text.addingPercentEncoding(withAllowedCharacters: .urlQueryParameterAllowed),
let searchUrl = URL(string: SearchTemplate.replacingOccurrences(of: "%s", with: escaped)) else {
assertionFailure("Invalid search URL")
return
Expand Down Expand Up @@ -122,7 +122,8 @@ extension BrowserViewController: BrowserToolbarDelegate {
}

func browserToolbarDidPressSend(browserToolbar: BrowserToolbar) {
print("TODO: Sending not yet supported.")
guard let url = browser.url else { return }
OpenUtils.openInExternalBrowser(url: url)
}
}

Expand Down
6 changes: 6 additions & 0 deletions Blockzilla/CharacterSetExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import Foundation

extension CharacterSet {
/// Returns the character set for characters allowed in a URL.
public static var urlAllowed: CharacterSet {
return CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=%")
}

/// Returns the character set for characters allowed in a URL query parameter.
public static var urlQueryParameterAllowed: CharacterSet {
return CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~")
}
}
30 changes: 30 additions & 0 deletions Blockzilla/OpenUtils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import Foundation

class OpenUtils {
private static let app = UIApplication.shared

private static func openInFirefox(url: URL) -> Bool {
guard let escaped = url.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlQueryParameterAllowed),
let firefoxURL = URL(string: "firefox://open-url?url=\(escaped)") else {
return false
}

return app.openURL(firefoxURL)
}

private static func openInSafari(url: URL) {
app.openURL(url)
}

/// Opens the URL in Firefox, if Firefox is available.
/// Otherwise, the URL is opened in Safari.
static func openInExternalBrowser(url: URL) {
if !openInFirefox(url: url) {
openInSafari(url: url)
}
}
}

0 comments on commit 79651da

Please sign in to comment.