【问题标题】:WCSession File Transfer IssueWCSession 文件传输问题
【发布时间】:2016-04-15 06:17:04
【问题描述】:

我在从父设备向 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];
}

【问题讨论】:

  • 传输失败时是否调用发送端的didTransferFile委托回调?如果是这样,有什么错误吗?
  • @ccjensen didTransferFile 委托在每次天气文件是否在手表端收到时都会被调用。我即使可能尝试在 didTransferFile 中使用 if 语句,如下所示: if([fileTransfer] file]){ NSLog(@"SUCCESS"); else { NSLog(@"FAILED")} 但那里没有运气。
  • 我正准备写下我的答案,但想看看[FileSystem_Helper removeFile:] 做了什么?你能发布它的代码吗?
  • @ccjensen 代码正是裘德所说的,但我修复了 if 语句后的附加分号,问题仍然存在。请参阅更新后的问题。
  • 就像我说的,这大约有 50% 的时间有效。从字面上看,每隔一次我运行这些命令时,它就会在手表上播放音频。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 2013-05-15
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多