React and Axios GET request issue with Safari on MacOs and iOs - ios

I'm trying to do an API request using Axios, it works in every windows' and Android browsers but does not work on MacOs Safari (but in Chrome it works fine) or any iOs browser.
In the same code I make another calls to the same API (different endpoints) and it works with no problems at all in safari and iOs.
When I console.log the throwed exception turns out that's a Network error.
I don't think that's a CORS issue, couse it works on another browsers.
My request:
try {
const response = await api.get(
"/precos/get",
{
headers: {
'Content-Type': 'application/json'
'token': getToken()
}
}
);
this.filtraProdutoPorTabela(response.data.data);
} catch (err){
// do something with the error
}
Console error:
Network Error
{"url":"http://url/precos/get","method":"get","headers":{"Accept":"application/json, text/plain, /","this.state-Type":"application/json","token”:”e…c”},”baseURL”:”the base url ”,”transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"httpsAgent":{}}}

The only problem that would cause this that I can think of is that you for sure have the links on your computer set to for example: http://localhost:5000/api/posts
And that works fine since your doing it in the same machine but when you try it on your mobile devices or any other device when the browser tries to hit that route it sees the localhost portion of the url and it points to it self not your actual machine.
To solve this problem you have to put the ip address and the port of your machine not the literal text "localhost".
From:
http://localhost:5000/api/posts
To:
http://192.168.1.23:500/api/posts
http://{ YOUR IP ADDRESS }:5000/api/posts

When investigating this issue on my own I came across this post:
link not working in Safari.
For some reason on Safari link event is not being triggered. I had my onClick action, that was triggering axios call, bind to html <a /> tag. Safari was not letting axios execute request properly.
Adding:
event.preventDefault()
to function called by tag when clicked solved the issue.

Related

Using Firebase Authentication from iOS 7.1.2 Safari

I see this error:
Error: This operation is not supported in the environment this
application is running on. "location.protocol" must be http, https or
chrome-extension and web storage must be enabled.
When I use:
firebase.auth().signInWithPopup(provider)
.then(function(result) {
console.log(result);
})
.catch(function(error) {
console.log('popup', error);
//webSettings.setDomStorageEnabled(true);
firebase.auth().signInWithRedirect(provider)
.then(function(result) {
console.log(result);
})
.catch(function(error) {
console.log('redirect', error);
firebase.auth().signInAnonymously().catch(function(error) {
console.log('anonymous', error);
});
});
});
The first two login attempts via popup and redirect fail. It seems to happen only on iOS Safari.
I see others reporting the issue with Cordova, but I don't see an answer and I'm only using web and firebase. Not Cordova or ionic etc.
The anonymous login works on iOS but that is only a test and not what we want to use to use.
If you want to test it you can use https://meetup-reporter.firebaseapp.com/ from Safari on iOS
An example dump of the error object from the returned Promise is:
{"code": "auth/operation-not-supported-in-this-environment",
"constructor": function (a, b)
{this.code="auth/"+a;this.message=b||Xf[a]||"";}, "F": function ()
{return{code:this.code,message:this.message}}, "line": 44, "message":
"This operation is not supported in the environment this application
is running on. \"location.protocol\" must be http, https or
chrome-extension and web storage must be enabled.", "sourceURL":
"https://meetup-reporter.firebaseapp.com//firebase/4.1.2/firebase-auth.js",
"stack":
"https://meetup-reporter.firebaseapp.com//firebase/4.1.2/firebase-auth.js:44:638\nhttps://meetup-reporter.firebaseapp.com//firebase/4.1.2/firebase-auth.js:45:258\nA#https://meetup-reporter.firebaseapp.com//firebase/4.1.2/firebase-auth.js:44:545\nD#https://meetup-reporter.firebaseapp.com//firebase/4.1.2/firebase-auth.js:45:242\nsignInWithPopup#https://meetup-reporter.firebaseapp.com//firebase/4.1.2/firebase-auth.js:241:48\na#https://meetup-reporter.firebaseapp.com/__/firebase/4.1.2/firebase-auth.js:260:432\nhttps://meetup-reporter.firebaseapp.com/scripts/main.js:430:36",
"toJSON": function () {var
a=Array.prototype.slice.call(arguments);a:{var
e=Array.prototype.slice.call(a);var l=0;for(var
n=!1,C=0;Cl||l>=fk.length)throw new N("internal-error","Argument
validator received an unsupported number of arguments.");e=fk[l]+"
argument "+(e.name?'"'+e.name+'" ':"")+"must be "+e.N+".";break
a}e=null}}if(e)throw new N("argument-error",d+" failed: "+e);return
b.apply(this,a);}}
signInWithRedirect actually works in Cordova now: https://firebase.google.com/docs/auth/web/cordova
signInWithPopup and signInWithRedirect should also work for iOS7 and up. I just tested both on an iOS 7 device and they both work. What you are experiencing is likely to the fact that you are using this operation in Safari Private/Incognito mode where web storage is disabled. These operations depend on web storage in order to securely communicate the OAuth result to the parent page. You will need to ask your users to switch to regular mode when this happens, or you can use the Google Sign-in JS SDk to get the OAuth credential and then signInWithCredential. I think it may work in incognito mode.

Chrome App webview does not allow access to MessageEvent source in inspector

I am using a webview in a Google Chrome app, and when I try to send a message to the webview from the parent page, it come through, but then I can't access the source property to send anything back. I have specified the origin to send to as the parent window origin, and it still comes through to the client, but when I try to inspect it in the client console, it throws an error.
webview.addEventListener('contentload', function() {
webview.contentWindow.postMessage('welcome', window.location.origin);
});
And in the webview:
window.addEventListener('message', function(event){
console.log(event);
});
It turned out that event.source.postMessage was still accessible, I just couldn't inspect it in the console.

How can I send a message to a custom Google Cast Receiver from Chrome for iOS?

I'm using the CastHelloText-chrome example and am able to get it all working just fine from the Chrome browser on my desktop, however, when I try to use Chrome on my iOS device, I receive the following error when trying to create a session:
{ "code": "channel_error", "description": "The operation couldn't be completed. (com.google.chrome.ios.cast error 2.)" }
This error happens when executing the following:
chrome.cast.requestSession(function(e) {
session = e;
session.sendMessage(namespace, message, onSuccess.bind(this, "Message sent: " + message), onError);
}, onError);
Specifically, this seems to happen on the requestSession function call (the nested callback never actually executes).
Everything up until this point seems to work. In fact, on my iOS device, I even receive the popup asking where to cast to (Chromecast) and once I select it, my Chromecast does indeed display my custom receiver. However, I still receive that error message and never get a session object, so I can't send messages to the receiver.
Like I said, this seems to work fine on my desktop Chrome browser, but does not work on my iOS Chrome browser. Does this sample not work on iOS devices? Is there a way to make it work?
Chrome on desktop and Chrome on mobile are not the same. You need to use the iOS API. Use the sample app for CastHelloText-ios as a reference.

HTTP Traffic monitoring issue when using MonoTouch, HttpClient and Charles Proxy

I'm new to HttpClient class and I'm having issue with monitoring requests using Charles Proxy. Basically what I need is to monitor the requests which are made either from simulator or actual iOS device. Here you can find a great tutorial of how to configure Charles for iOS development.
I was making simple HttpClient requests for instance, just a simple authorisation
async Task<string> authorizeUser()
{
HttpClient _client = new HttpClient ();
_client.BaseAddress = new Uri("https://...../api/");
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue ("bearer", token);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "theUserName"),
new KeyValuePair<string, string>("password", "thePassword")
});
var result = await _client.PostAsync("auth", content);
string resultContent = result.Content.ReadAsStringAsync().Result;
return resultContent;
}
The code works, the user is being authorised, and the bearer token is being returned. But what was the issue, that my requests on the simulator were not appearing in the Charles http traffic monitoring list.
I thought perhaps, it is because I'm using simulator, but that was not the case. I tried opening the safari and browsed some web page and the traffic immediately appeared. So the issue was not from the simulator.
I also tried installing on the device, and again the same story, when using HttpClient the traffic monitoring screen remains silent, but as soon as I open the browser, the traffic screen starts jiggling and usurping all the requests.
I thought may be it is because I use HTTPS, although in any case at least the request header should be captured, even though the body is encoded. But that was not the case, I tried opening some HTTPS site on my device safari and again the traffic appeared on my Charles screen.
The next thing that I did I downloaded monotouch HttpClient sample. And the good news is that there are several methods of sending requests, actually four of them -
1. http WebRequest,
2. https WebRequest,
3. http NSUrlConnection ,
4. HttpClient.
And I tried them all, as you may guess first three perfectly appeared in charles, but the last HttpClient again I don't know why didn't show up in traffic log screen.
So I'm 100% sure that the issue is the HttpClient class, which I don't know why despite the fact that it is working normally, that is sending/receiving requests, the requests made by this class can not be captured by Charles.
And to exclude the last possible reason for this issue, that is may be the problem is in Charles, I also tried using Fiddler on Windows, which was running as a virtual machine on my Mac (here you can find how to do that), the same story was repeated - all the requests made by HttpClient were not captured, the rest (WebRequests, NSUrlConnection-s, safari web page openings) worked perfectly fine.
Please, can anybody suggest me, whether it is some kind of bug, may be there is workaround or other solution to this problem.
Thanks all for your Replies
Kind Regards Gagik
There's many ways to initialize HttpClient. Some ways won't talk with the OS (fully managed) and won't be aware of the iOS proxy settings.
The best (for iOS) is generally to use the handler that uses CFNetwork, see this blog for more details. Basically it means:
var client = new HttpClient (CFNetworkHandler ());
Otherwise you'll need to set the HttpClientHandler.Proxy to CFNetwork.GetDefaultProxy. E.g.
var handler = new HttpClientHandler {
Proxy = CFNetwork.GetDefaultProxy (),
UseProxy = true,
};
var client = new HttpClient(handler);

Ajax POST requests loosing payload in sencha app + phonegap

I'm developing a sencha web application with sencha touch 2.1.x
This app needs to run on a web server and as well as an iOS app, packaged with phonegap 3.1.
The web server version works well, but I'm facing some problems in the iOS version, when executing Ajax POST requests. This is a sample code:
var jsonParams =
{
"username": loginParams.user,
"password": loginParams.pwd,
"serverVersion": serverVersion
};
var jsonStr = JSON.stringify(jsonParams);
console.log(jsonStr);
Ext.Ajax.request({
url: internalRoute.LOGIN,
headers: {'Content-Type':'application/json'},
method: 'POST',
scope:this,
params: jsonStr,
success: function(response){
.....
},
failure: function(response) {
.....
},
});
Sometimes this requests works fine from the iOS version packaged with phonegap, but randomly it fails, because the post payload is empty. The console.log(jsonStr) prints the correct data, but the server receives no data. I also used an HTTP sniffer to capture the sent package, and it contains no POST payload.
Changed the code to user jQuery $.ajaxinstead of Ext.Ajax.request, and the problem persists.
Any idea of what can be wrong?
Thx
Looks like it's actually a proxy issue, nothing to do with sencha or phonegap.
If I deactivate the proxy, I don't get other external resources, but this issue never happens.
Will search how to fix the proxy issue.
Thanks

Resources