Flutter 應(yīng)用間跳轉(zhuǎn)應(yīng)用,實(shí)現(xiàn)喚起第三方App
前言
最近因?yàn)楣ぷ餍枨螅隽藨?yīng)用間跳轉(zhuǎn)應(yīng)用,因?yàn)槭且粋€(gè)flutter新手,所以在工作之余隨便總結(jié)記錄一下。
一、應(yīng)用間跳轉(zhuǎn)應(yīng)用場(chǎng)景
1.使用第三方用戶登錄,跳轉(zhuǎn)到需授權(quán)的App。如QQ登錄,微信登錄等。需要用戶授權(quán),還需要"返回到調(diào)用的程序,同時(shí)返回授權(quán)的用戶名、密碼"。
2.應(yīng)用程序推廣,跳轉(zhuǎn)到另一個(gè)應(yīng)用程序(本機(jī)已經(jīng)安裝),或者跳轉(zhuǎn)到iTunes并顯示應(yīng)用程序下載頁面(本機(jī)沒有安裝)。
3.第三方支付,跳轉(zhuǎn)到第三方支付App,如支付寶支付,微信支付。
4.內(nèi)容分享,跳轉(zhuǎn)到分享App的對(duì)應(yīng)頁面,如分享給微信好友、分享給微信朋友圈、分享到微博。
5.顯示位置、地圖導(dǎo)航,跳轉(zhuǎn)到地圖應(yīng)用。
6.使用系統(tǒng)內(nèi)置程序,跳轉(zhuǎn)到打電話、發(fā)短信、發(fā)郵件、Safari打開網(wǎng)頁等內(nèi)置App中。
…
等等
二、配置URL Scheme
跳轉(zhuǎn)應(yīng)用的實(shí)現(xiàn),需要使用 uni_links 第三方庫來協(xié)助完成外部頁面的 Scheme(在想要跳轉(zhuǎn)到的應(yīng)用中引入uni_links庫,并配置Scheme)
Android 配置
安卓支持兩種app links 和deep links.
app links需要是scheme需要指定https,并且要增加hosts文件assetlinks.json,還需要服務(wù)端配合。
deep links可以自定義scheme,也不要服務(wù)端的驗(yàn)證.
在AndroidManifest.xml中添加
<!-- Deep Links -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with YOUR_SCHEME://YOUR_HOST -->
<data
android:scheme="[YOUR_SCHEME]"
android:host="[YOUR_HOST]" />
</intent-filter>
<!-- App Links -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with https://YOUR_HOST -->
<data
android:scheme="https"
android:host="[YOUR_HOST]" />
</intent-filter>
</activity>
ios配置
ios也支持兩種,“Universal Links” 和 “Custom URL schemes”,兩個(gè)功能和android類似。
Universal Link需要在ios/Runner/Runner.entitlements添加一個(gè)com.apple.developer.associated-domains環(huán)境,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- ... other keys -->
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:[YOUR_HOST]</string>
</array>
<!-- ... other keys -->
</dict>
</plist>
Custom URL schemes在Info.plist中添加
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>Two You</string>
<key>CFBundleURLSchemes</key>
<array>
<string>tyfapp</string>
</array>
</dict>
</array>
提示:當(dāng)需要被拉起的 App 沒有被安裝時(shí),這個(gè)鏈接就不會(huì)生效;
uni_links使用
它可以幫助我們幫助我們獲取進(jìn)入的鏈接,它有兩個(gè)方法供我們使用。一個(gè)是獲取初始鏈接,另一個(gè)是監(jiān)聽。
初始鏈接方法:
Future<void> _handleInitialUri() async {
// In this example app this is an almost useless guard, but it is here to
// show we are not going to call getInitialUri multiple times, even if this
// was a weidget that will be disposed of (ex. a navigation route change).
if (!_initialUriIsHandled) {
_initialUriIsHandled = true;
_showSnackBar('_handleInitialUri called');
try {
final uri = await getInitialUri();
if (uri == null) {
print('no initial uri');
} else {
print('got initial uri: $uri');
}
if (!mounted) return;
setState(() => _initialUri = uri);
} on PlatformException {
// Platform messages may fail but we ignore the exception
print('falied to get initial uri');
} on FormatException catch (err) {
if (!mounted) return;
print('malformed initial uri');
setState(() => _err = err);
}
}
}
監(jiān)聽鏈接變化:
void _handleIncomingLinks() {
if (!kIsWeb) {
// It will handle app links while the app is already started - be it in
// the foreground or in the background.
_sub = uriLinkStream.listen((Uri? uri) {
if (!mounted) return;
print('got uri: $uri');
setState(() {
_latestUri = uri;
_err = null;
});
}, onError: (Object err) {
if (!mounted) return;
print('got err: $err');
setState(() {
_latestUri = null;
if (err is FormatException) {
_err = err;
} else {
_err = null;
}
});
});
}
}
銷毀監(jiān)聽:
@override
void dispose() {
_sub?.cancel();
super.dispose();
}
三、實(shí)現(xiàn)跳轉(zhuǎn)
上面我們配置好了android和ios,現(xiàn)在只需要我們通過我們配置的deeplink來打開跳轉(zhuǎn)App了。
1.引入庫
url_launcher: ^6.1.8
2. 跳轉(zhuǎn)
打開瀏覽器
const url = 'https://flutter.io';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
打開外部APP
代碼如下(示例):
final Uri launch = Uri.parse('tyfapp://');
bool isInstall = await canLaunchUrl(launch);
if(isInstall){
print('已安裝,跳轉(zhuǎn)app');
await launchUrl(launch);
}else{
print('未安裝,做出提示或者到下載頁面');
}
提示:ios跳轉(zhuǎn)到App Store可通過這樣實(shí)現(xiàn):
final url = "https://itunes.apple.com/cn/app/id1380512641"; // id 后面的數(shù)字換成自己的應(yīng)用 id 就行了
launchUrlString(url);
H5跳轉(zhuǎn)App
第一種
window.location = 'wechat://';
第二種文章來源:http://www.zghlxwxcb.cn/news/detail-744490.html
<a href="wechat://"></a>
或
const a = document.createElement('a')
a.href = "wechat://";
document.body.appendChild(a);
a.click();
四、結(jié)尾
這樣就完成了打開第三方app了,我只是隨便簡(jiǎn)單記錄了一下,后續(xù)繼續(xù)完善補(bǔ)充。文章來源地址http://www.zghlxwxcb.cn/news/detail-744490.html
到了這里,關(guān)于Flutter 應(yīng)用間跳轉(zhuǎn)應(yīng)用,實(shí)現(xiàn)喚起第三方App的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!