提交 b38141c6 编写于 作者: VK1688's avatar VK1688

新增 canOpenURL UTS API,可用此API判断url是否可以跳转

上级 7e33f945
## 1.1.0(2024-12-06)
- 新增 canOpenURL UTS API,可用此API判断url是否可以跳转
## 1.0.1(2024-11-13)
- 修复 Android 打开部分 schema 时没有跳转到目标应用的 Bug
## 1.0.0(2024-04-25)
......
......@@ -12,25 +12,35 @@
1. 安装此插件
2. 在要使用的地方 `import` 导入
```ts
import { openSchema } from '@/uni_modules/uts-openSchema'
import { openSchema, canOpenURL } from '@/uni_modules/uts-openSchema'
```
3. 直接调用 `openSchema` 方法:
```ts
// #ifdef UNI-APP-X
// 使用外部浏览器打开指定URL
openSchema('https://uniapp.dcloud.io/uni-app-x')
// #ifdef APP-ANDROID
// Android 使用应用商店打开指定App
openSchema('market://details?id=com.tencent.mm')
// Android 打开地图坐标
openSchema('androidamap://viewMap?sourceApplication=Hello%20uni-app&poiname=DCloud&lat=39.9631018208&lon=116.3406135236&dev=0')
// 可以先用canOpenURL判断是否安装了地图软件
if (canOpenURL('androidamap://')) {
openSchema('androidamap://viewMap?sourceApplication=Hello%20uni-app&poiname=DCloud&lat=39.9631018208&lon=116.3406135236&dev=0')
} else {
console.log('未安装高德地图')
}
// #endif -->
// #ifdef APP-IOS
// 打开 AppStore 到搜索页
openSchema('itms-apps://search.itunes.apple.com//WebObjects//MZSearch.woa/wa/search?media=software&lterm=')
// 打开 iOS 地图坐标
openSchema('http://maps.apple.com/?q=Mexican+Restaurant&sll=50.894967,4.341626&z=10&t=s')
// #endif -->
// #endif -->
```
......
import Intent from 'android.content.Intent'
import Uri from 'android.net.Uri'
import { OpenSchema } from '../interface.uts'
import { OpenSchema, CanOpenURL } from '../interface.uts'
export const openSchema: OpenSchema = function (url: string) {
if (typeof url === 'string' && url.length > 0) {
export const openSchema : OpenSchema = function (url : string) {
if (canOpenURL(url)) {
const context = UTSAndroid.getUniActivity()!
const uri = Uri.parse(url)
const intent = new Intent(Intent.ACTION_VIEW, uri)
......@@ -11,6 +11,17 @@ export const openSchema: OpenSchema = function (url: string) {
intent.setData(uri)
context.startActivity(intent)
} else {
console.error('url param ERROR:', JSON.stringify(url))
console.error('url param Error:', JSON.stringify(url))
}
}
export const canOpenURL : CanOpenURL = function (url : string) {
if (typeof url === 'string' && url.length > 0) {
const context = UTSAndroid.getUniActivity()!
const uri = Uri.parse(url)
const intent = new Intent(Intent.ACTION_VIEW, uri)
return intent.resolveActivity(context.packageManager) != null
} else {
return false
}
}
import { OpenSchema } from '../interface.uts'
import { OpenSchema, CanOpenURL } from '../interface.uts'
export const openSchema: OpenSchema = function(url: string): void {
if (typeof url == 'string' && url.length > 0) {
let uri = new URL(string = url)
if (uri != null && UIApplication.shared.canOpenURL(uri!)) {
UIApplication.shared.open(uri!, options = new Map<UIApplication.OpenExternalURLOptionsKey, any>(), completionHandler = null)
}else {
console.error('url param Error: ', url)
}
}else {
console.error('url param Error: ', url)
}
}
\ No newline at end of file
export const openSchema : OpenSchema = function (url : string) : void {
if (canOpenURL(url)) {
let uri = new URL(string = url)
UIApplication.shared.open(uri!, options = new Map<UIApplication.OpenExternalURLOptionsKey, any>(), completionHandler = null)
} else {
console.error('url param Error: ', url)
}
}
export const canOpenURL : CanOpenURL = function (url : string) : boolean {
if (typeof url == 'string' && url.length > 0) {
let uri = new URL(string = url)
if (uri != null && UIApplication.shared.canOpenURL(uri!)) {
return true
}
}
return false
}
export type OpenSchema = (url: string) => void
export type OpenSchema = (url : string) => void
export type CanOpenURL = (url : string) => boolean
import { OpenSchema } from '../interface.uts'
import { OpenSchema, CanOpenURL } from '../interface.uts'
export const openSchema: OpenSchema = function(url: string): void {
location.href = url;
export const openSchema : OpenSchema = function (url : string) : void {
location.href = url;
}
export const canOpenURL : CanOpenURL = function (url : string) : boolean {
if (url != "") {
return true;
}
return false;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册