You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Swift开发中如何调用系统词典获取单词的具体释义

解答:Swift获取iOS系统词典单词释义的实现方案

iOS没有公开直接返回释义文本的官方API,目前有两种可落地的实现方案,可根据你的使用场景选择:

方案1:官方公开标准实现(无审核风险,推荐)

苹果官方提供了UIReferenceLibraryViewController控件,可直接弹出系统原生词典页面展示完整释义,完全符合App Store审核规则:

import UIKit

func showSystemWordDefinition(word: String) {
    // 先校验词典是否存在该单词的释义
    guard UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: word) else {
        print("当前系统词典未收录该单词")
        return
    }
    let dictViewController = UIReferenceLibraryViewController(term: word)
    // 从当前顶层控制器弹出即可
    UIApplication.shared.connectedScenes
        .filter{$0.activationState == .foregroundActive}
        .compactMap{$0 as? UIWindowScene}
        .first?.windows
        .first{$0.isKeyWindow}?.rootViewController?
        .present(dictViewController, animated: true)
}

这个方案支持用户自由切换多语种词典、查询词源、查看例句等系统词典原生功能,适合仅需要给用户展示释义、不需要拿到释义文本的场景。

方案2:私有API获取释义文本(有审核风险,仅适合内部测试场景)

如果确实需要获取释义的文本内容,可以通过私有API实现,但使用私有API大概率会被App Store审核驳回,请勿在线上发布的应用中使用:

// 先声明私有API映射,避免编译报错
@objc protocol DCSDictionaryPrivateProtocol {
    @objc func definitionForWord(_ word: String) -> NSString?
}

func getDefinitionString(word: String) -> String? {
    guard let DCSDictionaryClass = NSClassFromString("DCSDictionary"),
          let availableDicts = DCSDictionaryClass.value(forKey: "availableDictionaries") as? [Any] else {
        return nil
    }
    for dictItem in availableDicts {
        guard let dict = dictItem as? DCSDictionaryPrivateProtocol,
              let definition = dict.definitionForWord(word) as? String else {
            continue
        }
        // 返回的是带HTML标签的富文本,可自行解析成富文本展示
        return definition
    }
    return nil
}

补充注意:私有API的结构、命名随时可能在系统版本更新中变更,不保证全iOS版本兼容。


内容的提问来源于stack exchange,提问作者Underthestars-zhy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 09:57:04