首页 > 代码库 > iOS Bundle的简单使用

iOS Bundle的简单使用

class ViewController1: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //获得整个App的Bundle对象
        let mainBundle = Bundle.main
        
        //通过Bundle对象获得这个App的bundle标识: com.linyufeng.segueTest
        print(mainBundle.bundleIdentifier ?? "无法显示bundleIdentifier")
        
        //通过Bundle对象获得这个App的相对路径: /Users/linyufeng/Library/Developer/CoreSimulator/Devices/114FBDEE-A305-46FD-B324-A5C88845BB5D/data/Containers/Bundle/Application/3F857297-396F-4975-B73A-0DE463EA89AB/segueTest.app
        print(mainBundle.bundlePath)
        
        //通过Bundle对象获得这个App的绝对路径: file:///Users/linyufeng/Library/Developer/CoreSimulator/Devices/114FBDEE-A305-46FD-B324-A5C88845BB5D/data/Containers/Bundle/Application/3F857297-396F-4975-B73A-0DE463EA89AB/segueTest.app/
        print(mainBundle.bundleURL)
        
        //通过Bundle对象获得这个App的本地化信息(我在Info.plist设置的是China): zh_CN
        print(mainBundle.developmentLocalization ?? "无法显示developmentLocalization")
        
        //通过Bundle对象获得这个App的可执行文件的相对地址: /Users/linyufeng/Library/Developer/CoreSimulator/Devices/114FBDEE-A305-46FD-B324-A5C88845BB5D/data/Containers/Bundle/Application/3F857297-396F-4975-B73A-0DE463EA89AB/segueTest.app/segueTest
        print(mainBundle.executablePath ?? "无法显示executablePath")
        
        //通过Bundle对象获得Info.plist文件的消息(Info.plist是字典形式存储的,虽然我们在设置Info.plist时是将 Bundle Display Name 设为 网易云,但真正的键名是:CFBundleDisplayName),这里控制台的输出是:网易云
        print(mainBundle.infoDictionary!["CFBundleDisplayName"] ?? "无法显示Info.plist文件信息")
        
        
        //Bundle初始化
        //这里的输出是true,说明使用Bundle.main和使用Bundle的其他初始化函数都能获得Bundle对象,从而获得获得资源的接口
        let bundle1 = Bundle(for: type(of:self))
        let bundle2 = Bundle(identifier: "com.linyufeng.segueTest")
        let bundle3 = Bundle(path: mainBundle.bundlePath)
        let bundle4 = Bundle(url: mainBundle.bundleURL)
        print(mainBundle ==  bundle1 && mainBundle ==  bundle2 && mainBundle ==  bundle3 && mainBundle ==  bundle4)
        
        
    }

 

iOS Bundle的简单使用