WatchOS 2 app 与 Iphone app 数据交互(三3)文件传输

3.3 文件传输

(1)发送文件

发送文件也是后台模式。

发送文件函数transferFile:metadata:声明如下:

Declaration

SWIFT

func transferFile(_ file: NSURL,

        metadata metadata: [String : AnyObject]?) -> WCSessionFileTransfer

OBJECTIVE-C

- (WCSessionFileTransfer*)transferFile:(NSURL *)file

                              metadata:(NSDictionary

                                                id> *)metadata

Parameters

file      

A file-based URL that identifies the fileto send. The specified file must be readable by the current app. This parametermust not be nil.

metadata       

An optional dictionary containingadditional data to send. The values of the dictionary must all be property listobject types. You may specify nil for this parameter.

Return Value

A file transfer object containing the fileand dictionary being sent. You can use this object to cancel the file transferat a later time.

 

(2)接收文件

接收文件后存放在WCSessionFileTransfer中,通过如下函数获取:

Declaration

SWIFT

var outstandingFileTransfers:[WCSessionFileTransfer] { get }

OBJECTIVE-C

@property(nonatomic, readonly, copy)NSArray *outstandingFileTransfers

 

(3)文件处理

在接到一个文件后,会调用委托函数session:didReceiveFile:,处理文件代码就要写在改函数中。需要强调的是,如果在该函数中找另外一个地方来保存收到的文件,那么这个文件会被删除,不再存放在WCSessionFileTransfer,因为WCSessionFileTransfer只有一个,要用于接收下一个收到文件。

你可能感兴趣的:(Apple,Watch开发技术)