How to call setAllowUniversalAccessFromFileURLs from WebSettings in nativescript? - xmlhttprequest

In Android native development, to allow XMLHttpRequests in some scenarios when we need to request javascript files from an webview, one way to do that is setting setAllowUniversalAccessFromFileURLs(true) on WebSettings instance (on API Level 17+).
Is there a way to do that on nativescript?
The error displayed is:
XMLHttpRequest cannot load file:///data/data/.... Cross origin
requests are only supported for protocol schemes: http, data, https.",
source:
file:///data/data/com.yourdomain.appname/files/app/app/frameworks/nativescript.framework/app/components/login/

Just quickly looking at the Android docs you could get the WebSettings for the webview with: https://developer.android.com/reference/android/webkit/WebView.html#getSettings()
So in the loaded event for your webview instance, attach an event handler and in that JS code do something like this:
function webViewLoaded(args) {
if (args.object.android) {
var webView = args.object.android;
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
}

Related

Titanium evalJS slows loading of the webview - timeout waiting to evaluate js

I am developing Titanium application with a webview that is loading content from a remote url. In the load event I am injecting some code with evalJS. Using Titanium SDK 8.0.0+ the loading of the webview content is very slow because of evalJS, I need to wait for some time in order to scroll or click something. I can see in the console that there is a warning repeatedly saying "TiWebViewBinding: (main) [4405,4881] Timeout waiting to evaluate JS", and when this warning stops showing I can interact with the webview. If evalJS is not used meaning I am not injecting code, there is no problem. The webview is loaded properly and there is no warning saying "Timeout waiting to evaluate JS". Before 8.0.0 sdk the problem did not existed. Has anyone had an experience with an issue like this? Am I missing something?
I would be very grateful if I get some input on this. Thanks.
var webview = Ti.UI.createWebView({
width : Ti.UI.FILL,
height : Ti.UI.FILL,
url : "remote url"
});
webview.addEventListener('load', function() {
webview.evalJS('(function() {alert("test");})();');
webview.evalJS('(function sum(val1, val2){return val1 + val2;})();');
});
window.add(webview);
window.open();

Square Connect API - SqPaymentForm Apple Pay

I have problem enabling Apple Pay on site. I'm using https with valid certificate domain is registered in Square for apple pay payments. There is a callback method in js for enabled methods.
// SqPaymentForm callback functions
callbacks: {
/*
* callback function: methodsSupported
* Triggered when: the page is loaded.
*/
methodsSupported: function (methods) {
var applePayBtn = document.getElementById('sq-apple-pay');
// Only show the button if Apple Pay for Web is enabled
// Otherwise, display the wallet not enabled message.
if (methods.applePay === true) {
alert('Apple Pay supported');
applePayBtn.style.display = 'inline-block';
}
},
The method gets called but it never reaches inside of the if statement. Paying with card works just fine. So the iFrames get loaded into form properly and everything. I just can't make Apple Pay work. I'm loading the page in Safari on mac mini or in webview on iphone simulator. Any ideas?

Notification in Tizen wearable web app to display the UI of app running in background

In the tizen wearable web application that I am developing, I need my application to prompt a notification to the user every 10min to go into the same application and give some sort of input from the app UI.
I am currently using a simple status notification from notification API which gives a notification having link to the current application. When user clicks on it, the application is launched again (as it does according to description in simple status notifiation).
But I don't want the application to be restarted by clicking on the notification. Instead it should get the application running background to display on the watch UI.
Please let me know any possible solutions to achieve this.
Below is the code I am using right now.
var myappInfo = tizen.application.getAppInfo();
var notificationDict = {
content : "Please enter your response.",
iconPath : "images/icon.png",
vibration : true,
soundPath : "music/solemn.mp3",
appId : myappInfo.id
};
currentBatteryLevelNotification = new(tizen.StatusNotification("SIMPLE",
"Your input required!", notificationDict);
tizen.notification.post(currentBatteryLevelNotification);
I tried playing with your code, got some progress using:
AppContextId
var myappInfo = tizen.application.getAppContext();
//appId : myappInfo.id or muappInfo.appId
and Moving app to background:
document.addEventListener('tizenhwkey', function(e) {
if(e.keyName === "back") {
try {
tizen.application.getCurrentApplication().hide();
//instead of tizen.application.getCurrentApplication().exit();
} catch (ignore) {
}
}
});
config.xml:
<tizen:setting background-support="enable" encryption="disable" hwkey-event="enable"/>
Tip: If you don't add background-support for Web application, it just dies once you are on exit, It's not possible to get current state.
But I assume the answer is No. May be Notification API is not designed to launch running application I guess.

Worklight 6.1: How to add EULA to hybrid app

Environment:
Worklight 6.1.0.2
dojo 1.9.4
We have created a hybrid app using Worklight 6.1 for android, iOS and windows8 platform. Now we would like to add and show End User License Agreement (EULA) window to the user, when the app first time launch. It should have Accept and Decline button. If user tap on Accept button, then he should be able to use the app.
I would like to know, how can we achieve this using Worklight 6.1.
Any help on this, will be much appreciated.
FYI there is nothing specific here to Worklight.
You could implement this in any number of ways w/out ever using any Worklight API whatsoever.
You could achieve it for example like this (untested code - you'll need to experiment):
In main.js create some global variable eulaAccepted:
var eulaAccepted;
// You will need to handle this property using HTML5 Local Storage so that it will persist for the next time the app is launched, and have the app act accordingly.
Then, in wlCommonInit():
function wlCommonInit() {
if (!eulaAccepted) {
displayEula();
} else {
displayApp();
}
}
In displayEula():
function displayEula() {
// either display a dialog using `WL.SimpleDialog`...
// Or maybe custom HTML with "accept" and "not accept" buttons
WL.SimpleDialog.show(
"Eula Agreement", "your-eula-text-here",
[{text: "Accept", handler: acceptEula },
{text: "Reject", handler: rejectEula}]
);
}
Handle the result:
function acceptEula() {
eulaAccepted = true;
... // Some code that will store the `eulaAccepted` variable using HTML5 Local Storage API
displayApp();
}
function rejectEula() {
// Display some other custom HTML instead of your app.
// Maybe also additional logic to try again to accept the Eula...
}

How do I allow downloads from a WebView using the standard download system on a Mac app?

I am building a mac app and would like to able to include the ability to browse the web and download files from the sites using the standard downloads system like you see when using safari.
At the moment when I click a link to a .zip or .nzb in the app it does nothing! Is there a way of allowing this from the app?
Thanks in advance for any help :)
The download manager in Safari is implemented by Safari, not by WebKit, so you don't get that functionality "for free", just the tools to build it.
In order to be able to download files, you need to assign an object as the WebPolicyDelegate of the WebView and implement the webView:decidePolicyForMIMEType:request:frame:decisionListener: delegate method.
In that method, you must call one of the WebPolicyDecisionListener protocol methods on the object that is passed as the decisionlistener parameter to the method. The three WebPolicyDecisionListener protocol methods are ignore, use or download. For any MIME types that you want to download, you must call download on the object passed as the listener parameter:
- (void)webView:(WebView *)webView
decidePolicyForMIMEType:(NSString *)type
request:(NSURLRequest *)request
frame:(WebFrame *)frame
decisionListener:(id < WebPolicyDecisionListener >)listener
{
if([type isEqualToString:#"application/zip"])
{
[listener download];
}
//just ignore all other types; the default behaviour will be used
}
You then need to assign an object as the download delegate of your WebView. This object will be sent all of the NSURLDownloadDelegate protocol messages as well as the WebDownload
delegate messages. You can use these messages to choose where the file is downloaded, as well as to implement a download manager UI like Safari.
Off the top of my head I would think you could implement the WebView's delegates to detect when a user clicks on a link - then check the extension and if it's not html/php/whatever then manually download the file yourself
Here's a link to a question (and answer) for how to capture the link pressing:
Cocoa WebKit/WebView delegate for location change? (User clicked link, javascript action, etc)
Another option is to open the link in external browser. And the file will be downloaded using external browser's download manager.
Sample code in Swift 2.2 with XCode 7.3:
func webView(webView: WebView!, decidePolicyForNavigationAction actionInformation: [NSObject : AnyObject]!, request: NSURLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!) {
let url = request.URL!.absoluteString
if url.hasSuffix(".zip") || url.hasSuffix(".nzb") {
listener.ignore()
NSWorkspace.sharedWorkspace().openURL(request.URL!)
} else {
listener.use()
}
}
PS. method above comes from WebPolicyDelegate.