0

I'm trying to send data from an application Source to application Target. Everything works fine except the image part. When I encode the image as base64 and send it, the base64 code is truncated. I'm only getting the first 1024 characters.

Here is the code from Source application:

let uiimage = UIImage(data: selectedImageData!)
let uiimagebASE64 = uiimage!.jpegData(compressionQuality: 1)?.base64EncodedString() ?? ""

let strURL = "targetapp://x-callback-url/translate?x-success=sourceapp://x-callback-url/acceptTranslation&x-source=SourceApp&x-error=sourceapp://x-callback-url/translationError&word=Hello&username=\(username)&isPrivate=\(isPrivate)&notificationsEnabled=\(notificationsEnabled)&previewIndex=\(previewIndex)&version=\(version)&selectedImage=\(uiimagebASE64)"

if let encoded = strURL.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
   let myURL = URL(string: encoded) {
    print(myURL)
                        
    if (UIApplication.shared.canOpenURL(myURL)){
        UIApplication.shared.open(myURL, options: [:], completionHandler: nil)
    }
}

The code from target app :

func handleURL(_ url: URL) {
    let parameters = url.queryParameters
    let dataString = (parameters!["selectedImage"]!)
    let dataStringDecoded = dataString.removingPercentEncoding! 
    
    let imageData = Data(base64Encoded: dataString)
    let image = UIImage(data: imageData!)
    ...

I even deleted addingPercentEncoding in the source app and removingPercentEncoding in the target app. I'm getting the same problem.

How can I fix this problem? Is there a best solution to send image data from application A to B?

HangarRash
  • 7,314
  • 5
  • 5
  • 32
tamtoum1987
  • 1,957
  • 3
  • 27
  • 56
  • 1
    That's a lot of data to stuff in a URL query param. I doubt you can send an image that way. There may also be invalid characters in the base 64 string. – Rob C Apr 27 '23 at 03:39
  • 1
    Since you seem to be in control of both the sending and receiving app, I'm thinking using App Groups to share common file storage should work for you. Then your URL can point to the file for the receiving app to open. [This answer](https://stackoverflow.com/a/69560956/15280114) might help. – Chip Jarred Apr 27 '23 at 05:03
  • https://stackoverflow.com/questions/8577175/ios-url-scheme-length might interest you. Use another way to pass data. – Larme Apr 27 '23 at 08:12
  • @ChipJarred the two application must not have signed with the same group id (the same signature) – tamtoum1987 Apr 27 '23 at 12:08
  • @tamtoum1987, maybe you can use a named UIPasteboard, and send a custom URL to the target app to tell it to check the pasteboard for the data. The main problem I foresee with this approach is the user notifications when the target app tries to get pasteboard data that originated outside of itself. – Chip Jarred Apr 28 '23 at 14:48

0 Answers0