GoogleSignInButtonBundleExtensions.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2022 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. // MARK: - Bundle Extensions
  18. #if SWIFT_PACKAGE
  19. let GoogleSignInBundleName = "GoogleSignIn_GoogleSignIn"
  20. #else
  21. let GoogleSignInBundleName = "GoogleSignIn"
  22. #endif
  23. @available(iOS 13.0, macOS 10.15, *)
  24. extension Bundle {
  25. /// Gets the bundle for the SDK framework.
  26. /// - returns An optional instance of `Bundle`.
  27. /// - note If the main `Bundle` cannot be found, or if the `Bundle` cannot be
  28. /// found via a class, then this will return nil.
  29. static func gidFrameworkBundle() -> Bundle? {
  30. if let mainPath = Bundle.main.path(
  31. forResource: GoogleSignInBundleName,
  32. ofType: "bundle"
  33. ) {
  34. return Bundle(path: mainPath)
  35. }
  36. let classBundle = Bundle(for: GoogleSignInButtonViewModel.self)
  37. if let classPath = classBundle.path(
  38. forResource: GoogleSignInBundleName,
  39. ofType: "bundle"
  40. ) {
  41. return Bundle(path: classPath)
  42. } else {
  43. return nil
  44. }
  45. }
  46. /// Retrieves the Google icon URL from the bundle.
  47. /// - parameter name: The `String` name for the resource to look up.
  48. /// - parameter ext: The `String` extension for the resource to look up.
  49. /// - returns An optional `URL` if the resource is found, nil otherwise.
  50. static func urlForGoogleResource(
  51. name: String,
  52. withExtension ext: String
  53. ) -> URL? {
  54. let bundle = Bundle.gidFrameworkBundle()
  55. return bundle?.url(forResource: name, withExtension: ext)
  56. }
  57. }