iOS 之 URL Scheme

URL Scheme 是苹果设计用来在本应用内打开其他应用 (并在其他打开其他指定页面, 亦可传递参数) 的一个方法, 其格式与 URL 的请求格式类似, 对比如下:

  • URL: https://www.hanleylee.com
  • URL Scheme: scheme://host:port/path

比如微信扫一扫功能的 URL Scheme 就是 weixin://dl/scan

URL Scheme 的优缺点对比

  1. 优点
  2. 缺点

iOS 项目中使用 URL Scheme

本 App 可以打开第三方其他 App; 同理, 第三方 App 也可以打开我们的 App, 这涉及到两方面的设置

本应用唤醒其他应用

  1. Info.plistLSApplicationQueriesSchemes 中注册要打开的应用的 URL Scheme (这一步也被称为 “设置白名单”. 除系统组件不需要额外注册外, 其他第三方 App 都需要注册, fb 也不能例外)

  2. 在要执行打开其他 App 的位置执行以下程序 (注意, 要先用 canOpenUrl 来确认是否可以打开)
    swift
       if let url1 = URL(string: "fb://profile/100041507435168"), let url2 = URL(string: "https://www.facebook.com/hanley.lei") {
           if UIApplication.shared.canOpenURL(url1) {
               UIApplication.shared.open(url1, options: [:]) { (success) in }
           } else {
               UIApplication.shared.open(url2, options: [:]) { (success) in }
           }
       }

其他应用唤醒本应用

  1. Info.plist 文件中的 URL typesItem 0URL SchemesItem 0 做如下设置

    注意:

    • URL Scheme 名不可以数字开头!
    • URL identifier 这一项, 我很好奇是什么作用, 看了一大堆博客, 每个博客都互相抄, 说是 “该字符串是你自定义的 URL scheme 的名字. 建议采用反转域名的方法保证该名字的唯一性”, 我感觉他们自己都不知道这是干什么的. 我查了下 alipay 中 Info.plist 文件这一项的设置, 发现 alipay 自己都没有对自己的 URL Scheme 组设置 Identifier, 而只是对其他三个组设置了 Identifier, 所以我认为这一项完全就是给自己看的, 用来进行分组, 设置不设置见仁见智, 可有可无.

  2. Appdelegate.swift 文件中实作 optional func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool 方法, 以根据用户输入的不同 Scheme URL 在进入本 App 时执行不同的动作
    swift
       func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
           if url.host == nil {
               return true;
           }
       
           // 获取来源应用的 Identifier
           // print("来源 App: \(options[UIApplication.OpenURLOptionsKey.sourceApplication]!)")
           if url.path == "123" {
               print("123")
           } else {
               print("32")
           }
       
           // 获取 url 以及参数
           let urlString = url.absoluteString
           let queryArray = urlString.components(separatedBy: "/")
           print(urlString)
       
           simpleInfoAlert(info: "\(queryArray[2])  \(queryArray[3])", duration: 2, interact: true)
           return true
       }

注意

  • Info.plistLSApplicationQueriesSchemes 中最多注册 50 个 URL Scheme, 这是因为 Apple 担心某些 App 使用此功能配合 canOpenUrl 检测用户装了哪些 App, 这会触犯到用户的隐私
  • 如果两个 App 的 URL Scheme 相同, 那么最近安装 App 的 URL Scheme 将会替代前面安装 App 的 URL Scheme

常用的 URL Scheme

系统

打开项对应 URL Scheme
短信sms://
app storeitms-apps://
电话tel://
备忘录mobilenotes://
设置prefs:root=SETTING
E-MailMESSAGE://
iCloudprefs:root=CASTLE
iCloud Backupprefs:root=CASTLE&path=BACKUP
Wi-Fiprefs:root=WIFI
Bluetoothprefs:root=Bluetooth
Cellularprefs:root=MOBILE_DATA_SETTINGS_ID
Personal Hotspotprefs:root=INTERNET_TETHERING
Personal Hotspot ⇾ Family Sharingprefs:root=INTERNET_TETHERING&path=Family%20Sharing
Personal Hotspot ⇾ Wi-Fi Passwordprefs:root=INTERNET_TETHERING&path=Wi-Fi%20Password
VPNprefs:root=General&path=VPN
Notificationsprefs:root=NOTIFICATIONS_ID
Notifications ⇾ Siri Suggestionsprefs:root=NOTIFICATIONS_ID&path=Siri%20Suggestions
Soundsprefs:root=Sounds
Ringtoneprefs:root=Sounds&path=Ringtone
Do Not Disturbprefs:root=DO_NOT_DISTURB
Do Not Disturb ⇾ Allow Calls Fromprefs:root=DO_NOT_DISTURB&path=Allow%20Calls%20From
Screen Timeprefs:root=SCREEN_TIME
Screen Time ⇾ Downtimeprefs:root=SCREEN_TIME&path=DOWNTIME
Screen Time ⇾ App Limitsprefs:root=SCREEN_TIME&path=APP_LIMITS
Screen Time ⇾ Always Allowedprefs:root=SCREEN_TIME&path=ALWAYS_ALLOWED
Generalprefs:root=General
General ⇾ Aboutprefs:root=General&path=About
General ⇾ Software Updateprefs:root=General&path=SOFTWARE_UPDATE_LINK
General ⇾ CarPlayprefs:root=General&path=CARPLAY
General ⇾ Background App Refreshprefs:root=General&path=AUTO_CONTENT_DOWNLOAD
General ⇾ Multitasking (iPad-only)prefs:root=General&path=MULTITASKING
General ⇾ Date & Timeprefs:root=General&path=DATE_AND_TIME
General ⇾ Keyboardprefs:root=General&path=Keyboard
General ⇾ Keyboard ⇾ Keyboardsprefs:root=General&path=Keyboard/KEYBOARDS
General ⇾ Keyboard ⇾ Hardware Keyboardprefs:root=General&path=Keyboard/Hardware%20Keyboard
General ⇾ Keyboard ⇾ Text Replacementprefs:root=General&path=Keyboard/USER_DICTIONARY
General ⇾ Keyboard ⇾ One Handed Keyboardprefs:root=General&path=Keyboard/ReachableKeyboard
General ⇾ Language & Regionprefs:root=General&path=INTERNATIONAL
General ⇾ Dictionaryprefs:root=General&path=DICTIONARY
General ⇾ Profilesprefs:root=General&path=ManagedConfigurationList
General ⇾ Resetprefs:root=General&path=Reset
Control Centerprefs:root=ControlCenter
Control Center ⇾ Customize Controlsprefs:root=ControlCenter&path=CUSTOMIZE_CONTROLS
Displayprefs:root=DISPLAY
Display ⇾ Auto Lockprefs:root=DISPLAY&path=AUTOLOCK
Display ⇾ Text Sizeprefs:root=DISPLAY&path=TEXT_SIZE
Accessibilityprefs:root=ACCESSIBILITY
Wallpaperprefs:root=Wallpaper
Siriprefs:root=SIRI
Apple Pencil (iPad-only)prefs:root=Pencil
Face IDprefs:root=PASSCODE
Emergency SOSprefs:root=EMERGENCY_SOS
Batteryprefs:root=BATTERY_USAGE
Battery ⇾ Battery Health (iPhone-only)prefs:root=BATTERY_USAGE&path=BATTERY_HEALTH
Privacyprefs:root=Privacy
Privacy ⇾ Location Servicesprefs:root=Privacy&path=LOCATION
Privacy ⇾ Contactsprefs:root=Privacy&path=CONTACTS
Privacy ⇾ Calendarsprefs:root=Privacy&path=CALENDARS
Privacy ⇾ Remindersprefs:root=Privacy&path=REMINDERS
Privacy ⇾ Photosprefs:root=Privacy&path=PHOTOS
Privacy ⇾ Microphoneprefs:root=Privacy&path=MICROPHONE
Privacy ⇾ Speech Recognitionprefs:root=Privacy&path=SPEECH_RECOGNITION
Privacy ⇾ Cameraprefs:root=Privacy&path=CAMERA
Privacy ⇾ Motionprefs:root=Privacy&path=MOTION\
App Storeprefs:root=STORE
App Store ⇾ App Downloadsprefs:root=STORE&path=App%20Downloads
App Store ⇾ Video Autoplayprefs:root=STORE&path=Video%20Autoplay
Walletprefs:root=PASSBOOK
Passwords & Accountsprefs:root=ACCOUNTS_AND_PASSWORDS
Passwords & Accounts ⇾ Fetch New Dataprefs:root=ACCOUNTS_AND_PASSWORDS&path=FETCH_NEW_DATA
Passwords & Accounts ⇾ Add Accountprefs:root=ACCOUNTS_AND_PASSWORDS&path=ADD_ACCOUNT
Mailprefs:root=MAIL
Mail ⇾ Previewprefs:root=MAIL&path=Preview
Mail ⇾ Swipe Optionsprefs:root=MAIL&path=Swipe%20Options
Mail ⇾ Notificationsprefs:root=MAIL&path=NOTIFICATIONS
Mail ⇾ Blockedprefs:root=MAIL&path=Blocked
Mail ⇾ Muted Thread Actionprefs:root=MAIL&path=Muted%20Thread%20Action
Mail ⇾ Blocked Sender Optionsprefs:root=MAIL&path=Blocked%20Sender%20Options
Mail ⇾ Mark Addressesprefs:root=MAIL&path=Mark%20Addresses
Mail ⇾ Increase Quote Levelprefs:root=MAIL&path=Increase%20Quote%20Level
Mail ⇾ Include Attachments with Repliesprefs:root=MAIL&path=Include%20Attachments%20with%20Replies
Mail ⇾ Signatureprefs:root=MAIL&path=Signature
Mail ⇾ Default Accountprefs:root=MAIL&path=Default%20Account
Contactsprefs:root=CONTACTS
Calendarprefs:root=CALENDAR
Calendar ⇾ Alternate Calendarsprefs:root=CALENDAR&path=Alternate%20Calendars
Calendar ⇾ Syncprefs:root=CALENDAR&path=Sync
Calendar ⇾ Default Alert Timesprefs:root=CALENDAR&path=Default%20Alert%20Times
Calendar ⇾ Default Calendarprefs:root=CALENDAR&path=Default%20Calendar
Notesprefs:root=NOTES
Notes ⇾ Default Accountprefs:root=NOTES&path=Default%20Account
Notes ⇾ Passwordprefs:root=NOTES&path=Password
Notes ⇾ Sort Notes Byprefs:root=NOTES&path=Sort%20Notes%20By
Notes ⇾ New Notes Start Withprefs:root=NOTES&path=New%20Notes%20Start%20With
Notes ⇾ Sort Checked Itemsprefs:root=NOTES&path=Sort%20Checked%20Items
Notes ⇾ Lines & Gridsprefs:root=NOTES&path=Lines%20%26%20Grids
Notes ⇾ Access Notes from Lock Screenprefs:root=NOTES&path=Access%20Notes%20from%20Lock%20Screen
Remindersprefs:root=REMINDERS
Reminders ⇾ Default Listprefs:root=REMINDERS&path=DEFAULT_LIST
Voice Memosprefs:root=VOICE_MEMOS
Phoneprefs:root=Phone
Messagesprefs:root=MESSAGES
FaceTimeprefs:root=FACETIME
Mapsprefs:root=MAPS
Maps ⇾ Driving & Navigationprefs:root=MAPS&path=Driving%20%26%20Navigation
Maps ⇾ Transitprefs:root=MAPS&path=Transit
Compassprefs:root=COMPASS
Measureprefs:root=MEASURE
Safariprefs:root=SAFARI
Safari ⇾ Content Blockersprefs:root=SAFARI&path=Content%20Blockers
Safari ⇾ Downloadsprefs:root=SAFARI&path=DOWNLOADS
Safari ⇾ Close Tabsprefs:root=SAFARI&path=Close%20Tabs
Safari ⇾ Clear History and Dataprefs:root=SAFARI&path=CLEAR_HISTORY_AND_DATA
Safari ⇾ Page Zoomprefs:root=SAFARI&path=Page%20Zoom
Safari ⇾ Request Desktop Websiteprefs:root=SAFARI&path=Request%20Desktop%20Website
Safari ⇾ Readerprefs:root=SAFARI&path=Reader
Safari ⇾ Cameraprefs:root=SAFARI&path=Camera
Safari ⇾ Microphoneprefs:root=SAFARI&path=Microphone
Safari ⇾ Locationprefs:root=SAFARI&path=Location
Safari ⇾ Advancedprefs:root=SAFARI&path=ADVANCED
Newsprefs:root=NEWS
Healthprefs:root=HEALTH
Shortcutsprefs:root=SHORTCUTS
Musicprefs:root=MUSIC
Music ⇾ Cellular Dataprefs:root=MUSIC&path=com.apple.Music:CellularData
Music ⇾ Optimize Storageprefs:root=MUSIC&path=com.apple.Music:OptimizeStorage
Music ⇾ EQprefs:root=MUSIC&path=com.apple.Music:EQ
Music ⇾ Volume Limitprefs:root=MUSIC&path=com.apple.Music:VolumeLimit
Settings ⇾ TVprefs:root=TVAPP
Photosprefs:root=Photos
Cameraprefs:root=CAMERA
Camera ⇾ Record Videoprefs:root=CAMERA&path=Record%20Video
Camera ⇾ Record Slo-moprefs:root=CAMERA&path=Record%20Slo-mo
Booksprefs:root=IBOOKS
Game Centerprefs:root=GAMECENTER

支付宝

打开项对应 URL Scheme
支付宝alipay://
蚂蚁庄园alipays://platformapi/startapp?appId=66666674
蚂蚁森林alipays://platformapi/startapp?appId=60000002
蚂蚁宝卡alipays://platformapi/startapp?appId=60000057
款码alipayqr://platformapi/startapp?saId=20000056
扫码alipays://platformapi/startapp?saId=10000007
红包alipays://platformapi/startapp?appId=88886666
股票alipays://platformapi/startapp?appId=20000134
生活缴费alipays://platformapi/startapp?appId=20000193
手机充值alipays://platformapi/startapp?appId=10000003
彩票alipays://platformapi/startapp?appId=10000011
淘票票alipays://platformapi/startapp?appId=20000131
查快递alipays://platformapi/startapp?appId=20000754
AA 收款alipays://platformapi/startapp?appId=20000263
收款alipays://platformapi/startapp?appId=20000123
转账alipays://platformapi/startapp?appId=20000221
还信用卡alipays://platformapi/startapp?appId=09999999
滴滴出行alipays://platformapi/startapp?appId=20000778
钉钉dingtalk://
淘宝网taobao://
淘宝旅行taobaotravel://
淘宝宝贝搜索taobao://http://s.taobao.com/?q=: prompt
淘宝店铺搜索taobao://http://shopsearch.taobao.com/browse/shop_search.htm?q=: prompt
天猫tmall://

QQ

打开项对应 URL Scheme
QQmqq://
QQ 群组mqqapi://card/show_pslcard?src_type=internal&version=1&card_type=group&uin={QQ 群号}
QQ 联系人mqqapi://card/show_pslcard?src_type=internal&version=1&uin={QQ 号码}
QQ 国际版mqqiapi://
QQ 音乐qqmusic://
QQ 音乐最近播放qqmusic://today?mid=31&k1=2&k4=0
QQ 浏览器qq browser://
QQ 邮箱qqmail://
腾讯企业邮箱qqbizmailDistribute2://
腾讯视频tenvideo://tenvideo2://tenvideo3://
腾讯新闻qqnews://
腾讯微云weiyun://
腾讯地图sosomap://
腾讯视频tenvideo://
QQ 斗地主tencent382://
QQ 浏览器mttbrowser://
QQ 安全中心qmtoken://
腾讯手机管家mqqsecure://
腾讯微博TencentWeibo://
天天星连萌tencent100689806://
天天爱消除tencent100689805://
天天酷跑tencent100692648://
天天飞车tencent100695850://
节奏大师tencentrm://

微信

打开项对应 URL Scheme
微信weixin://
微信-扫一扫weixin://scanqrcode

百度

打开项对应 URL Scheme
百度baiduboxapp:// 或 BaiduSSO://
百度地图baidumap://
百度贴吧com.baidu.tieba://
百度云baiduyun://
百度音乐baidumusic://
百度视频baiduvideoiphone://bdviphapp://
百度糯米bainuo://
百度魔图photowonder://
百度魔拍wondercamera://
百度导航bdNavi://
百度输入法BaiduIMShop://

网易

打开项对应 URL Scheme
网易邮箱neteasemail://
网易新闻newsapp://
网易云音乐orpheuswidget://
有道词典yddict://yddictproapp://
有道云笔记youdaonote://
网易公开课ntesopen://
网易将军令netease-mkey://

美团

打开项对应 URL Scheme
美团外卖meituanwaimai://
美团imeituan://
点评dianping://dianping://search

Tweetbot

打开项对应 URL Scheme
Opentweetbot://
Timelinetweetbot://<screenname>/timeline
Mentionstweetbot://<screenname>/mentions
Retweetstweetbot://<screenname>/retweets
Direct Messagestweetbot://<screenname>/direct_messages
Liststweetbot://<screenname>/lists
Favoritestweetbot://<screenname>/favorites
Searchtweetbot://<screenname>/search[?query=<text>&callback_url=<url>]
Statustweetbot://<screenname>/status/<tweet_id>?callback_url=<url>
Favoritetweetbot://<screenname>/favorite/<tweet_id>
Unfavoritetweetbot://<screenname>/unfavorite/<tweet_id>
Retweettweetbot://<screenname>/retweet/<tweet_id>
Listtweetbot://<screenname>/list/<list_id>?callback_url=<url>

WhatsApp

打开项对应 URL Scheme
only open whatsAppwhatsapp://
only open whatsApp(universal-link)https://wa.me/
start a chatwhatsapp://send?phone=1234
start a chat(universal-link)https://wa.me/1234
start chat to phone numberwhatsapp://send?phone=1234&text=Hello%20world
start chat to phone number(universal-link)https://wa.me/1234/?text=Hello%20world

Surge

打开项对应 URL Scheme
Opensurge://
Start with selected configurationsurge://start[?autoclose=true]
Stop current sessionsurge://stop[?autoclose=true]
Start or stop with selected configurationsurge://toggle[?autoclose=true]
Install a configuration from a URLsurge:///install-config?url=URL

其他常用 APP

打开项对应 URL Scheme
12306cn.12306://
京东openApp.jdMobile://jd://
今日头条snssdk141://
高德地图iosamap://
新浪微博weibo://sinaweibo://
微博国际版weibointernational://
优酷youku://
爱奇艺iqiyi://qiyi-iphone://
爱奇艺 PPSppstream://
土豆视频tudou://
PPTVpptv://
暴风影音com.baofeng.play://
搜狐视频sohuvideo-iphone://sohuvideo://
搜狐新闻sohunews://
虾米音乐xiami://
酷我音乐com.kuwo.kwmusic.kwmusicForKwsing://
酷狗音乐kugouURL://
天天动听ttpod://
摩拜单车mobike://
ofoofoapp://
chromegooglechrome://
Gmailgooglegmail://
印象笔记evernote://
挖财记账wacai://
猎豹浏览器sinaweibosso.422729959://
UC 浏览器ucbrowser://
名片全能王camcard://
豆瓣 fmdoubanradio://
微盘sinavdisk://
人人renren://
我查查wcc://
1 号店wccbyihaodian://
知乎zhihu://
墨客com.moke.moke-1://
扫描全能王camscanner://
TuneIn Radiotunein://tuneinpro://
OfficeSuitemobisystemsofficesuite://
WPS OfficeKingsoftOfficeApp://
Lineline://
1Passwordonepassword://
Clear(著名的 Todo 应用)clearapp://
Calendars 5calendars://
GoodReader 4com.goodreader.sendtogr://
PDF Expert 5pdfexpert5presence://
Documents 5rdocs://
nPlayernplayer-http://
GPlayergplayer://
AVPlayerHD AVPlayerHD://
AVPlayerAVPlayer://
Ace Playeraceplayer://
12306 订票助手trainassist://
金山词霸com.kingsoft.powerword.6://
凤凰新闻comIfeng3GifengNews://
高铁管家gtgj://
飞信fetion://
大智慧dzhiphone://
布卡漫画buka://
哔哩哔哩动画bilibili://
56 视频com.56Video://
365 日历rili365://
58 同城wbmain://
遇见iaround://
陌陌momochat://
旺旺卖家版wangwangseller://
掌阅 iReaderiReader://
艺龙旅行elongIPhone://
迅雷 + 迅雷云播thunder://
熊猫公交wb1405365637://
携程无线CtripWireless://
无线苏州SuZhouTV://
唯品会vipshop://
微视weishiiosscheme://
微拍wpweipai://
旺信wangxin://
万年历youloft.419805549://
同花顺amihexin://
天涯社区tianya://
天气通 Prosinaweatherpro://
天气通sinaweather://
墨迹天气rm434209233MojiWeather://
蜻蜓 FMqtfmp://
浦发银行wx1cb534bb13ba3dbd://
招商银行cmbmobilebank://
建设银行wx2654d9155d70a468://
工商银行com.icbc.iphoneclient://
保卫萝卜 2wb2217954495://
保卫萝卜wb1308702128://
搜狗输入法com.sogou.sogouinput://
随手记FDMoney://
weico 微博weico://
TestFilghtitms-beta://

如何查找第三方 App 的 URL Scheme

  1. iMazimgManage AppsDownload to Library -> Export.IPA

  2. 将后缀名 .ipa 修改为 .zip, 并解压文件
  3. 打开 payload***.app显示包内容, 查找 info.plist, 这个就跟自己 appinfo.plist 一样了, app 注册了哪些自定义的 URL Scheme 就一目了然了

Reference

本博客文章采用 CC 4.0 协议,转载需注明出处和作者。

鼓励作者