2015-05-08 47 views
0

我正在爲iOS編寫FileProvider擴展(NSFileProviderExtension)。當調用「itemChangedAtURL」時,我想從NSURL獲取數據並將其提交回我的服務器。我遇到的問題是我將使用Alamofire進行請求調用,並且請求永遠不會觸發。我曾嘗試設置backgroundSessionConfigurationWithIdentifier並使用sharedContainerIdentifier,但沒有任何工作。相同的alamofire調用在FileProvider擴展之外工作。有誰知道我到底需要設置NSURLSession或Alamofire才能使其正常工作?無法從iOS上的FileProvider擴展創建網絡請求

回答

0

我遇到了AFNetworking這個類似的問題。我決定根據我在http://z43studio.com/2015/04/storage-providers/

override func itemChangedAtURL(url: NSURL) { 
    var manager = AFHTTPSessionManager() 
    var request = manager.requestSerializer.multipartFormRequestWithMethod("PUT", URLString: "http://somewhere.awesome", parameters: nil, constructingBodyWithBlock: { (data: AFMultipartFormData!) -> Void in 
      var res = data.appendPartWithFileURL(fileUrl, name: "custom_name", error: nil) 
    }, error: nil) 

    var semaphore = dispatch_semaphore_create(0)   
    let task:NSURLSessionDataTask = manager.uploadTaskWithStreamedRequest(request, progress: nil, 
     completionHandler: {(response: NSURLResponse!, responseObject: AnyObject!, error: NSError!) -> Void in 
      if ((error) != nil) { 
       println("ERROR") 
       // Attempt to recover - otherwise release 
       dispatch_semaphore_signal(semaphore) 
      } else { 
       println("SUCCESS") 
       dispatch_semaphore_signal(semaphore) 
      } 

    }) 

    task.resume() 
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) 
}