Skip to content

Instantly share code, notes, and snippets.

@saitoxu
Last active August 6, 2017 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saitoxu/19f31c67c7aff98fc56f9bd24c070cb9 to your computer and use it in GitHub Desktop.
Save saitoxu/19f31c67c7aff98fc56f9bd24c070cb9 to your computer and use it in GitHub Desktop.
2017-08-06
// 外部ブラウザで開く例
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