Send data to external webview - javascript

I'm trying to send some data from app.js to an open webview (external url, example: http://mysite.com/file.html), without success. I've check through many questions and answers and tried different solutions with Ti.App.fireEvent and Ti.App.addEventListener without any good success. I did however find a solution that did this with a local html-file some time ago, but weren't able to recreate this for an external.
app.js
Ti.App.fireEvent('helloWorld', { data : "Hello World" );
http://mysite.com/file.html
Ti.App.addEventListener('helloWorld', function(e)
{
// do something with e.data
});
doesn't seem to do anything.

Solved the problem by using evalJS
app.js
web.addEventListener('load', function() {
var data = "some data";
web.evalJS("testJS('" + data + "')");
});
http://mysite.com/file.html
<script>
function testJS (data) {
alert(data);
}
</script>

Oddly, this only works in the iPhone simulator but not in the Android simulator (1.6 API and 2.2 API). In Android, you get the dreaded "Force Close" button.

Related

Get push additional Data onesignal iOS (JS)

My app (html/JS with Cordova/Phonegap) gets push from the OneSignal system, everything works fine.
I send each time a small variable (additionalData) so that once the notification has been clicked my app opens and a specific action is done. This variabla is stocked into a sessionStorage var so it can be used everywhere i want on my app.
In my index.js, in onDeviceready i have :
var notificationOpenedCallback = function(jsonData) {
sessionStorage.setItem('pushToLoad', JSON.stringify(jsonData.notification.payload.additionalData.ADDITIONALVAR));
};
window.plugins.OneSignal
.startInit("USER CODE")
.handleNotificationOpened(notificationOpenedCallback)
.endInit();
Then, in a file called function.js I simply analyze the variable and I do my actions according to its value.
Like:
if(sessionStorage.getItem('pushToLoad') == 'blabla') { do stuff }
On Android, everything works fine ! But on iOS my var is empty/null, i don't know why, ive tryed a lot of piece of code and nothing works.
alert('Push var > ' + sessionStorage.getItem('pushToLoad'));
It returns me "null" on iOS.
Thanks for you help.

Certain $http request not working on iOS Emulators or Devices (but fine on everything else)

hope you are well.
I am currently trying to call an API we have designed on a C# WebAPI which will call successfully on devices. However, on occasion, it will not call on the iOS Platform. This is how I have the calling of the API setup:
$scope.RunNews = function() {
console.log("Testing 123. I don't know!")
var app_url_news = APP_URL.getUrl() + '/api/news/getallnewsinfo';
$http.get(app_url_news).success(function(data) {
console.log("Successful Transfer. Data Below")
console.log(data);
angular.forEach(data.data, function(obj) {
console.log(obj);
if (obj.videoUrl) {
if (Validators.isValidYoutubeUrl(obj.videoUrl)) {
var youtubeVideoId = obj.videoUrl.split('v=')[1].split('&')[0];
obj.videoUrl = 'http://www.youtube.com/embed/' + youtubeVideoId + '?html5=1&rel=0&hl=en_US&version=3';
}
obj.isVideo = true;
} else {
obj.imagePath = APP_URL.getUrl() + '/NewsImages/files/' + obj.imagePath;
obj.isVideo = MediaType.isVideo(obj.imagePath);
}
});
$scope.newsList = data.data;
$ionicLoading.hide();
});
}
}
Currently, this function calls on the ionic serve web browsing emulator and android devices... HOWEVER, on iOS, the $http is never called. I have noticed that this is apparent for multiple $http calls but for others, they work fine.
I'm here to ask if anyone has experienced this issue and has a fix for it? I believe it may have something to do with permissions but I cannot seem to find anything in my project.
I had a similar problem. GET requests couldn't be made on iOS devices but were working in the browser. I fixed the issue by installing cordova-plugin-whitelist. To use it, you must have Cordova version 4.0 or greater.
If the problem persists, you should debug the app for any logs in console and check the request's response and request headers. These will help you pin down what the problem is.

Meteor method not working properly in mozilla

I am trying to make a call to a meteor method, to insert a document before redirecting the user to the relevant url (using the generated document _id).
The code currently works on chromium but not on firefox, where on firefox it appears to just get redirected right away without actually inserting anything.
I've attached my code at the bottom. Can anyone tell me what went wrong and what can I do to fix it? Why will chrome and firefox behave differently in this situation?
Any help provided is greatly appreciated!
client.js
newDoc(){
Meteor.call('addDoc',{
// some parameters
})
}
clientandserver.js (Meteor method)
'addDoc'(obj){
console.log(obj); // does not output anything on firefox
DocumentData.insert({
//some parameters
},function(err,documentID){
if (Meteor.isClient){
window.location = '/docs/' + documentID;
// redirection happens before insertion on firefox
}
});
}
Bring window.location to the client side. Like:
newDoc(){
Meteor.call('addDoc', data, function(error, result){
if(result){
window.location = '/docs/' + documentID;
}
})
}
And put only the insertion in server side, like:
'addDoc'(obj){
return DocumentData.insert({
//some parameters
});
}
I've used this structure and it works for me in both Firefox & Chrome.

WKWebView doesn't interact anymore with Javascript in iOS10

I have a native app which has to interact with a website. It has been working normally up to iOS 9, but with iOS 10, the Javascript code inside the web app is no longer valid.
Here is an example of the JS code I use on the onClick event of a button, which as mentioned worked like a charm before iOS10.
function DoSomething()
{
var iframe = document.createElement("IFRAME");
var url='codeToBeUsed://id=1230';
iframe.setAttribute("src", url);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
when I debug the app on Xcode, the request variable which normally contained the content of the "url" variable on the example provided, now returns a blank value...
<NSMutableURLRequest: 0x170011070> { URL: about:blank }
I even tested placing a alert('click'); but it didn't work either. Does anybody know how to solve this issue?
Using a code close to yours (I pass a stringified json in the src), I also got an embarassing 'about;blank' in the request.
It seems iOs10 has some new restrictions on what you pass to iframe 'src'. I found it requires a valid url to trigger properly the request.
Try to use :
var url='codeToBeUsed://?id=1230';
Edit : or encode URI...

Sammy Js and ajax not working in IE browser

I am using sammy js and ajax for my frontend.
for example
URL: www.mysite.com/#/index
(function($) {
var app = $.sammy(function() {
this.get('#/index', function() {
init();
});
});
$(function() {
app.run()
});
})(jQuery);
function int(){
alert('testing');
}
In firefox and chrome i am getting alert message, but in IE its not working and i am not getting any error also. Any can one help for me?
I am using IE11. after navigating through 2 routes, when I click browser BACK, it goes straight back to the path having no hash sign appended. In Chrome it works as expected.

Categories

Resources