2015年12月

往项目里面添加文件,文件夹的时候Create groups和Create folder references的区别

Create groups:在创建文件夹之后,其实只是创建了一个虚拟目录,便于组织文件,在实际安装的时候,会把文件夹下面的文件都放到应用程序根目录
Create folder references:这个就是真实的创建了文件夹了
感觉Create groups是个很方便的东西啊,在新建项目的时候勾选这个话,之后在项目里面改变位置也不会有什么问题

使用Cocapods管理开源库

1.初始化pod,进入到项目的目录下执行

pod init

执行完成之后会出现Podfile文件
2.编辑Podfile文件

# Uncomment this line to define a global platform for your project
# platform :ios, '8.0'
# Uncomment this line if you're using Swift
# use_frameworks!

target 'MagicBox' do
platform:ios,'7.0'#所有库支持的ios最低版本
pod 'AFNetworking','~>2.3.1'#开源库版本号
pod 'SVProgressHUD','~>1.0'
pod 'Cordova','~>3.5.0'
pod 'GCJSONKit','~>1.5.0'
pod 'CordovaPlugin-console','~>0.2.10'
pod 'CordovaPlugin-device','~>0.2.11'
pod 'CordovaPlugin-network-information','~>0.2.3'
end

target 'MagicBoxTests' do

end

target 'MagicBoxUITests' do

end

3.安装开源库

pod install

执行成功之后会显示

Updating local specs repositories
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (2.3.1)
Installing Cordova (3.5.0)
Installing CordovaPlugin-console (0.2.10)
Installing CordovaPlugin-device (0.2.11)
Installing CordovaPlugin-network-information (0.2.3)
Installing GCJSONKit (1.5.0)
Installing SVProgressHUD (1.1.3)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `DemoApp.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 7 dependencies from the Podfile and 7 total pods installed.

4.打开创建的DemoApp.xcworkspace,在桥接文件里面加入引入库文件的代码即可

#import "JSONKit.h"
#import "SVProgressHUD.h"
#import "CDV.h"
#import "AFNetworking.h"
#import "CDVLogger.h"
#import "CDVDevice.h"
#import "CDVConnection.h"

沙盒文件读写

//  ViewController.swift

import UIKit
//默认生成的主控制器类
class ViewController: UIViewController {
    //当控制器的视图类夹在完成时调用
    override func viewDidLoad() {
        super.viewDidLoad()
        self.testSandBoxFileWriterOperation()//调用写文件方法
        self.testSandBoxFileReadOperation()//调用读文件方法
        // Do any additional setup after loading the view, typically from a nib.
    }
    //系统触发内存警告的时候调用
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //写沙盒文件
    func testSandBoxFileWriterOperation()
    {
        //获取程序Home目录
        var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as Array<String>
        let documentPath:String? = paths[0]
        //判断是否获取成功
        if let _ = documentPath
        {
            //拼接实际文件路径
            let filePath = documentPath! + "/MagixBox.txt"
            //测试存储内容
            let content:NSArray = ["Hello,world","hahahahah","中文"]
            //写入文件,写入的内容是xml格式的
            let bRet = content.writeToFile(filePath, atomically: true)
            //打印写入结果
            if bRet == true
            {
                print("成功")
            }
            else
            {
                print("失败")
            }
        }
    }
    //读取沙盒文件内容
    func testSandBoxFileReadOperation()
    {
        //获得程序Home路径
        var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as Array<String>
        let documentPath:String? = paths[0]
        //判断是否获取成功
        if let _ = documentPath
        {
            //拼接文件路径
            let filePath = documentPath! + "/MagixBox.txt"
            //读取内容,这里的到的内容是一个可选类型
            let fileContent = NSArray(contentsOfFile: filePath)
            print(fileContent!)
        }

    }

}

查询沙盒路径

//
//  ViewController.swift
//  持久化数据-ns
//
//  Created by admin on 16/1/19.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 应用程序根目录
        let home = NSHomeDirectory()
        // 保存应用需要持久化,并且需要在不同设备间同步的数据,itunus会备份这个目录
        let document = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        // 获得libray目录路径
        let library = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        // 缓存文件路径,itunus不会备份此目录,存放需要持久化,但是不需要在不同设备间同步的数据
        let caches = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        // 临时文件路径
        let temp = NSTemporaryDirectory()
        print(home)
    }
}

ViewController文件

记录下

//  ViewController.swift

import UIKit
//默认生成的主控制器类
class ViewController: UIViewController {
    //当控制器的视图类夹在完成时调用
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    //系统触发内存警告的时候调用
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //


}