Search code examples
iosapple-watchfile-transferwatchos-2wcsession

WCSession File Transfer Issue


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];
}

Solution

  • 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.