InAppFeedback.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import Photos
  16. import UIKit
  17. @objc(FIRFADInAppFeedback) open class InAppFeedback: NSObject {
  18. @objc(
  19. feedbackViewControllerWithAdditionalFormText:releaseName:image:onDismiss:
  20. ) public static func feedbackViewController(additionalFormText: String,
  21. releaseName: String,
  22. image: UIImage?,
  23. onDismiss: @escaping ()
  24. -> Void)
  25. -> UIViewController {
  26. // TODO: Add the additionalInfoText parameter.
  27. let frameworkBundle = Bundle(for: self)
  28. let resourceBundleURL = frameworkBundle.url(
  29. forResource: "AppDistributionInternalResources",
  30. withExtension: "bundle"
  31. )
  32. let resourceBundle = Bundle(url: resourceBundleURL!)
  33. let storyboard = UIStoryboard(
  34. name: "AppDistributionInternalStoryboard",
  35. bundle: resourceBundle
  36. )
  37. let vc: FeedbackViewController = storyboard
  38. .instantiateViewController(withIdentifier: "fir-ad-iaf") as! FeedbackViewController
  39. vc.additionalFormText = additionalFormText
  40. vc.releaseName = releaseName
  41. vc.image = image
  42. vc.viewDidDisappearCallback = onDismiss
  43. return vc
  44. }
  45. @objc(getManuallyCapturedScreenshotWithCompletion:)
  46. public static func getManuallyCapturedScreenshot(completion: @escaping (_ screenshot: UIImage?)
  47. -> Void) {
  48. getPhotoPermissionIfNecessary(completionHandler: { authorized in
  49. guard authorized else {
  50. completion(nil)
  51. return
  52. }
  53. let manager = PHImageManager.default()
  54. let fetchOptions = PHFetchOptions()
  55. fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
  56. fetchOptions.predicate = NSPredicate(
  57. format: "(mediaSubtype & %d) != 0",
  58. PHAssetMediaSubtype.photoScreenshot.rawValue
  59. )
  60. let requestOptions = PHImageRequestOptions()
  61. requestOptions.isSynchronous = true
  62. let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
  63. manager.requestImage(
  64. for: fetchResult.object(at: 0),
  65. // TODO: Identify the correct size.
  66. targetSize: CGSize(width: 358, height: 442),
  67. contentMode: .aspectFill,
  68. options: requestOptions
  69. ) { image, err in
  70. // TODO: Add logic to respond correctly if there's an error.
  71. completion(image)
  72. }
  73. })
  74. }
  75. static func getPhotoPermissionIfNecessary(completionHandler: @escaping (_ authorized: Bool)
  76. -> Void) {
  77. if #available(iOS 14, *) {
  78. // The iOS 14 API is used to prompt users for permission if they previously provided limited
  79. // access, but have now taken an additional screenshot.
  80. guard PHPhotoLibrary.authorizationStatus(for: .readWrite) != .authorized else {
  81. completionHandler(true)
  82. return
  83. }
  84. PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
  85. completionHandler(status != .denied)
  86. }
  87. } else {
  88. guard PHPhotoLibrary.authorizationStatus() != .authorized else {
  89. completionHandler(true)
  90. return
  91. }
  92. PHPhotoLibrary.requestAuthorization { status in
  93. completionHandler(status != .denied)
  94. }
  95. }
  96. }
  97. @objc(captureProgrammaticScreenshot)
  98. public static func captureProgrammaticScreenshot() -> UIImage? {
  99. // TODO: Explore options besides keyWindow as keyWindow is deprecated.
  100. let layer = UIApplication.shared.keyWindow?.layer
  101. if let layer {
  102. let renderer = UIGraphicsImageRenderer(size: layer.bounds.size)
  103. let image = renderer.image { ctx in
  104. layer.render(in: ctx.cgContext)
  105. }
  106. return image
  107. }
  108. return nil
  109. }
  110. }