GoogleSignInButtonBundleExtensions.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #if !arch(arm)
  17. import Foundation
  18. // MARK: - Bundle Extensions
  19. #if SWIFT_PACKAGE
  20. let GoogleSignInBundleName = "GoogleSignIn_GoogleSignIn"
  21. #else
  22. let GoogleSignInBundleName = "GoogleSignIn"
  23. #endif
  24. @available(iOS 13.0, macOS 10.15, *)
  25. extension Bundle {
  26. /// Gets the bundle for the SDK framework.
  27. /// - returns An optional instance of `Bundle`.
  28. /// - note If the main `Bundle` cannot be found, or if the `Bundle` cannot be
  29. /// found via a class, then this will return nil.
  30. static func gidFrameworkBundle() -> Bundle? {
  31. if let mainPath = Bundle.main.path(
  32. forResource: GoogleSignInBundleName,
  33. ofType: "bundle"
  34. ) {
  35. return Bundle(path: mainPath)
  36. }
  37. let classBundle = Bundle(for: GoogleSignInButtonViewModel.self)
  38. if let classPath = classBundle.path(
  39. forResource: GoogleSignInBundleName,
  40. ofType: "bundle"
  41. ) {
  42. return Bundle(path: classPath)
  43. } else {
  44. return nil
  45. }
  46. }
  47. /// Retrieves the Google icon URL from the bundle.
  48. /// - parameter name: The `String` name for the resource to look up.
  49. /// - parameter ext: The `String` extension for the resource to look up.
  50. /// - returns An optional `URL` if the resource is found, nil otherwise.
  51. static func urlForGoogleResource(
  52. name: String,
  53. withExtension ext: String
  54. ) -> URL? {
  55. let bundle = Bundle.gidFrameworkBundle()
  56. return bundle?.url(forResource: name, withExtension: ext)
  57. }
  58. }
  59. #endif // !arch(arm)