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

Fix #8516: Add Open in Brave to iOS Share Sheet Action (Action Extension) #8682

Merged
merged 15 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions App/ActionExtension/ActionExtension.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BRAVE_URL_SCHEME</key>
<string>$(BRAVE_URL_SCHEME)</string>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>Open in Brave</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>$(BRAVE_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(BRAVE_BUILD_ID)</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsText</key>
<true/>
<key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
</dict>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.ui-services</string>
<key>NSExtensionPrincipalClass</key>
<string>ActionExtension.ActionToBraveViewController</string>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"images" : [
{
"filename" : "brave-icon-outline.png",
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
118 changes: 118 additions & 0 deletions App/ActionExtension/ActionToBraveViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2024 The Brave Authors. All rights reserved.
// 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 UIKit
import MobileCoreServices
import UniformTypeIdentifiers

class ActionToBraveViewController: UIViewController {

private enum SchemeType {
case url, query
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// Get the item[s] for handling from the extension context
for item in extensionContext?.inputItems as? [NSExtensionItem] ?? [] {
for provider in item.attachments ?? [] {

// Opening browser with search url
if provider.hasItemConformingToTypeIdentifier(UTType.text.identifier) {
loadAttachmentFor(type: .query, using: provider)
break
}

// Opening browser with site
if provider.hasItemConformingToTypeIdentifier(UTType.url.identifier) {
loadAttachmentFor(type: .url, using: provider)
break
}
}
}
}

func done() {
// Return any edited content to the host app, in this case empty
extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
}

private func loadAttachmentFor(type: SchemeType, using provider: NSItemProvider) {
let typeIdentifier: String = type == .query ? UTType.text.identifier : UTType.url.identifier

provider.loadItem(forTypeIdentifier: typeIdentifier) { item, error in
DispatchQueue.main.async {

guard let schemeUrl = self.constructSchemeURL(for: type, with: item) else {
self.done()
return
}

self.openBrowser(with: schemeUrl)
}
}
}

private func constructSchemeURL(for schemeType: SchemeType, with item: NSSecureCoding?) -> URL? {
switch schemeType {
case .query:
guard let item = item as? String else {
return nil
}

return createURL(for: .query, with: item)

case .url:
// The first URL found within item url absolute string
guard let item = (item as? URL)?.absoluteString,
let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue),
let match = detector.firstMatch(in: item, options: [], range: NSRange(location: 0, length: item.count)),
let range = Range(match.range, in: item),
let queryURL = URL(string: String(item[range]))?.absoluteString else {
return nil
}

return createURL(for: .url, with: queryURL)
}

}

private func createURL(for schemeType: SchemeType, with value: String) -> URL? {
var queryItem: URLQueryItem
var components = URLComponents()
components.scheme = Bundle.main.infoDictionary?["BRAVE_URL_SCHEME"] as? String ?? "brave"
soner-yuksel marked this conversation as resolved.
Show resolved Hide resolved

switch schemeType {
case .query:
queryItem = URLQueryItem(name: "q", value: value)
components.host = "search"
case .url:
queryItem = URLQueryItem(name: "url", value: value)
components.host = "open-url"
}

components.queryItems = [queryItem]
return components.url
}

private func openBrowser(with url: URL) {
var responder = self as UIResponder?

while let currentResponder = responder {
let selector = sel_registerName("openURL:")
if currentResponder.responds(to: selector) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0) {
Thread.detachNewThreadSelector(selector, toTarget: currentResponder, with: (url as NSURL))
}
}
responder = currentResponder.next
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.done()
}
}
}
2 changes: 2 additions & 0 deletions App/ActionExtension/de.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "In Brave öffnen";
2 changes: 2 additions & 0 deletions App/ActionExtension/en.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Open in Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/es.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Abrir en Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/fr.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Ouvrir dans Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/id-ID.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Buka di Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/it.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Apri con Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/ja.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Brave で開く";
2 changes: 2 additions & 0 deletions App/ActionExtension/ko-KR.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Brave에서 열기";
2 changes: 2 additions & 0 deletions App/ActionExtension/ms.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Buka dalam Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/nb.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Åpne i Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/pl.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Otwórz w Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/pt-BR.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Abrir no Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/ru.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Открыть в Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/sv.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Öppna i Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/tr.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Brave ile Aç";
2 changes: 2 additions & 0 deletions App/ActionExtension/uk.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "Відкрити в Brave";
2 changes: 2 additions & 0 deletions App/ActionExtension/zh-TW.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "以 Brave 開啟";
2 changes: 2 additions & 0 deletions App/ActionExtension/zh.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Bundle display name */
"CFBundleDisplayName" = "在 Brave 中打开";
Loading
Loading