WCSession文件传输问题

时间:2016-01-11 01:52:51

标签: ios apple-watch file-transfer watch-os-2 wcsession

我在从父设备向Apple Watch发送文件时遇到问题。有时文件会通过并完全解析。其他时候它开始文件传输,但它失败了,甚至从未在Apple Watch上重新编写方法session:(WCSession *)session didReceiveFile:(WCSessionFile *)file

以下是父设备代码:

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

以下是Apple Watch代码:

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

我甚至不确定它与代码本身有什么关系。我认为文件传输有时会失败,但我找不到任何地方打印出错误来调试。

有人能在这里给我一些见解吗?

iOS 9.2&& WatchOS 2.1

以下是[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); 
          } 
     }
}

这里是[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];
}

1 个答案:

答案 0 :(得分:0)

watchOS 2.2上的文件传输有问题。它可能会在下一个版本中修复。有关更多证词,请参阅https://forums.developer.apple.com/thread/43596