ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
12.06k stars 1k forks source link

bug: CapacitorHttp plugin issue with iOS (every other call). Works with Android/Web app #6789

Closed thevariousartist closed 1 year ago

thevariousartist commented 1 year ago

Bug Report

Capacitor Version

Latest Dependencies:

@capacitor/cli: 5.2.2 @capacitor/core: 5.2.2 @capacitor/android: 5.2.2 @capacitor/ios: 5.2.2

Installed Dependencies:

@capacitor/cli: 5.2.2 @capacitor/core: 5.2.2 @capacitor/android: 5.2.2 @capacitor/ios: 5.2.2

[success] iOS looking great! 👌

Platform(s)

iOS

Current Behavior

Every other call to backend web service fails.

For the unsuccessful call, I see this error after the failure: ... (NSURLErrorDomain error -1000.)","message":"The operation couldn’t be completed"

For the successful call, I see this just before the call (not sure if this affects the next web service call): ... [NSURLSession sharedSession] may not be invalidated

Expected Behavior

In iOS I Expect the web service call to succeed every time, rather than "every other time". In Android it works every time. In Web App it works every time.

Code Reproduction

try {
   const url = 'https://example.com/my/api';
   const options: HttpOptions = { url: url };
   const httpResponse: HttpResponse = await CapacitorHttp.get(options);

} catch (error) { // ERROR in iOS only (every other call): // (NSURLErrorDomain error -1000.)","message":"The operation couldn’t be completed" }

Other Technical Details

Ionic:

Ionic CLI : 7.1.1 (/Users/me/.nvm/versions/node/v18.16.1/lib/node_modules/@ionic/cli) Ionic Framework : @ionic/angular 7.2.0 @angular-devkit/build-angular : 16.1.5 @angular-devkit/schematics : 16.1.5 @angular/cli : 16.1.5 @ionic/angular-toolkit : 9.0.0

Capacitor:

Capacitor CLI : 5.2.2 @capacitor/android : 5.2.2 @capacitor/core : 5.2.2 @capacitor/ios : 5.2.2

Utility:

cordova-res : not installed globally native-run : 1.7.2

System:

NodeJS : v18.16.1 (/Users/me/.nvm/versions/node/v18.16.1/bin/node) npm : 9.5.1 OS : macOS Unknown

npm --version output: 9.5.1

node --version output: v18.16.1

pod --version output (iOS issues only): 1.12.1

Additional Context

const config: CapacitorConfig = { plugins: { CapacitorHttp: { enabled: false, // tried both FALSE and TRUE, but get same result } }, webDir: 'www' };

Tried the above config with and without but it made no difference to the result: iosScheme: 'https'

Backend web services are all "https" and have been working fine for years under Angular HTTP, Cordova HTTP plugins, etc.

Various links that seem relevant or are my other requests for help: https://forum.ionicframework.com/t/capacitorhttp-ios-error/231416 https://stackoverflow.com/questions/76764952/issue-with-capacitorhttp-on-ios https://forum.ionicframework.com/t/capacitorhttp-ios-error/231416/6

ionitron-bot[bot] commented 1 year ago

This issue needs more information before it can be addressed. In particular, the reporter needs to provide a minimal sample app that demonstrates the issue. If no sample app is provided within 15 days, the issue will be closed. Please see the Contributing Guide for how to create a Sample App. Thanks! Ionitron 💙

thevariousartist commented 1 year ago

Figured it out, though I'm not sure why it worked 50% of the time without this for iOS, nor why it always worked for Android/web.

The line of code I was missing is simply this:

url = encodeURI(url);

Annoyingly, I had this line for the Cordova plugin I used before Capacitor. Anyway, I hope this helps someone out there who may run into the same issue.

Maybe this might be a curiosity item for the developers of the Capacitor plugin as to why iOS worked every other call. I would have expected it to always fail or always work.

jcesarmobile commented 1 year ago

You shouldn't need to use encodeURI(url) for the plugin to work. If you can provide a sample app we can take a look and try to fix it, if not, the issue will auto close in two weeks.

thevariousartist commented 1 year ago

I know, it surprised me but I was able to prove that it made a difference by running the same test repeatedly with consistent results as indicated in my opening comment. If I removed that statement, then only iOS would start to exhibit issues, with the pattern of success, fail, success, fail... No such issues occur with Android nor the web app.

I can appreciate that there might be something else going on, maybe some other setting or plugin that is interfering with the behavior on iOS, but at this point, having spent a lot of time troubleshooting this, I now have a very stable app simply by including that statement.

I'll see if I can put together a sample app, but I have a lot on my plate and not much bandwidth (especially due to losing time on this).

For what it's worth, I do have a couple of remaining Cordova plugins in my app (network and device) that I have yet to replace with Capacitor (I was having issues with the Device plugin for Capacitor -- on Android only!).

Ionitron commented 1 year ago

It looks like this issue didn't get the information it needed, so I'll close it for now. If I made a mistake, sorry! I am just a bot.

Have a great day! Ionitron 💙

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.