我需要上傳影像和 pdf 檔案,所以我使用HSAttachmentPicker框架,我可以打開選擇器選項并能夠在 iPhone 中上傳影像和 pdf,但是當我單擊上傳按鈕時它在 iPad 中崩潰
錯誤:
您必須通過警報控制器的 popoverPresentationController 提供此彈出視窗的位置資訊。您必須提供 sourceView 和 sourceRect 或 barButtonItem。如果在您呈現警報控制器時不知道此資訊,您可以在 UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation 中提供它。
但HSAttachmentPicker它是一個框架,所以我應該在哪里為 iPad 添加代碼
代碼:這是我在上傳圖片類中撰寫的代碼
let picker = HSAttachmentPicker()
@IBAction func uploadButtonTapped(_ sender: UIButton) {
picker.delegate = self
picker.showAttachmentMenu()
}
extension PostEnquiryViewController: HSAttachmentPickerDelegate {
func attachmentPickerMenu(_ menu: HSAttachmentPicker, showErrorMessage errorMessage: String) {
}
func attachmentPickerMenuDismissed(_ menu: HSAttachmentPicker) {
}
func attachmentPickerMenu(_ menu: HSAttachmentPicker, show controller: UIViewController, completion: (() -> Void)? = nil) {
self.present(controller, animated: true, completion: completion)
}
func attachmentPickerMenu(_ menu: HSAttachmentPicker, upload data: Data, filename: String, image: UIImage?) {
if !data.isEmpty && (filename as NSString).pathExtension == "pdf" {
self.attachmentFiles.append(.init(data: data, fileName: filename))
self.attachmentImages.append(.init(image: UIImage(named: "pdf")!, imageName: filename))
self.uploadFileImage.image = UIImage(named: "pdf")
}
if let img = image {
DispatchQueue.main.async {
self.uploadFileImage.image = img
}
self.attachmentImages.append(.init(image: img, imageName: filename))
}
}
}
這是showAttachmentMenuHSAttachmentPicker 框架檔案中的代碼: here where to add code for iPad.. 請指導。
- (void)showAttachmentMenu {
self.selfReference = self;
UIAlertController *picker = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
NSString *showPhotosPermissionSettingsMessage = [NSBundle.mainBundle objectForInfoDictionaryKey:@"NSPhotoLibraryUsageDescription"];
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] && showPhotosPermissionSettingsMessage != nil) {
UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:[self translateString:@"Take Photo"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if (@available(iOS 14.0, *)) {
[self validatePhotosPermissionsWithAccessLevel:PHAccessLevelAddOnly completion:^{
[self showImagePicker:UIImagePickerControllerSourceTypeCamera];
}];
} else {
[self validatePhotosPermissions:^{
[self showImagePicker:UIImagePickerControllerSourceTypeCamera];
}];
}
}];
[picker addAction:takePhotoAction];
}
if (showPhotosPermissionSettingsMessage != nil) {
if (@available(iOS 14.0, *)) {
// the app already has access to the Photo Library so we're safe to add `Use Last Photo` here
if ([PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite] == PHAuthorizationStatusAuthorized) {
UIAlertAction *useLastPhotoAction = [UIAlertAction actionWithTitle:[self translateString:@"Use Last Photo"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self useLastPhoto];
}];
[picker addAction:useLastPhotoAction];
}
} else {
UIAlertAction *useLastPhotoAction = [UIAlertAction actionWithTitle:[self translateString:@"Use Last Photo"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self validatePhotosPermissions:^{
[self useLastPhoto];
}];
}];
[picker addAction:useLastPhotoAction];
}
}
if (showPhotosPermissionSettingsMessage != nil) {
UIAlertAction *chooseFromLibraryAction = [UIAlertAction actionWithTitle:[self translateString:@"Choose from Library"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if (@available(iOS 14, *)) {
// don't request access to users photo library since we don't need it with PHPicker
[self showPhotoPicker];
} else {
[self validatePhotosPermissions:^{
[self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}];
}
}];
[picker addAction:chooseFromLibraryAction];
}
UIAlertAction *importFileFromAction = [UIAlertAction actionWithTitle:[self translateString:@"Import File from"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self showDocumentPicker];
}];
[picker addAction:importFileFromAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:[self translateString:@"Cancel"] style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self dismissed];
}];
[picker addAction:cancelAction];
[self.delegate attachmentPickerMenu:self showController:picker completion:nil];
}
uj5u.com熱心網友回復:
閱讀演示文稿是個好主意popover:Docs
如果您查看HSAttachmentPicker提供的示例中的視圖控制器,您將看到示例應用程式如何處理它。是的,它在 Objective-C 中......但在 Swift 中幾乎相同:
func attachmentPickerMenu(_ menu: HSAttachmentPicker, show controller: UIViewController, completion: (() -> Void)? = nil) {
// popover will be non-nil if we're on an iPad
if let popover = controller.popoverPresentationController {
// we want the popover arrow pointing to the button
popover.sourceView = self.uploadButton
}
self.present(controller, animated: true, completion: completion)
}
因此,這是一個完整的、可運行的示例:
import UIKit
class ViewController: UIViewController {
let uploadButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemYellow
uploadButton.setTitle("Tap to Upload", for: [])
uploadButton.setTitleColor(.white, for: .normal)
uploadButton.setTitleColor(.lightGray, for: .highlighted)
uploadButton.backgroundColor = .systemBlue
uploadButton.layer.cornerRadius = 8.0
uploadButton.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(uploadButton)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// let's put the button near the bottom of the screen
uploadButton.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -40.0),
uploadButton.widthAnchor.constraint(equalToConstant: 200.0),
uploadButton.heightAnchor.constraint(equalToConstant: 60.0),
uploadButton.centerXAnchor.constraint(equalTo: g.centerXAnchor),
])
uploadButton.addTarget(self, action: #selector(uploadButtonTapped(_:)), for: .touchUpInside)
}
@IBAction func uploadButtonTapped(_ sender: UIButton) {
let picker = HSAttachmentPicker()
picker.delegate = self
picker.showAttachmentMenu()
}
}
extension ViewController: HSAttachmentPickerDelegate {
func attachmentPickerMenu(_ menu: HSAttachmentPicker, showErrorMessage errorMessage: String) {
// Handle errors
}
func attachmentPickerMenuDismissed(_ menu: HSAttachmentPicker) {
// Run some code when the picker is dismissed
}
func attachmentPickerMenu(_ menu: HSAttachmentPicker, show controller: UIViewController, completion: (() -> Void)? = nil) {
// popover will be non-nil if we're on an iPad
if let popover = controller.popoverPresentationController {
// we want the popover arrow pointing to the button
popover.sourceView = self.uploadButton
}
self.present(controller, animated: true, completion: completion)
}
func attachmentPickerMenu(_ menu: HSAttachmentPicker, upload data: Data, filename: String, image: UIImage?) {
// Do something with the data of the selected attachment, i.e. upload it to a web service
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/514416.html
