웹뷰에 이미지를 보내야한다...
웹뷰 이미지 첨부
채팅방에서 사진 첨부하는 기능이 추가되었다.
JSON 객체로 보내고 싶었지만 결국 실패..
안되는게 맞다고 한다.
Swift에 맞게 보내주면 웹에서 못받아오고
웹에 맞춰서 보내주려니 앱에서 보낼 때 에러나고.
그냥 문자열은 잘 보내진다.
let aaa = String(repeating: "a", count: 1000)
짤라서 보내도된다.
base64String = String(base64String[base64String.startIndex..<base64String.index(base64String.startIndex, offsetBy: 100)])
100자 정도 짤라서 보내면 되는데 full string 은 에러나길래 너무 길어서 문제인가 했더니 그건 아니고
에러나서 base64String 이 문제인가
다 변환 해줘야댐
base64String = base64String.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "'", with: "\\'")
.replacingOccurrences(of: "\"", with: "\\\"")
.replacingOccurrences(of: "\n", with: "\\n")
.replacingOccurrences(of: "\r", with: "\\r")
.replacingOccurrences(of: "+", with: "\\+")
.replacingOccurrences(of: "=", with: "\\=")
.replacingOccurrences(of: "/", with: "\\/")
결국 base64String 통 문자열로 그대로 보내줌.
"""
{"action": "setLibraryImage","data": {\"name\":\"\(fileName)\",\"imageString\":\"\(base64String)\"}}
"""
JSON 변환하면 안됨. jsonString
Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=1, WKJavaScriptExceptionMessage=SyntaxError: Unexpected EOF, WKJavaScriptExceptionColumnNumber=0, WKJavaScriptExceptionSourceURL=http://172.30.1.34:3000/counseling/64835271, NSLocalizedDescription=A JavaScript exception occurred}
SyntaxError: JSON Parse error: Expected ‘}’
{"action": "setLibraryImage","data": “{\”name\":\"9385979AC16542B8AC1E68BB64A5E651.jpeg\",\"imageString\":\"9j4Qo0RXhpZgAA\”}”}
“” 때문에 에러난거같음
빼주니까
SyntaxError: JSON Parse error: Unterminated string
아몰라 다시 객체 JSON Encoder로 돌려서 넣으니깐 잘 됨 ㅡㅡ
근데 base64String 전체 다 보내면 다음 에러 뜸
SyntaxError: JSON Parse error: Invalid escape character +
guard let encodedBase64String = base64String.addingPercentEncoding(withAllowedCharacters: .alphanumerics) else {
print(" === encoding error")
return
}
이런식으로 base64String.addingPercentEncoding 이거 해주니까 잘 들어감
근데 이미지 안뜸 ^^
encoder.outputFormatting = .prettyPrinte 넣으면 다음 에러
Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=1, WKJavaScriptExceptionMessage=SyntaxError: Unexpected EOF, WKJavaScriptExceptionColumnNumber=0, WKJavaScriptExceptionSourceURL=http://172.30.1.34:3000/counseling/64835271, NSLocalizedDescription=A JavaScript exception occurred}
SyntaxError: JSON Parse error: Unrecognized token '/'
Thread 1: "*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write"
'초보 iOS 개발자의 일상 > 개발 업무' 카테고리의 다른 글
비동기 Task 병렬 실행 (TaskGroup vs. async let) (0) | 2024.12.09 |
---|---|
sendbird 연동 (0) | 2024.09.24 |
[iOS] 웹뷰 파일 및 사진 첨부 구분하기 (0) | 2024.08.19 |
프로젝트 별 spm 설정 초기화 (설정값 주입? or 환경변수 설정?) (0) | 2024.07.23 |
Unlisted app distribution 등록되지 않은 앱 배포 (+ Enterprise 배포를 하지 못한 이유) (0) | 2024.07.22 |