iOS 通过webview打开相册和相机, 地理位置信息
1,iOS通过webview 打开相机和相册
1.1前端在webview里面的代码
1.2 iOS 需要做的事情就是配置权限
1.2.1 配置info.plist 配置相机,相册和麦克风的权限
Privacy - Camera Usage Description: 相机权限描述
Privacy - Photo Library Usage Description:相册权限描述
Privacy - Microphone Usage Description:麦克风权限
2. iOS通过WebView获取地理位置信息
2.1 前端在WebView里面获取地理位置的代码
2.2 iOS需要在info.plist配置地理位置权限, 获取权限后h5即可获得权限
Privacy - Location Usage Description:地理位置权限描述
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSLog(@"decidePolicyForNavigationAction");
NSURLRequest * request = navigationAction.request;
NSString * scheme = request.URL.scheme;
// 处理通过webview 打电话逻辑
if (scheme != nil && [@"tel" isEqualToString:scheme]) {
[self hideLoadHud];
UIApplication * app = [UIApplication sharedApplication];
if ([app canOpenURL:request.URL]) {
[app openURL:request.URL];
}
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
// 其他逻辑 ......
}
Android调用系统相机、GPS
相机调用准备工作
声明权限:
<uses-permission android:name="android.permission.CAMERA" />
使用前动态申请相机权限。
Android7.0以上需要 声明FileProvider (https://www.jianshu.com/p/47fcd7873f39)
相机调用开始
val CAPTURE_REQUEST = 123
var mFilePathCallback: ValueCallback<Array<Uri>>? = null //webview内部回调
var captureUri: Uri? = null //使用该uri接收系统返回的图片/视频
mWebview.webChromeClient = object : WebChromeClient() {
fun file2Uri(file: File): Uri {
//Android 7.0以上需要使用fileprovider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val authority = application.packageName + ".provider"
return FileProvider.getUriForFile(Utils.getApp(), authority, file)
} else {
return Uri.fromFile(file)
}
}
/**
* 复写该方法
*/
override fun onShowFileChooser(webView: WebView?, filePathCallback: ValueCallback<Array<Uri>>?, fileChooserParams: FileChooserParams?): Boolean {
mFilePathCallback = filePathCallback
if (fileChooserParams?.acceptTypes?.isNotEmpty() == true) {
val mipmap = fileChooserParams.acceptTypes[0]
when (mipmap) {
"image/*" -> {
val file = File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "imageCapture${System.currentTimeMillis()}.jpg")
///文件路径 storage/emulated/0/Android/data/包名/files/Pictures/imageCapture1563956535105.jpg
captureUri = file2Uri(file)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(intent, CAPTURE_REQUEST)
return true
}
"video/*" -> {
val file = File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "videoCapture${System.currentTimeMillis()}.mp4")
///文件路径 storage/emulated/0/Android/data/包名/files/Pictures/videoCapture1563956535105.mp4
captureUri = file2Uri(file)
val intent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(intent, CAPTURE_REQUEST)
return true
}
}
}
return super.onShowFileChooser(webView, filePathCallback, fileChooserParams)
}
}
接收返回结果
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CAPTURE_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
mFilePathCallback?.onReceiveValue(arrayOf(captureUri)) //给回调设置数据
} else
mFilePathCallback?.onReceiveValue(arrayOf()) //给回调设置空数据
}
}
GPS调用准备工作
申明权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
使用时动态申请权限
WebSettings webseting = mWebView.getSettings();
webseting.setJavaScriptEnabled(true);
获取权限后h5即可获得权限