4

I am having an issue sending files to the Apple Watch from the parent device. Sometimes the file goes through and is parsed completely. Other times it begins the file transfer, but it fails and never even rens the method session:(WCSession *)session didReceiveFile:(WCSessionFile *)file on the Apple Watch.

Here is the parent device code:

- (void)sendLiveAudioRecording
{
    NSError *moveError;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *groupURL = [fileManager containerURLForSecurityApplicationGroupIdentifier: @"group.myCompany.myApp"];
    NSURL *permanentURL = [groupURL URLByAppendingPathComponent: @"PhoneToWatch.mp4"];
    [FileSystem_Helper removeFile: [permanentURL path]];
    [fileManager moveItemAtURL: [self fileURL] toURL: permanentURL error: &moveError];

    if (!moveError)
    {
        if ([WCSession isSupported])
        {
            [[WCSession defaultSession] setDelegate: self];
            [[WCSession defaultSession] activateSession];
            if ([[WCSession defaultSession] isReachable])
            {
                NSLog(@"File Is Being Transferred: %@", [permanentURL path]);
                [[WCSession defaultSession] transferFile: permanentURL metadata: nil];
            }
            else
            {
                [self createAlertWithTitle: @"Error" andMessage: @"WCSession Not Reachable"];
            }
        }
        else
        {
            [self createAlertWithTitle: @"Error" andMessage: @"WCSession Not Supported"];
        }
    }
    else
    {
        NSLog(@"%@", [moveError localizedDescription]);
    }
}

And here is the Apple Watch code:

-(void) session:(WCSession *)session didReceiveFile:(WCSessionFile *)file
{
    NSData *fileData = [NSData dataWithContentsOfURL: [file fileURL]];
    WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL: [file fileURL]];
    NSURL *fileLocation = [FileSystem_Helper writeAudioToAppGroupsWithData: fileData withName: @"FromPhone.mp4"];
    NSLog(@"%@", [file fileURL]);
    NSLog(@"%@", [fileLocation path]);

    [FileSystem_Helper removeFile: [[file fileURL] path]];

    NSError *writingError;
    if (!writingError)
    {
        [self playURL: fileLocation withDuration: [asset duration]];
    }
    else
    {
        [WKAlertViewController_Helper showWKAlertControllerWithTitle: @"Audio Receive Failed" andMessage: [writingError localizedDescription] andButtonTitle: @"Ok" onController: self];
    }
}

I am not even sure it has anything to do with the code itself. I think the file transfer is failing sometimes, but I can't find anywhere to print out an error to debug.

Can anyone give me some insight here?

iOS 9.2 && WatchOS 2.1

Here is the code from [FileSystem_Helper removeFile: (NSString *)filePath]:

+ (void) removeFile: (NSString *)filePath
{
     NSFileManager *fileManager = [NSFileManager defaultManager];
     if ([fileManager fileExistsAtPath: filePath])
     { 
          NSError *error; 
          if (![fileManager removeItemAtPath: filePath error:&error]) 
          {
                NSLog(@"Error removing file: %@", error); 
          } 
     }
}

And here is [FileSystem_Helper writeAudioToAppGroupsWithData]:

+ (void)writeAudioToAppGroupsWithData: (NSData *)audioData withName: (NSString *)name
{
    // Writing the audio data to the App Groups
    NSURL *URL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: @"group.myCompany.myApp"];
    NSURL *containerURL = [URL URLByAppendingPathComponent: name];
    [audioData writeToURL: containerURL atomically: YES];
}
S. Stark
  • 189
  • 1
  • 1
  • 11
  • Does the didTransferFile delegate callback on the sending side get called when the transfer fails? If so, any error? – ccjensen Jan 11 '16 at 04:31
  • @ccjensen The didTransferFile delegate gets called every time weather the file is received on the watch-side or not. I even though maybe trying an if statement in the didTransferFile like this: if([fileTransfer] file]){ NSLog(@"SUCCESS"); else { NSLog(@"FAILED")} but there was no luck there. – S. Stark Jan 11 '16 at 11:29
  • 1
    I'm preparing to write up my answer, but wanted to check what `[FileSystem_Helper removeFile:]` does? Could you post its code? – ccjensen Jan 11 '16 at 16:50
  • @ccjensen the code is exactly what Jude said, but I fixed the additional semicolon after the if statement and the problem still exists. See the updated question to follow. – S. Stark Jan 11 '16 at 20:57
  • Like I said, this works about 50% of the time. Literally every other time I run through these commands it will play the audio on the watch. – S. Stark Jan 11 '16 at 21:04
  • when the didFinishFileTransfer gets called and the file didn't arrive on the watch, is there a non-nil NSError? If so, what is the error? – ccjensen Jan 12 '16 at 00:40
  • @ccjensen No, the didFinishFileTransfer delegate method does not have an error. – S. Stark Jan 12 '16 at 03:35
  • Sounds like a bug in the OS. Did you file a bug report with Apple? Great if you could post the radar number here in case any apple engineers happen upon it – ccjensen Jan 12 '16 at 03:36
  • @ccjensen I edited the question to display the content of helper methods being used – S. Stark Jan 12 '16 at 03:37
  • @ccjensen radar #: 24143290 – S. Stark Jan 12 '16 at 04:08

1 Answers1

0

File transfer is buggy on watchOS 2.2. It may be fixed on the next release. See https://forums.developer.apple.com/thread/43596 for more testimonies.

Ben G
  • 3,965
  • 3
  • 30
  • 34