搜索插件、代码仓与 APP 分类

external_path_ios_mac 封面
插件详情v0.0.8

external_path_ios_mac

一个用于在 iOS 和 macOS 设备上检索目录路径的 Flutter 包,确保跨平台的文件访问和管理。

iOSmacOS
近 30 天下载
271
喜欢数
3
Pub 分数
160

安装插件

flutter pub add external_path_ios_mac

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个用于在 iOS 和 macOS 设备上检索标准化目录路径的 Flutter 包,可在两个平台上实现一致的文件访问和管理。它通过提供可靠的路径解析,简化了对外部存储位置(如文档、下载和缓存)的处理。该包特别适用于需要跨平台文件系统交互而无需编写特定于平台代码的应用程序。以简单性和可靠性为核心设计,确保开发者能够以统一的方式高效管理文件操作。

适合场景

  • 访问用户文档目录
  • 管理应用缓存文件
  • 处理下载文件夹
  • 跨平台文件路径解析
  • 与外部存储集成

主要特点

  • 平台无关的路径检索
  • 对 iOS 和 macOS 的支持
  • 常用目录的简单 API
  • 可靠的路径解析
  • 无需额外权限

适合对象

  • 文件密集型应用
  • 跨平台 Flutter 应用
  • 需要外部存储访问权限的应用
  • 避免原生代码的开发者
  • 需要一致文件路径的项目

使用建议

  • 仅限 iOS 和 macOS
  • 不处理文件的创建或删除
  • 需要了解平台特定的目录语义
  • 不适用于 Android 或 Windows
  • 依赖于路径常量的正确使用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

external_path_ios_mac

A Flutter package for retrieving directory paths on iOS and macOS devices, ensuring file access and management across platforms.

Platform support: iOS ✓ | macOS ✓


Getting Started

Add the dependency to your pubspec.yaml file:

dependencies:
  external_path_ios_mac: ^0.0.8

Alternatively, you can use the Git repository:

dependencies:
  external_path_ios_mac:
    git:
      url: https://github.com/SwanFlutter/external_path_ios_mac.git


How to Use

Import the package in your Dart file:

import 'package:external_path_ios_mac/external_path_ios_mac.dart';

Supported Directories

iOS Directories
Directory Type Description
DirectoryType.downloads Path to the Downloads directory.
DirectoryType.music Path to the Music directory.
DirectoryType.podcasts Path to the Podcasts directory.
DirectoryType.ringtones Path to the Ringtones directory.
DirectoryType.alarms Path to the Alarms directory.
DirectoryType.notifications Path to the Notifications directory.
DirectoryType.pictures Path to the Pictures directory.
DirectoryType.movies Path to the Movies directory.
DirectoryType.dcim Path to the DCIM directory.
DirectoryType.documents Path to the Documents directory.
DirectoryType.screenshots Path to the Screenshots directory.
DirectoryType.audiobooks Path to the Audiobooks directory.
macOS Directories
Directory Type Description
MacDirectoryType.downloads Path to the Downloads directory.
MacDirectoryType.pictures Path to the Pictures directory.
MacDirectoryType.movies Path to the Movies directory.

Example Usage

Retrieving Directory Paths on iOS
final externalPath = ExternalPathIosMac();

// Downloads directory
final downloadsPath = await externalPath.getDirectoryPath(
  directory: DirectoryType.downloads,
);

// Music directory
final musicPath = await externalPath.getDirectoryPath(
  directory: DirectoryType.music,
);

// Documents directory
final documentsPath = await externalPath.getDirectoryPath(
  directory: DirectoryType.documents,
);

// DCIM directory
final dcimPath = await externalPath.getDirectoryPath(
  directory: DirectoryType.dcim,
);
Retrieving Directory Paths on macOS
final externalPath = ExternalPathIosMac();

// Downloads directory
final downloadsPathMac = await externalPath.getDirectoryPathMacOs(
  directory: MacDirectoryType.downloads,
);

// Pictures directory
final picturesPathMac = await externalPath.getDirectoryPathMacOs(
  directory: MacDirectoryType.pictures,
);

// Movies directory
final moviesPathMac = await externalPath.getDirectoryPathMacOs(
  directory: MacDirectoryType.movies,
);
Retrieving Platform Version
final platformVersion = await externalPath.getPlatformVersion();
print('Platform Version: $platformVersion');
Retrieving Root Installation Path
final externalPath = ExternalPathIosMac();

try {
  final rootPath = await externalPath.getRootInstallationPath();
  print('Root installation path: $rootPath');
} catch (e) {
  print('Error: $e');
}

On iOS, returns the main bundle path. On macOS, returns the application's installation directory.


Full Example

import 'package:flutter/material.dart';
import 'dart:io';
import 'package:external_path_ios_mac/external_path_ios_mac.dart';

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _externalPath = ExternalPathIosMac();

  String _platformVersion = '';
  String _downloadsPath = '';
  String _musicPath = '';
  String _documentsPath = '';
  String _downloadsPathMac = '';
  String _picturesPathMac = '';

  @override
  void initState() {
    super.initState();
    _initPlatformState();
  }

  Future<void> _initPlatformState() async {
    final platformVersion = await _externalPath.getPlatformVersion();

    // iOS paths
    final downloadsPath = await _externalPath.getDirectoryPath(
      directory: DirectoryType.downloads,
    );
    final musicPath = await _externalPath.getDirectoryPath(
      directory: DirectoryType.music,
    );
    final documentsPath = await _externalPath.getDirectoryPath(
      directory: DirectoryType.documents,
    );

    // macOS paths
    final downloadsPathMac = await _externalPath.getDirectoryPathMacOs(
      directory: MacDirectoryType.downloads,
    );
    final picturesPathMac = await _externalPath.getDirectoryPathMacOs(
      directory: MacDirectoryType.pictures,
    );

    setState(() {
      _platformVersion = platformVersion ?? 'Unknown';
      _downloadsPath = downloadsPath ?? 'Unknown';
      _musicPath = musicPath ?? 'Unknown';
      _documentsPath = documentsPath ?? 'Unknown';
      _downloadsPathMac = downloadsPathMac ?? 'Unknown';
      _picturesPathMac = picturesPathMac ?? 'Unknown';
    });
  }

  Future<void> _saveFile(String path, String fileName) async {
    if (path != 'Unknown') {
      final file = File('$path/$fileName');
      await file.writeAsString('Sample content');
      print('File saved to $path/$fileName');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Directory Paths Example')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Platform: $_platformVersion'),
              const SizedBox(height: 16),
              Text('iOS Downloads: $_downloadsPath'),
              Text('iOS Music: $_musicPath'),
              Text('iOS Documents: $_documentsPath'),
              const SizedBox(height: 16),
              Text('macOS Downloads: $_downloadsPathMac'),
              Text('macOS Pictures: $_picturesPathMac'),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: () => _saveFile(_downloadsPath, 'example.txt'),
                child: const Text('Save File to Downloads'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Platform-Specific Setup

iOS

Add the following keys to your Info.plist if your app accesses photos or camera:

<key>NSPhotoLibraryUsageDescription</key>
<string>We need access to your photo library.</string>
<key>NSCameraUsageDescription</key>
<string>We need access to your camera.</string>
macOS

Add the following entitlement to macos/Runner/DebugProfile.entitlements and macos/Runner/Release.entitlements:

<key>com.apple.security.files.user-selected.read-only</key>
<true/>

Swift Package Manager

This package supports Swift Package Manager (SPM) for both iOS and macOS, compatible with Flutter 3.44+. CocoaPods is also supported for backwards compatibility.


Additional Information

For issues, questions, or suggestions, contact us at [swan.dev1993@gmail.com](mailto:swan.dev1993@gmail.com) or visit our GitHub repository.

所属分类

依赖

依赖清单 1 项

  • plugin_platform_interface^2.1.8

源码仓库

使用此插件的项目

暂无相关数据。