Last active
August 6, 2017 03:24
-
-
Save saitoxu/19f31c67c7aff98fc56f9bd24c070cb9 to your computer and use it in GitHub Desktop.
2017-08-06
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 外部ブラウザで開く例 | |
import React, { Component } from 'react' | |
import { Alert, Linking, WebView } from 'react-native' | |
const ATS_ERROR_CODE = -1022 | |
export default class WebViewSample extends Component { | |
constructor(props) { | |
super(props) | |
this.handleError = this.handleError.bind(this) | |
this.openUrl = this.openUrl.bind(this) | |
} | |
handleError(proxy) { | |
const { code } = proxy.nativeEvent | |
const { url } = this.props | |
if (code === ATS_ERROR_CODE) { | |
this.openUrl(url) | |
} | |
} | |
openUrl(url) { | |
Linking.canOpenURL(url).then(supported => { | |
if (supported) { | |
Linking.openURL(url) | |
} else { | |
Alert.alert('URLを開けませんでした。') | |
} | |
}) | |
} | |
render() { | |
const { url } = this.props | |
return <WebView source={{ uri: url }} startInLoadingState onError={this.handleError} /> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment