I need to upload images and pdf files so i am using HSAttachmentPicker framework with this i am able to open picker options and able to upload image and pdf in iPhone but its crashing in iPad when i click upload button
error:
You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'
but HSAttachmentPicker
is a framework so where should i add code for iPad
code: this is the code i have written in my upload image class
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))
}
}
}
here is the code for showAttachmentMenu
in HSAttachmentPicker framework file: here where to add code for iPad.. please guide.
- (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];
}