Hey developers, I have a KMM project. I have imple...
# multiplatform
a
Hey developers, I have a KMM project. I have implemented
KTOR
for network request. My android version is working completely fine. But when I run the iOS app, it gives the
kotlin.IllegalStateException: Invalid url
error. Do you know for which reason it might occur? The URL is fine as it's working on Android side.
m
Is it a secure URL? ios does not accept those connections out of the blue, similar to android, you need to enable it
a
Yes, the connection is secure. It’s an HTTPS connection. And sorry, but I didn’t get the enabled thing. Where should I enable? In android we are just adding internet permission. Is there something we need to do in plist file too?
m
I meant unsafe connections. Check here: https://developer.apple.com/news/?id=jxky8h89.
Basically add
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "<http://www.apple.com/DTDs/PropertyList-1.0.dtd>">
<plist version="1.0">
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</plist>
and try again
a
@McEna Thank you so much for your response. This doesn’t solve my problem. May be secure connection is not the problem in this case. It’s giving below error while running iOS app.
Copy code
REQUEST <https://myApiDomainHere.com> failed with exception: kotlin.IllegalStateException: Invalid url: <https://myApiDomainHere.com>
Here, actual domain is replaced with
<https://myApiDomainHere.com>
in error message pasted.
p
So there is no trailing slash in the url? Have you tried it with
<https://myApiDomainHere.com/>
a
No, there is no trailing slash as tha path of API is being added on call site The final url becomes something like
https://myApiDomainHere.com/api/v1/history
p
So does your error message also have that path at the end or is it already missing in the error? Does it look like this?
Copy code
kotlin.IllegalStateException: Invalid url: <https://myApiDomainHere.com/api/v1/history>
Because to me it feels like there is something invalid in the URL string itself, like some sort of invalid character that's not encoded properly. I think the error is thrown before any network request is made because on iOS the framework already consideres the URL invalid.
a
Yes, the error message also have the full path at the end. The URL doesn’t have any special character in it. It’s all alphabet except the v1 part
p
I assume you're using ktor-client-darwin. So You can take a look at the source code where the exception seems to come from: https://github.com/ktorio/ktor/blob/2.3.7/ktor-client/ktor-client-darwin/darwin/src/io/ktor/client/engine/darwin/internal/DarwinUrlUtils.kt#L62 (Assuming you're using ktor-client-darwin version 2.3.7) There has to be some hidden error I can't see, but you could try to analyze around this in your iosMain or iosTest sourceSet by duplicating this otherwise internal function into your code and play around with it in more detail.
(the #ktor channel also exists here on slack)
a
Thanks a lot for your response and detailed information. For testing I have just created an iOS native app with Swift and SwiftUI. In that project, this seems working fine and another thing I have noticed, as you said in your answer hint, it's automatically adding a trailing slash like below
<https://myApiDomainHere.com/api/v1/history/>
Whereas, in Ktor, there is no trailing slash being added. Don't know whether this is the reason or not, but I will try to investigate further with your provided lead. Thanks a lot!
And yeas, for iOS side, I'm using Darwin engine
101 Views