nathantannar4 / InputBarAccessoryView

A simple and easily customizable InputAccessoryView for making powerful input bars with autocomplete and attachments
MIT License
1.17k stars 229 forks source link

Library crashes app when adding an attachment when an AttachmentManager dataSource is defined #237

Closed sgraesser17 closed 1 year ago

sgraesser17 commented 2 years ago

Describe the bug My app crashes on iOS 15.5 when adding attachments when an AttachmentManager dataSource is defined

To Reproduce Add the following code to the CommonTableViewController.swift file After line 29:

        manager.dataSource = self

After line 250

extension CommonTableViewController: AttachmentManagerDataSource {
    func attachmentManager(_ manager: AttachmentManager, cellFor attachment: AttachmentManager.Attachment, at index: Int) -> AttachmentCell {
        let indexPath = IndexPath(row: index, section: 0)

        // Only images are supported by default
        switch attachment {
        case .image(let image):
            guard let cell = manager.attachmentView.dequeueReusableCell(withReuseIdentifier: ImageAttachmentCell.reuseIdentifier, for: indexPath) as? ImageAttachmentCell else {
                fatalError()
            }
            cell.attachment = attachment
            cell.indexPath = indexPath
            cell.manager = manager
            cell.imageView.image = image
            return cell

        default:
            return manager.attachmentView.dequeueReusableCell(withReuseIdentifier: AttachmentCell.reuseIdentifier, for: indexPath) as! AttachmentCell
        }
    }
}

Compile and run the example app. Select the Slack row in the InputAccessoryView section Tap on the photo library button Select a photo App crashes at line 205 in AttachmentManager.swift file with a "Fatal error: Index out of range"

Expected behavior I expected the photo to be added as an attachment

Screenshots None

Environment

Additional context Replace line 205 with the following code:

        if indexPath.row == attachments.count && showAddAttachmentCell {
            return nil
        }

        let attachment = attachments[indexPath.row]
        if let customSize = self.dataSource?.attachmentManager(self, sizeFor: attachment, at: indexPath.row){
nathantannar4 commented 2 years ago

I cannot reproduce a crash on the example project.

Replace line 205 with the following code:

I'm not sure how you are returning nil since the function expects a non optional.

sgraesser17 commented 2 years ago

Tested my changes again with Xcode 13.4.1 on a iPhone 8 simulator running iOS 15.5. The sample app still crashes.

I am using the 5.5 release of InputBarAccessoryView. Haven't tested with release 6.0 or 6.1.

You are correct in that returning nil will not work. It should have been returning .zero

I am attaching the changes I made to the InputBarAccessoryView files for you to compare with. Archive.zip

nathantannar4 commented 2 years ago

Does not crash for me. Please clearly list all reproduction steps.

sgraesser17 commented 2 years ago

Don't know what else to tell you. The app crashes every time for me. The index path row is 1 and the attachments array has only 1 item in it. Since showAddAttachmentCell is true, the collection view has increased in the number of items in the section by 1. But, the collection view isn't checking if the last cell in the section is for the add attachment cell. The method is automatically assuming the index path row is smaller than the attachment array size.

Screen Shot 2022-10-06 at 11 27 23 AM
mmdock commented 1 year ago

got the same crash. my team is using messagekit on version 3.8.0 (just updated to this version in our latest release). Now, it looks like we need to revert back to 3.7.0. the update to inputbaraccessoryview 5.5 caused this for us as well.

Kaspik commented 1 year ago

@mmdock 3.8.0 is quite old too, did you try 4.0.0? Any hint or idea how to repro or when it happens? Did you repro in the example project?

monstermac77 commented 1 year ago

While trying to change the delete button (it's currently just the character x) on the attachment view (issue can be found here https://github.com/nathantannar4/InputBarAccessoryView/issues/16), I also hit this issue. Using version 6.2.0 which comes with MessageKit 4.1.1. Errored on the same line. This makes it effectively impossible to customize any of the AttachmentCells, which is a pretty major issue. I think @sgraesser17's steps to reproduce are pretty clear. Did you guys figure out a workaround @mmdock?

mmdock commented 1 year ago

@monstermac77 @Kaspik @nathantannar4 still having this crash while trying to update to 6.2.0 (messagekit 4.1.1). I can't add any attachment.

the issue seems to come from

  if let customSize = self.dataSource?.attachmentManager(self, sizeFor: self.attachments[indexPath.row], at: indexPath.row){
        return customSize
    }

it looks like the row value is 1 greater than the actual number of attachments on inspection

monstermac77 commented 11 months ago

Thanks so much for fixing this with 6.3.0! Been working great for us.