WCSession.transferFile Not Working

Hi All,


I have implemented a system for transfering files from an Apple Watch app to the host iOS app. Below is the Apple Watch code that succeeds:


- (void)sendAudioAtURL:(NSURL *)audioURL
{
    if ([WCSession isSupported] && [audioURL checkResourceIsReachableAndReturnError:nil]) {
        [[WCSession defaultSession] transferFile:audioURL metadata:nil];
    }
}
- (void)session:(WCSession *)session didFinishFileTransfer:(WCSessionFileTransfer *)fileTransfer error:(nullable NSError *)error
{
    NSLog(@"Finished file transfer: %@ error: %@", fileTransfer, error);
    [[NSFileManager defaultManager] removeItemAtURL:fileTransfer.file.fileURL error:nil];
}


When I debug against the Apple Watch, I see didFinishFileTransfer method being called with no error. The issue is, in my iOS app, despite the fact that I activate my session on startup, the received transfer delegate callback never occurs:


- (void)configureWatchConnectivity
{
    if ([WCSession isSupported]) {
        CLS_LOG(@"Configuring WCSession...");
        [WCSession defaultSession].delegate = self;
        [[WCSession defaultSession] activateSession];
    }
}
- (void)session:(WCSession *)session didReceiveFile:(WCSessionFile *)file
{
// NEVER CALLED
}


Has anyone successfullly transfered a file via WCSession from Apple Watch -> iPhone? If so, would you mind sharing how you did it?


Thanks!

Hi,


Sorry to hear things aren't working for you. Sounds like you are hitting a bug which you can reproduce very easily. It'd be great if you could file a bug report, include a small sample project where the problem reproduces and report back here with the number. I'll then make sure it gets routed to the right engineers.

Is this still a bug in beta 6? I am using the nearly the same code but it is not firing for me either.


App Code:

    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session.delegate = self;
        session.activateSession()
     }
    let bundle = NSBundle.mainBundle()
    let modelURL = bundle.URLForResource("Sunday", withExtension: "momd")!

    let fileTransfer = WCSession.defaultSession().transferFile(modelURL, metadata:nil)
    let outstanding = WCSession.defaultSession().outstandingFileTransfers


fileTransfer.transferring = false and outstanding = 0, however my didReceiveFile doesn't fire.


Here is my Watch Code.

override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
     
        if WCSession.isSupported() {
            let session = WCSession.defaultSession()
            session.delegate = self
            session.activateSession()
   }

    func session(session: WCSession, didReceiveFile file: WCSessionFile) {
     print("Hello") //Never called
    }

Hello


Same here, working with beta5

this callback:

https://github.com/theothertomelliott/WatchConnectivityTutorial/blob/master/Swift/WatchConnectivityExample%20WatchKit%20Extension/InterfaceController.swift#L21

is never called when deployed on device. In simulators, it works just fine.


Puzzled.

I've tried unpairing/repairing without much success.


any help welcome

++

Corinne

Has anyone gotten this working as of the GM? Still seeing this issue.

I strongly suggest, and pleasantly request, you file a bug if you are seeing things unexpectedly fail! Report back here with the radar number and I'll make sure they get prompt attention.

I was having the same problem where didReceiveFile was never being called.


In my case, I was creating the url from a filePath string:

[session transferFile:[NSURL URLWithString:filePath] metadata:nil];


where the filePath looked something like this:

/Users/DD/Library/Developer/CoreSimulator/Devices/<guid>/data/Containers/Data/Application/<guid>/Library/Caches/4b01dde41c331b1.png


The way I finally ended up getting it to work was to prepend "file://" infront of the filePath:

[session transferFile:[NSURL URLWithString:[NSString stringWithFormat:@"file://%@", filePath]] metadata:nil];


so that the url for transferFile became this:

file:///Users/DD/Library/Developer/CoreSimulator/Devices/<guid>/data/Containers/Data/Application/<guid>/Library/Caches/4b01dde41c331b1.png


... and then didReceiveFile magically started getting called!

glad you figured it out! A better fix would be:

[session transferFile:[NSURL fileURLWithPath:filePath] metadata:nil];


before you applied the fix, were you getting an error returned in the delegate callback:

- (void)session:(WCSession *)session didFinishFileTransfer:(WCSessionFileTransfer *)fileTransfer error:(nullable NSError *)error;

on the sending side?

You're totally right, now that i've got it working i'll go back in and clean up a bunch of stuff including using fileURLWithPath instead 🙂


Here is the error I was receiving:

WCErrorDomain Code 7013

WatchConnectivity session cannot access the file.

Invalid file path, or insufficient permissions to access the file.

I am using Watch OS2.0. I have tried on device and on simulator. I am unable to get didReceiveFile called on app side for some reason. However, didFinishFileTransfer is getting called on the watch side.


Code at Watch Side:

@interface ViewController ()<WCSessionDelegate>

@property (strong, nonatomic) WCSession *session;

@end



@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

if ([WCSession isSupported]) {

self.session = [WCSession defaultSession];

self.session.delegate = self;

[self.session activateSession];

}

}


- (void)session:(WCSession *)session didReceiveFile:(WCSessionFile *)file {

NSLog(@"File has been received:%@", file.fileURL);

}


@end



Code at App Side:


-(void)transferFile {

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"txt"];

if (filePath) {

NSURL *path = [NSURL fileURLWithPath:filePath];

[self.session transferFile:path metadata:dict];

}

}


- (void)session:(WCSession *)session didFinishFileTransfer:(WCSessionFileTransfer *)fileTransfer error:(nullable NSError *)error {

if (!error) {

NSLog(@"File transfer Successful");

}

else {

NSLog(@"File transfer failed");

}

}

How large is the file you are sending? Alternatively, file a bug report and reply here with the radar number and I'll make sure it gets prompt attention.

WCSession.transferFile Not Working
 
 
Q