背景会话中的AlamoFire上传

时间:2016-03-05 14:45:09

标签: ios xcode swift nsurlsession alamofire

我试图在应用程序处于后台时将文件上传到我的服务器, 我的NSOperation设置如下:

class uploadOperation: NSOperation {

    weak var request: Alamofire.Request?

    override func main() {

        let task = backgroundTaskManager.sharedManager.backgroundTask

        request = task.upload(.POST, "http://foo.com", headers: nil, file: fileURL).responseJSON { response in
        }
    }

    override func cancel() {
        request?.cancel()
        super.cancel()
    }
}

我设置了后台会话配置。

class backgroundTaskManager: NSObject {

    class var sharedManager:backgroundTaskManager {
        struct Static {
            static let instance:backgroundTaskManager = backgroundTaskManager()
        }
        return Static.instance
    }

    lazy var backgroundTask: Alamofire.Manager = {
        let identifier = "com.MyAppName.background"
        return Alamofire.Manager(configuration: NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier))
    }()
}
在app appate

func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) {

    backgroundTaskManager.sharedManager.backgroundTask.backgroundCompletionHandler = completionHandler
}

这是我对“必需的背景模式”的设置: Required background modes

*我正在使用Alamofire 3.2.1 *

当我去后台应用程序上传1个文件然后停止,我做错了什么?

0 个答案:

没有答案