GoogleSignInButtonBundleExtensions.swift 2.0 KB

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