How do I open in-app browser window in React native? - react-redux

I'm trying to open the browser window without leaving the app when I click a URL (for both iOS and Android).
The behavior should be as follows (with airbnb app example):
Clicks in "Terms and conditions" link: links example
Open the browser in-app:
For iOS: in-app browser iOS
Same for Android.
How can I do this? Do I need to use any specified existing library?
I'm using react-native 0.37.

You can use the new InAppBrowser plugin for React Native, check the next example:
import { Linking } from 'react-native'
import InAppBrowser from 'react-native-inappbrowser-reborn';
...
async openLink() {
try {
const isAvailable = await InAppBrowser.isAvailable()
const url = 'https://www.google.com'
if (isAvailable) {
InAppBrowser.open(url, {
// iOS Properties
dismissButtonStyle: 'cancel',
preferredBarTintColor: 'gray',
preferredControlTintColor: 'white',
// Android Properties
showTitle: true,
toolbarColor: '#6200EE',
secondaryToolbarColor: 'black',
enableUrlBarHiding: true,
enableDefaultShare: true,
forceCloseOnRedirection: true,
}).then((result) => {
Alert.alert(JSON.stringify(result))
})
} else Linking.openURL(url)
} catch (error) {
Alert.alert(error.message)
}
}
...
On the other hand, you can use this plugin with deep linking, check the README of the project.

Opens Safa Module Method
On iOS SafariView 3rd party native module - https://github.com/naoufal/react-native-safari-view
On Android CustomTabs 3rd party native module - https://github.com/droibit/react-native-custom-tabs - however if the user does not have Chrome installed, it will pop open the link outside of your app in their default browser.
Alternative WebView Method
You can use a <WebView> but this is not using the real browser - http://facebook.github.io/react-native/releases/0.47/docs/webview.html#webview
import React, { Component } from 'react';
import { WebView } from 'react-native';
class MyWeb extends Component {
render() {
return (
<WebView
source={{uri: 'https://github.com/facebook/react-native'}}
style={{marginTop: 20}}
/>
);
}
}

Use React Native Custom Tabs to open in-app browser window in React native. It support both platform Android and iOS

You can use React Native In-App Browser Reborn. It works perfectly on both Android and iOS.
This library use react-native-safari-view (SafariView) on iOS and react-native-custom-tabs (ChromeView) on Android.

Use native react native linking to open a url
https://reactnative.dev/docs/linking

Related

Nativescript Camera Plus photoCapturedEvent not called and image not saving to library nativescript+vue

I am trying to implement the nativescript-camera-plus plugin to take an image. I am developing an IOS (for iPad) application with NativeScript + vue and am using an emulator to test.
For some reason the photoCapturedEvent event is not been triggered when the takePicture() is called. Furthermore, when { saveToGallery: true } is been passed through this method, the image is not saved to the photo gallery.
The loaded event works and runs as the component is loaded. I am unsure if the errorEvent event gets triggered.
Here is a snippet of my code (in TakePhoto.vue):
<CameraPlus ref="CameraPlus" height="600" id="camPlus"
saveToGallery="true"
showCaptureIcon="true"
showGalleryIcon="false"
showToggleIcon="false"
showFlashIcon="false"
debug="true"
enableVideo="false"
confirmVideo="false"
defaultCamera="front"
#loaded="onCameraLoaded"
#photoCapturedEvent="photoCaptured($event)"
#errorEvent="onCameraError">
</CameraPlus>
And here is the associated methods
onCameraLoaded(result) {
this.cam = result.object;
console.log("Camera loaded...");
},
onButtonCapture() {
console.log('Take Picture');
this.image = this.cam.takePicture({ saveToGallery: true });
},
photoCaptured(args){
console.log("ARGS - ", args)
},
I would also like to mention that I have registered the component in the app.js file like so: Vue.registerElement('CameraPlus', () => require('#nstudio/nativescript-camera-plus').CameraPlus);
Any help would be appreciated.

Is there a way to disable all caching in WebView (both iOS and Andriod), in a NativeScript Angular app?

I am making a NativeScript Angular app which uses WebView to display a remote web page, but as I am making changes on this page on a website, it doesn't get updated on Andriod nor on iOS devices I use for app development. Page is updated normally on regular browsers on these devices.
I've read what NativeScript Angular documentation has to say about the WebView control and it ain't very detailed.
I'd like to be able to do something like:
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
... as described here.
Here is how you could get a reference to the NativeScript WebView and from there access the native Android control (android.webkit.WebView)
import { Component } from "#angular/core";
import { EventData } from "tns-core-modules/data/observable";
import { WebView } from "tns-core-modules/ui/web-view";
import { isAndroid } from "tns-core-modules/platform";
declare let android: any; // or even better - use tns-platform-declarations for intelliSense for the native APis
#Component({
selector: "Home",
moduleId: module.id,
templateUrl: "./home.component.html",
styleUrls: ["./home.component.css"]
})
export class HomeComponent {
onWebViewLoaded(args: EventData) {
const webView = args.object as WebView;
const nativeWebView = webView.nativeView; // equal to webView.android or webView.ios (depending on the platform)
if (isAndroid) {
nativeWebView.getSettings().setAppCacheEnabled(false);
nativeWebView.getSettings().setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);
}
}
}
Where onWebViewLoaded is used with the loaded event in the HTML
<WebView height="1200" src="https://www.nativescript.org" (loaded)="onWebViewLoaded($event)"></WebView>
A Playground app demonstrating the above here

Share images to social media using react native

I am developing a react native app. I want to share the selected images which I fetch from firebase storage and listed in the app to social media like WhatsApp. For that, I am using an npm package called
react-native-share
. Using that I was able to share text but no image. the official page is telling that I should convert the image first to base64 and I have done that, and the app started to crash. can anyone please tell me how to do it.
You no longer need to use react-native-share. Use the native Share component.
check this post: React Native - can we share an image and text into whatsapp?
Here you can find an example of code:
import React, { Component } from 'react';
import {
Share,
Text,
TouchableOpacity
} from 'react-native';
const shareOptions = {
title: 'Title',
message: 'Message to share', // Note that according to the documentation at least one of "message" or "url" fields is required
url: 'www.example.com',
subject: 'Subject'
};
export default class ShareExample extends React.Component {
onSharePress = () => Share.share(shareOptions);
render(){
return(
<TouchableOpacity onPress={this.onSharePress} >
<Text>Share data</Text>
</TouchableOpacity>
);
}
}
Finally you have to options to send the image + text message: - You can use the url field of the shareOptions adding the remote URI of the image so it can be previewed in the WhatsApp message, and the title or subject fields to add the text. - You can share a base64 file url like this: url: 'data:image/png;base64,'

How can I access Native api in NativeScript when I use Typescript

When I create two new apps with tns, one is the regular js version and one is with typescript. I get a strange error in the typescript version when I try to access a native library.
When I create a loaded function with a console.log(pow(x,y)), it works fine with the js version but the typescript version crashes with this error.
error TS2304: Cannot find name 'pow'.
Why?
TS:
import { EventData } from "data/observable";
import { Page } from "ui/page";
import { HelloWorldModel } from "./main-view-model";
// Event handler for Page "navigatingTo" event attached in main-page.xml
export function navigatingTo(args: EventData) {
// Get the event sender
var page = <Page>args.object;
page.bindingContext = new HelloWorldModel();
}
export function loaded() {
console.log('Hello World')
console.log('pow(2.5, 3) = ', pow(2.5, 3));
}
JS:
var createViewModel = require("./main-view-model").createViewModel;
function onNavigatingTo(args) {
var page = args.object;
page.bindingContext = createViewModel();
}
function loaded() {
console.log('hello world')
console.log('pow(2.5, 3) = ', pow(2.5, 3));
}
exports.onNavigatingTo = onNavigatingTo;
exports.loaded = loaded;
TypeScript is strongly typed language and needs an explicit type definition for each variable (e.g. like pow). Instead of casting to any, you could provide definition files pre-generated by NativeScript that will give you typing and IntelliSense for the native Android and iOS APIs.
The latest release of NativeScript by default is creating the app without the platform declaration files (android17.d.ts for Android and ios.d.ts for iOS) and without those files, your TypeScript simply does not know about the native APIs references.
The reason not to include the definition files - those are enormously big and are needed only when the developers are going to use TypeScript (or Angular) + access to native APIs.
The solution:
1.) Install the definition files as a developer dependency
npm i tns-platform-declarations --save-dev
This will install your platform declaraion files in node_modules/tns-platform-declarations
2.) Create references.d.ts in your main app directory and add the following
// SEE the updated paths below
Now you are good to go!
UPDATE (as of October 2017 - with installing tns-core-modules 3.x.x (or above) and tns-platform-declarations 3.x.x (or above)):
The npm plugin now has a different structure so these are the proper paths (create references.d.ts file in the root directory and place the one below)
/// <reference path="./node_modules/tns-platform-declarations/ios/ios.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/android/android.d.ts" />
Important: Your tsconfig.json should look like this:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"experimentalDecorators": true,
"lib": [
"es6",
"dom"
]
}
}

desktop notification api for windows

Are there any windows software that i can use that can connect with my website via API where there is a customizable feature for desktop notifications?
Alternatively, are there any hacks i can use to make the width of the chrome desktop notification larger?
I tried using html but this just prints as a string.
function notifyMe() {
if (!Notification) {
alert('Notifications are supported in modern versions of Chrome, Firefox, Opera and Firefox.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
var notification = new Notification('Notification title', {
icon: 'http://www.metoffice.gov.uk/media/image/j/i/Cumulus_2.jpg',
body: "<div style='width:700px'>You've been notified!</div>",
});
notification.onclick = function () {
window.open("http://www.google.com");
};
}
Any suggestions.. i want a customizable size notification?
You can't change the size of Chrome-provided notifications.
You can use an extension + Native Messaging to pass something to a native application of your choice (maybe through a "proxy" script, as Native Messaging is limited).

Resources