我正在尝试传输从 facebook 下载的图像,但由于某种原因,我的 transferFile 方法已停止工作。相同的代码似乎在一小时前可以工作,但我找不到我所做的更改以使其停止工作。
我没有从委托方法中得到错误,也没有在手表端调用我的日志。
这是我下载图像和传输的方式。
func downloadImageToFile(urlString : String, completion: (NSURL?, NSError?) -> Void) {
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession.init(configuration: config)
let imageURL = NSURL(string: urlString)
session.downloadTaskWithURL(imageURL!, completionHandler:{(locationURL : NSURL?, response: NSURLResponse?, error: NSError?) in
if (error != nil) {
print("Error: " + (error?.description)!)
completion(nil, error)
}
else {
if let data = NSData(contentsOfURL: locationURL!) {
let documentsDirect = self.documentDirectory()
let saveLocation = documentsDirect.URLByAppendingPathComponent("GCD.png")
let saveWasSuccessfull : Bool = data.writeToURL(saveLocation, atomically: true)
dispatch_async(dispatch_get_main_queue(),{
if (saveWasSuccessfull) {
print("Save Was Successful")
completion(saveLocation, nil)
}
else {
completion(nil, error)
}
})
}
else {
print("Data Download Error")
}
}
}).resume()
}
func documentDirectory() -> NSURL {
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
if let documentDirectory: NSURL = urls.first! as NSURL {
return documentDirectory
}
}
ViewController.swift
let downloader = WebImageDownloader()
downloader.downloadImageToFile(string, completion: { (transferLocation, error) in
if (error != nil) {
}
else {
self.session.transferFile(transferLocation!, metadata: nil)
}
})
func session(session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: NSError?) {
if error != nil {
print(error?.description)
}
else{
print("Finished File Transfer Successfully")
}
}
手表
func session(session: WCSession, didReceiveFile file: WCSessionFile) {
print("File Recieved on Watch")
dispatch_async(dispatch_get_main_queue()) { [weak self] in
if let data = NSData(contentsOfURL: file.fileURL) {
self?.photos.append(data)
self?.updateTable()
}
else {
print(file.fileURL)
}
}
}
有什么想法吗?我没有错误记录以处理任何事情。