【问题标题】:How to get AppID of my application to access application data folder in iOS simulator?如何获取我的应用程序的 AppID 以访问 iOS 模拟器中的应用程序数据文件夹?
【发布时间】:2017-07-19 05:51:23
【问题描述】:

我正在为我的 iOS 应用构建 Cucumber/Calabash 测试。我需要在模拟器中访问我的应用程序数据文件夹,以将数据复制到该文件夹​​。 我可以到达这条路:

~/Library/Developer/CoreSimulator/Devices/[DeviceID]/data/Containers/Data/Application/[NEED TO GET THIS AppID]

但我需要检测我的[AppID] 才能访问正确的文件夹。有什么方法可以通过终端或使用一些.sh 脚本从应用程序名称或捆绑ID 中获取它?

【问题讨论】:

    标签: ios sh calabash-ios


    【解决方案1】:

    在 Swift 4 中,在 viewDidLoad() 中插入以下代码:

        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) // get the Documents folder path
        
        let pathForDocumentDir = documentsPath[0]
        print("pathForDocumentDir: \(pathForDocumentDir)")
    

    例如:

    class ViewController: UIViewController {
        override func viewDidLoad() {
            let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) // get the Documents folder path
            
            let pathForDocumentDir = documentsPath[0]
            print("pathForDocumentDir: \(pathForDocumentDir)")
        }
    }
    

    这将在控制台输出中打印如下内容:

    /Users/user/Library/Developer/CoreSimulator/Devices/11321A31-27D6-49E0-85C8-D7EDEFA6751F/data/Containers/Data/Application/BB9EAD09-17B5-4FA1-907A-0EDFC07BFA62/Documents
    

    代码基于this source

    【讨论】:

      【解决方案2】:

      [[NSBundle mainBundle] bundlePath] 将为您提供应用程序的顶层文件夹。 它看起来像这样:

      ~/Library/Developer/CoreSimulator/Devices/[DeviceID]/data/Containers/Bundle/Application/[AppID]/[AppName.app]

      然后你可以使用 NSArray 获取 [AppID]:

      NSArray *components = [path componentsSeparatedByString:@"/"];
      NSLog(@"AppID: %@", [components objectAtIndex:[components count] - 2]);
      

      【讨论】:

      • 我必须通过终端命令访问它...(在葫芦测试的 .rb 文件中使用)我的应用程序是用 C# 和 Xamarin 而不是在目标 C 或 swift 中制作的...我可以得到Containers/**Bundle**/Application/ 中的 AppID,通过搜索 BundleName 但该 ID 与 Containers/**Data**/Application/ 中的 AppID 不同...
      【解决方案3】:

      我已经用这样的脚本解决了这个问题:

      APPLICATION_DATA_FOLDER = $1
      cd APPLICATION_DATA_FOLDER
      
      for dir in *
      do
       IDENTIFIER=$(/usr/libexec/PlistBuddy -c "print :MCMMetadataIdentifier" ${dir}/.com.apple.mobile_container_manager.metadata.plist)
       if [ "${IDENTIFIER}" == "YOUR APPNAME HERE" ]; then
        cd -
        echo "${APPLICATION_DATA_FOLDER}/${dir}"
        exit 0
       fi
      done
      

      我将应用程序数据文件夹作为路径传递 - 这个:

      ~/Library/Developer/CoreSimulator/Devices/[DeviceID]/data/Containers/Bundle/Application/
      

      脚本遍历文件夹中的.plist 文件并尝试匹配我的应用程序名称

      【讨论】:

        【解决方案4】:

        我分两步完成:

        1. 查找或添加(保存在模拟器上)一些独特的文件。就我而言,它是扩展名为 .xlsx 的文件。
        2. 使用可以在终端运行的脚本:

          cd ~/Library/Developer/CoreSimulator/Devices/[DeviceId] && find . -name '*.xlsx'

        它返回找到的文件的路径,您可以在其中使用正则表达式获取适当的应用程序 ID,例如:

        ./data/Containers/Shared/AppGroup/5BBE0D19-83A3-41D5-81DA-971CB547B9D0/File Provider Storage/test_pdf_file1578673688647.xlsx
        

        【讨论】:

          猜你喜欢
          • 2019-02-07
          • 2020-03-26
          • 1970-01-01
          • 1970-01-01
          • 2021-02-12
          • 1970-01-01
          • 1970-01-01
          • 2013-04-18
          相关资源
          最近更新 更多