尝试使用 Watch OS 2 发送图像时出现 WCErrorDomain 7013

WCErrorDomain 7013 when trying to send image using Watch OS 2

我正在测试 Apple Watch OS 2,我正在尝试将图像从应用程序发送到手表。根据 Apple 的说法,我将使用 WCSession transferFile 来执行此操作。

Use the transferFile:metadata: method to transfer files in the background. Use this method in cases where you want to send more than a simple dictionary of values. For example, use this method to send images or file-based documents.

例如:

NSString *string = [[NSBundle mainBundle] pathForResource:@"my_image" ofType:@"png"];
NSURL *path = [NSURL URLWithString:string];

[[WCSession defaultSession] transferFile:path metadata:@{@"meta1":@"meta2"}];

在调试器中一切正常,路径正确,文件可访问(使用 NSFileManager 检查)和可读。

但是,每次我尝试时都会收到 didFinishFileTransfer 函数的回调,包括一个错误:

Error Domain=WCErrorDomain Code=7013 "The operation couldn’t be completed. (WCErrorDomain error 7013.)"

查找错误:

WCErrorCodeFileAccessDenied An error indicating that a file could not be transferred because it was inaccessible. Available in watchOS 2.0 and later.

发送功能似乎无法访问该文件?我试过将文件重新保存到另一个目录等,但似乎没有任何效果。

有人知道吗?

我成功解决了问题!

因为我的路径不是以file://

开头

以下代码运行良好:

NSString *string = [[NSBundle mainBundle] pathForResource:@"my_image" ofType:@"png"];
string = [NSString stringWithFormat:@"file://%@", string];    
NSURL *path = [NSURL URLWithString:string];

[[WCSession defaultSession] transferFile:path metadata:@{@"meta1":@"meta2"}];

所以对路径比较挑剔

您正在创建的 URL 不是文件URL。尝试:

NSURL *path = [NSURL fileURLWithPath:string];