在iOS中,点击手机附件通常是指用户通过文件管理器(如Files应用)或邮件附件等方式打开文件。要获取这些数据,可以通过以下几种方式:
Info.plist
文件中添加UIFileSharingEnabled
键,并设置为YES
,以允许应用通过iTunes共享文件。LSApplicationQueriesSchemes
键,并包含com.apple.developer.fileprovider
,以便应用可以访问文件提供者。UIDocumentPickerViewController
来显示文件选择器。import UIKit
import MobileCoreServices
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text"], in: .import)
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
}
extension ViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first else { return }
// 处理选中的文件
}
}
Capabilities
中启用Associated Domains
,并添加applinks:
加上你的域名。import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url = userActivity.webpageURL {
// 处理Universal Link
}
return true
}
}
通过以上方法,你可以在iOS应用中有效地获取和处理用户点击手机附件时的数据。
领取专属 10元无门槛券
手把手带您无忧上云