Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): add path utilities to bridge #3842

Merged
merged 15 commits into from
Nov 28, 2020
14 changes: 9 additions & 5 deletions ios/Capacitor/Capacitor/CAPBridgeProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import WebKit

@objc public protocol CAPBridgeProtocol: NSObjectProtocol {
// MARK: Environment Properties
// MARK: - Environment Properties
var viewController: UIViewController? { get }
var config: InstanceConfiguration { get }
var webView: WKWebView? { get }
Expand Down Expand Up @@ -33,16 +33,16 @@ import WebKit
@available(iOS 12.0, *)
@available(*, deprecated, renamed: "userInterfaceStyle")
func getUserInterfaceStyle() -> UIUserInterfaceStyle

@available(*, deprecated, message: "Moved - equivalent is found on config.localURL")
func getLocalUrl() -> String

// MARK: Call Management
// MARK: - Call Management
func getSavedCall(_ callbackId: String) -> CAPPluginCall?
func releaseCall(_ call: CAPPluginCall)
func releaseCall(callbackId: String)

// MARK: JavaScript Handling
// MARK: - JavaScript Handling
func evalWithPlugin(_ plugin: CAPPlugin, js: String)
func eval(js: String)

Expand All @@ -55,7 +55,11 @@ import WebKit
func triggerDocumentJSEvent(eventName: String)
func triggerDocumentJSEvent(eventName: String, data: String)

// MARK: View Presentation
// MARK: - Paths, Files, Assets
func localURL(fromWebURL webURL: URL?) -> URL?
func portablePath(fromLocalURL localURL: URL?) -> URL?

// MARK: - View Presentation
func showAlertWith(title: String, message: String, buttonTitle: String)
func presentVC(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?)
func dismissVC(animated flag: Bool, completion: (() -> Void)?)
Expand Down
62 changes: 1 addition & 61 deletions ios/Capacitor/Capacitor/CAPFile.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
public class CAPFile {
var url: URL

public init(url: URL) {
self.url = url
}
}

/**
* CAPFileManager helps map file schemes to physical files, whether they are on
* disk, in a bundle, or in another location.
*/
@objc public class CAPFileManager: NSObject {
static func get(path: String) -> CAPFile? {
let handlers: [String: CAPFileResolver.Type] = [
"res://": CAPFileResolverResource.self,
"file://": CAPFileResolverFile.self,
"base64:": CAPFileResolverNotImplemented.self
]

for (handlerPrefix, handler) in handlers {
if path.hasPrefix(handlerPrefix) {
return handler.resolve(path: path)
}
}

return nil
}

@available(*, deprecated, message: "Use portablePath(fromLocalURL:) on the Bridge")
public static func getPortablePath(host: String, uri: URL?) -> String? {
if let uri = uri {
let uriWithoutFile = uri.absoluteString.replacingOccurrences(of: "file://", with: "")
Expand All @@ -35,40 +12,3 @@ public class CAPFile {
return nil
}
}

private protocol CAPFileResolver {
static func resolve(path: String) -> CAPFile?
}

private class CAPFileResolverFile: CAPFileResolver {
static func resolve(path: String) -> CAPFile? {
let manager = FileManager.default
let absPath = path.replacingOccurrences(of: "file:///", with: "")
if !manager.fileExists(atPath: absPath) {
return nil
}
return CAPFile(url: URL(fileURLWithPath: absPath))
}

}

private class CAPFileResolverResource: CAPFileResolver {
static func resolve(path: String) -> CAPFile? {
let manager = FileManager.default
let bundle = Bundle.main
let resourcePath = bundle.resourcePath

var absPath = path.replacingOccurrences(of: "res://", with: "")
absPath = resourcePath! + "/" + absPath
if !manager.fileExists(atPath: absPath) {
return nil
}
return CAPFile(url: URL(fileURLWithPath: absPath))
}
}

private class CAPFileResolverNotImplemented: CAPFileResolver {
static func resolve(path: String) -> CAPFile? {
return nil
}
}
49 changes: 47 additions & 2 deletions ios/Capacitor/Capacitor/CapacitorBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol {
public func getUserInterfaceStyle() -> UIUserInterfaceStyle {
return userInterfaceStyle
}

public func getLocalUrl() -> String {
return config.localURL.absoluteString
}

@nonobjc public func setStatusBarAnimation(_ animation: UIStatusBarAnimation) {
statusBarAnimation = animation
}
Expand Down Expand Up @@ -533,6 +533,51 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol {
}
}

// MARK: - CAPBridgeProtocol: Paths, Files, Assets

/**
* Translate a URL from the web view into a file URL for native iOS.
*
* The web view may be handling several different types of URLs:
* - res:// (shortcut scheme to web assets)
* - file:// (fully qualified URL to file on the local device)
* - base64:// (to be implemented)
* - [web view scheme]:// (already converted once to load in the web view, to be implemented)
*/
public func localURL(fromWebURL webURL: URL?) -> URL? {
guard let inputURL = webURL else {
return nil
}

let url: URL

switch inputURL.scheme {
case "res":
guard let resourcePath = Bundle.main.resourcePath else {
return nil
}

url = URL(fileURLWithPath: resourcePath).appendingPathComponent("public").appendingPathComponent(inputURL.path)
imhoffd marked this conversation as resolved.
Show resolved Hide resolved
case "file":
url = inputURL
default:
return nil
}

return url
}

/**
* Translate a file URL for native iOS into a URL to load in the web view.
*/
public func portablePath(fromLocalURL localURL: URL?) -> URL? {
guard let inputURL = localURL else {
return nil
}

return self.config.localURL.appendingPathComponent(CapacitorBridge.fileStartIdentifier).appendingPathComponent(inputURL.path)
}

// MARK: - CAPBridgeProtocol: View Presentation

@objc public func showAlertWith(title: String, message: String, buttonTitle: String) {
Expand Down