적당한 고통은 희열이다

- 댄 브라운 '다빈치 코드' 중에서

초보 iOS 개발자의 일상/개발 업무

[Swift iOS] Alamofire 5 upgrade changes

hongssup_ 2021. 6. 8. 15:12
반응형

카카오 공유하기 기능을 위해 KakaoSDK 모듈을 추가하면서 많은 에러가 발생했다. 

 

1. 터미널에서 라이브러리 설치 문제

coapods issue : None of your spec sources contain a spec satisfying the dependency

 

arch -x86_64 pod install 

[!] CocoaPods could not find compatible versions for pod "Alamofire":

In snapshot (Podfile.lock):

Alamofire (= 4.9.1, ~> 4.9, ~> 4.9.1)

 

In Podfile:

Alamofire (~> 4.9.1)

 

AlamofireImage was resolved to 3.6.0, which depends on

Alamofire (~> 4.9)

 

KakaoSDKUser was resolved to 2.0.0, which depends on

KakaoSDKCommon (= 2.0.0) was resolved to 2.0.0, which depends on

Alamofire (~> 5.1)

 

CocoaPods could not find compatible versions for pod "KakaoSDKAuth":

In Podfile:

KakaoSDKAuth

 

KakaoSDKUser was resolved to 2.0.0, which depends on

KakaoSDKAuth (= 2.0.0)

 

CocoaPods could not find compatible versions for pod "KakaoSDKUser":

In Podfile:

KakaoSDKUser

 

There are only pre-release versions available satisfying the following requirements:

 

'KakaoSDKUser', '>= 0'

 

You should explicitly specify the version in order to install a pre-release version

 

카카오 SDK를 사용하려면 5.1 버전 이상의 Alamofire가 필요하다고 하여

기존에 '~> 4.9.1' 설정이 되어 있던 Alamofire 버전을 5.1 올려주었다

 

pod deintegrate - pod install (Alamofire 버전 ~> 5.1로 바꿔줌)

[!] CocoaPods could not find compatible versions for pod "Alamofire":

  In snapshot (Podfile.lock):

    Alamofire (= 5.1, ~> 4.9, ~> 4.9.1)

 

  In Podfile:

    Alamofire

 

None of your spec sources contain a spec satisfying the dependencies: `Alamofire, Alamofire (= 5.1, ~> 4.9, ~> 4.9.1)`.

 

You have either:

 * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.

 * mistyped the name or version.

 * not added the source repo that hosts the Podspec to your Podfile.

 

하라는 대로 pod repo update 해줬는데도 에러... 

 

Looking for related issues on cocoapods/cocoapods...

 - CocoaPods 1.10 OneSignal MacAir M1 Issue

   https://github.com/CocoaPods/CocoaPods/issues/10684

 

이렇게 알려주는데 또 M1 이슈인거니..

막 다 찾아보고 하라는 거 다 해도 계속 에러 뜸 ㅠㅠㅠ 

그러다가 Podfile.lock 파일이 문제라면 그냥 그 파일 지우고 다시 pod install 하라는 댓글 보고 

Podfile.lock 파일 지우고 다시 설치하니깐... 된다..ㅠㅠㅠ ㅋㅋㅋㅋㅋㅋ 휴 

참고 : StackOverflow_CocoaPods subspec issue

 

2. Alamofire 버전 업그레이드 문제

Alamofire 버전을 올렸더니 Xcode 프로젝트에서는 다음과 같은 에러가 발생했다. 

Module 'Alamofire' has no member named 'request'

Alamofire.request(...) -> AF.request(...) 로 변경해준다. 

 

그리고 많은 시간을 잡아먹었던 대망의 Alamofire upload multipart form data 

기존의 코드로 실행했더니 다음과 같은 에러들이 발생했다. 

Argument 'to' must precede argument 'usingThreshold'

: 이거는 그냥 fix 누르면 알아서 순서 바꿔줌

'_' can only appear in a pattern or on the left side of an assignment

Pattern cannot match values of type 'URLRequest'

: 이 두 에러는 버전 5에서 upload multipartFormData를 사용하는 방식이 아예 바뀌어서 뜨는 에러라 새로운 버전에 맞게 코드를 수정해주어야 한다. 사용을 좀 더 간편하게 하기 위해 버전 업그레이드를 했다지만, 기존 코드를 바꿔야 하는 사람은 넘나 귀찮고 짱나는 것 ㅎㅎ  

속으로 욕은 좀 했지만 고치고 보니 좀 더 간결해지긴 했구만. 사람들마다 구현 방법이 조금씩 다르고 결과값을 전달해주어야 하는 형식들이 달라 예제들 다 찾아서 살펴보면서 짜집기하고 수정하느라 애먹었다. ㅎㅎㅎ 

가장 큰 변화라면 굳이 결과값을 upload.responseJSON으로 받거나 에러를 출력할 일이 없이 response 값이 자동으로 upload 된다는 점인 것 같다. 

// Alamofire 4

Alamofire.upload(multipartFormData: { (multipartFormData) in
    if let param = parameters {
        for (key, value) in param {
            multipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
        }
    }
}, usingThreshold: UInt64.init(), to: url!, method: .post, headers: hTTPHeaders) { (result) in
    
    switch result {
    case .success(let upload, _, _):
        upload.responseJSON { (responseData) in
            switch responseData.result {
            case .success(let successData):
                response(true,successData)
                break
            case .failure(let error):
                response(false,error)
                break
            }
        }
        break
    case .failure(let error):
        response(false, error)
        break
    }
}
//Alamofire 5

AF.upload(multipartFormData: { (multipartFormData) in
    if let param = parameters {
        for (key, value) in param {
            multipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
        }
    }
}, to: url!, usingThreshold: UInt64.init(), method: .post, headers: hTTPHeaders).responseJSON { response in
    
    switch response.result {
    case .success(let successData):
        result(true,successData)
        break
    case .failure(let error):
        result(false, error)
        break
    }
}

 

참고 : Alamofire Usage Github

 

Alamofire/Alamofire

Elegant HTTP Networking in Swift. Contribute to Alamofire/Alamofire development by creating an account on GitHub.

github.com

https://i-colours-u.tistory.com/3

 

[Cocoapod] Alamofire 4에서 5 차이점 및 오류처리 방법!

안녕하세요! 이번에는 Alamofire 5로 업데이트하게 되면서 겪었던 오류들에 대해서 간단하게 설명을 해보려고 합니다. 사건의 발단은 KakaoSDK를 도입해서 카카오링크 기능을 사용 하기 위해서 다음

i-colours-u.tistory.com

https://stackoverflow.com/questions/64547522/issue-with-af-upload-pattern-cannot-match-values-of-type-urlrequest

 

Issue with AF.upload "Pattern cannot match values of type URLRequest”

I am not able to debug this error in handling response of multipartFormData of AF.upload. The error 'Pattern cannot match values of type "URLRequest" shows up on the 2 case statements suc...

stackoverflow.com

https://nsios.tistory.com/39

 

[Swift] - MultiPart통신 (멀티파트 이미지업로드)

안녕하세요😁😁 body와 이미지를 같이통신을 하는 예제가 많이 없어서 정리해봤어요 이번 게시물은 통신을 하는데 이미지를 올릴 때... 어떻게 올리지?? 해본적이 있을거에요 그 방법에 대해서

nsios.tistory.com

 

728x90
반응형