IOS开发Swift使用NSURLSessionUploadTask实现后台上传功能

本文介绍了如何在Swift中利用NSURLSessionUploadTask实现在iOS设备上进行后台文件上传,同时提及服务器端使用nginx和tocat的情况。
摘要由CSDN通过智能技术生成

服务器的前端用的是nginx 后端是tocat,具体不知什么个原理没有接触,不过nginx是支持我这样上传的

1.代码类:

//
//  JPCUploadHelper.swift
//  joopic
//
//  Created by jianxiong li on 16/4/9.
//  Copyright © 2016年 joobot. All rights reserved.
//

import Foundation

protocol JPCUploadStatusDelegate{
    func  bytesSent(key:String,bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64);
    func  complete(key:String,error:NSError?,jesionStr:String?);
}

var upHelpers:NSMutableDictionary = NSMutableDictionary()

class JPCUploadHelper:NSObject{
    
    var session:NSURLSession?
    var delegate:JPCUploadStatusDelegate?
    var key:String!
    
    var backgroundCompletionHandler:(()->())?
    
    
    private static let JPC_UPLOAD_IDENTIFIER_PRE:String = "IDENTIFIER_PRE"
    
    
    let boundary = "AF0xKhTmLbOuNdArY"
    
    
    class func uploadHelper(key:String)->JPCUploadHelper{
        let identifier = "\(JPC_UPLOAD_IDENTIFIER_PRE)_\(key)"
        let helper:JPCUploadHelper? = upHelpers.objectForKey(identifier) as? JPCUploadHelper
        print("===============================uploadHelper.......helper:\(helper)")
        if(helper == nil){
            return JPCUploadHelper(key: key)
        }else{
            return helper!
        }
    }
    
    class func hasUploadHelper(identifier:String)->JPCUploadHelper?{
        let helper:JPCUploadHelper? = upHelpers.objectForKey(identifier) as? JPCUploadHelper
        return helper
    }
    
    
    //指定构造器
    private init(key:String){
        super.init()
        self.key = key
        if(self.session == nil){
            let identifier = self.getIdentifier(key)
            let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier)
            
            //config.discretionary = true
            config.allowsCellularAccess = SafeUtil.getBoolean(KEY_SAVE_IMAGE_ON_2G_3G_NET, delf: false)//是否允许使用蜂窝连接
            //print("JPCDownloadHelper init.......config.allowsCellularAccess:\(config.allowsCellularAccess) config.discretionary:\(config.discretionary)")
            
            self.session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
            upHelpers.setObject(self, forKey: identifier)
        }
    }
    
    
    
    
    private func getRequest(url:NSURL)->NSMutableURLRequest{

        
        let req = NSMutableURLRequest(URL: url)
        
        //1 MARK: >必须加参数  时间戳和鉴权 已经省略
        //req.setValue("Bearer \(base64String!)", forHTTPHeaderField: "Authorization")
        req.HTTPMethod = "POST"
        
        //  Add the header info
        req.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
        
        return req
    }
    

    //这个方法可能改成中上传一个图片的方法,去掉参数midFilepath  minFilePath
    //->2 Uploading Body Content Using a Stream 使用uploadTaskWithStreamedRequest(request) 上传
    func uploadFile(ObjectFilePath: String,MidFilePath: String,MinFilePath: String,var reqURL:NSURL?,delegate:JPCUploadStatusDelegate){
        print("=======================================================================================&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值