Last active
May 1, 2019 22:07
-
-
Save katiesmillie/e4bba679c1476d50748b54284894d574 to your computer and use it in GitHub Desktop.
Parsing a deep link like urbanarchive://posts/9987
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Call from App Delegate | |
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { | |
guard let components = URLComponents(string: url.absoluteString), let scheme = components.scheme else { return true } | |
let absolute = url.absoluteString | |
let path = absolute.replacingOccurrences(of: "\(scheme)://", with: "") | |
launchCoordinator.appCoordinator.deepLink(path: path) | |
return true | |
} | |
// Call from App Coordinator | |
func deepLink(path: String) { | |
guardf let route = URLRoute.parse(path: path) else { return } | |
Analytics().track("Open From Deep Link", properties: ["Path": path]) | |
switch route { | |
case let .specificList(id): | |
// | |
case let .specificOrganization(id): | |
// | |
case .specificPerson, .specificLocation, .specificPost: | |
// | |
} | |
} | |
} | |
// URLRoute class | |
enum URLRoute { | |
case specificList(id: String) | |
case specificLocation(id: String) | |
case specificOrganization(id: String) | |
case specificPost(id: String) | |
case specificPerson(id: String) | |
static func parse(path: String) -> URLRoute? { | |
let pathComponents = path.components(separatedBy: "/") | |
if pathComponents.first == "lists", | |
let listID = pathComponents[safe: 1] { | |
return .specificList(id: listID) | |
} | |
if pathComponents.first == "locations", | |
let locationID = pathComponents[safe: 1] { | |
return .specificLocation(id: locationID) | |
} | |
if pathComponents.first == "posts", | |
let imageID = pathComponents[safe: 1] { | |
return .specificPost(id: imageID) | |
} | |
if pathComponents.first == "organizations", | |
let orgID = pathComponents[safe: 1] { | |
return .specificOrganization(id: orgID) | |
} | |
if pathComponents.first == "profiles", | |
let personID = pathComponents[safe: 1] { | |
return .specificPerson(id: personID) | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment