Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions FirebaseAppDistribution/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased
- [fixed] Prevented in-app feedback screenshot retrieval from crashing when no screenshot is
available in the user's photo library.

# 10.6.0
- [fixed] Fixed bug where testers were sent to the wrong URL when signing in (#10772).

Expand Down
41 changes: 29 additions & 12 deletions FirebaseAppDistributionInternal/Sources/InAppFeedback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,43 @@ import UIKit
@objc(getManuallyCapturedScreenshotWithCompletion:)
public static func getManuallyCapturedScreenshot(completion: @escaping (_ screenshot: UIImage?)
-> Void) {
getPhotoPermissionIfNecessary(completionHandler: { authorized in
getManuallyCapturedScreenshot(
requestPermission: getPhotoPermissionIfNecessary,
fetchAssets: {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.predicate = NSPredicate(
format: "(mediaSubtype & %d) != 0",
PHAssetMediaSubtype.photoScreenshot.rawValue
)
return PHAsset.fetchAssets(with: .image, options: fetchOptions)
},
completion: completion
)
}

static func getManuallyCapturedScreenshot(requestPermission: (@escaping (Bool) -> Void)
-> Void,
fetchAssets: @escaping () -> PHFetchResult<
PHAsset
>,
completion: @escaping (UIImage?) -> Void) {
requestPermission { authorized in
guard authorized else {
completion(nil)
return
}

let manager = PHImageManager.default()

let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.predicate = NSPredicate(
format: "(mediaSubtype & %d) != 0",
PHAssetMediaSubtype.photoScreenshot.rawValue
)
let fetchResult = fetchAssets()
guard fetchResult.count > 0 else {
completion(nil)
return
}

let manager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true

let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

manager.requestImage(
for: fetchResult.object(at: 0),
// TODO: Identify the correct size.
Expand All @@ -82,7 +99,7 @@ import UIKit
// TODO: Add logic to respond correctly if there's an error.
completion(image)
}
})
}
}

static func getPhotoPermissionIfNecessary(completionHandler: @escaping (_ authorized: Bool)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Photos
import XCTest

@testable import FirebaseAppDistributionInternal

final class InAppFeedbackTests: XCTestCase {
func testGetManuallyCapturedScreenshotWithNoScreenshotsCompletesWithNil() {
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [], options: nil)
let completionExpectation = expectation(description: "No screenshot is returned")

InAppFeedback.getManuallyCapturedScreenshot(
requestPermission: { completion in completion(true) },
fetchAssets: { fetchResult }
) { screenshot in
XCTAssertNil(screenshot)
completionExpectation.fulfill()
}

wait(for: [completionExpectation], timeout: 1)
}
}