iOS UIActivityViewController image sharing fails with Gmail - HTTP 400 error

I’m having trouble sharing images through UIActivityViewController when using Gmail and other Google apps on iOS. The native Mail app works perfectly, but Gmail throws this error:

Unable to attach the operation couldn’t be completed.(com.google.HTTPStatus error 400.)

Here’s my current implementation:

extension UIView {
    func captureAsImage() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.frame.size, self.isOpaque, 0.0)
        self.drawHierarchy(in: self.frame, afterScreenUpdates: true)
        let capturedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return capturedImage!
    }
}

@objc func initiateImageShare() {
    let capturedImage = targetView.captureAsImage()
    
    guard capturedImage != nil else {
        print("Failed to capture image")
        return
    }
    
    let shareController = UIActivityViewController(activityItems: [capturedImage], applicationActivities: nil)
    
    shareController.excludedActivityTypes = [.addToReadingList, .postToTwitter]
    
    if let popover = shareController.popoverPresentationController {
        popover.sourceView = self.view
        popover.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.maxY, width: 0, height: 0)
        popover.permittedArrowDirections = []
    }
    
    self.present(shareController, animated: true, completion: nil)
}

What I’ve tried:

  • The view-to-image conversion works fine
  • Native iOS Mail handles the sharing without issues
  • Only Google services (Gmail, Google Drive, etc.) show this HTTP 400 error

Has anyone encountered this before? What could be causing Gmail to reject the image attachment while Mail accepts it just fine?

totally agree, i had the same issues! doing the temp file thing def helps. google wants file paths, not just images. just save it to a temp location, then share that. way easier for it to work!

This is a data format issue - Google apps are way pickier about this stuff. Had the same problem last year. The issue is how image data gets passed to third-party apps vs native iOS apps. Mail can handle raw UIImage objects no problem, but Gmail wants properly formatted data with correct MIME types and metadata. Convert your UIImage to NSData with specific compression settings before adding it to the activity items array. Try UIImageJPEGRepresentation(capturedImage, 0.8) instead of the raw UIImage object. Google services validate data format way more strictly than Apple’s native apps - that’s why you’re getting the HTTP 400 error with Gmail but not Mail.

Been there. Gmail throws HTTP 400 errors because it validates file metadata differently than iOS Mail. Google apps are picky about file headers and MIME types.

Skip the manual file conversions and temp directories - just automate it. Set up a workflow that captures images, formats them for different platforms, and handles sharing automatically.

I’ve built stuff like this where the automation detects which app you’re sharing to and formats accordingly. Gmail gets JPEG with specific compression and metadata, Mail takes the raw format. No more failed shares.

The workflow handles fallbacks too. Gmail fails? It tries other formats or different sharing methods.

Saves me tons of debugging time and users get smooth sharing across all platforms. Set it once, forget about platform-specific sharing headaches.