ios - WCSession 文件传输问题

标签 ios apple-watch file-transfer watchos-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];
}

最佳答案

文件传输在 watchOS 2.2 上存在错误。它可能会在下一个版本中修复。参见 https://forums.developer.apple.com/thread/43596获取更多证词。

关于ios - WCSession 文件传输问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713062/

相关文章:

ios - 没有得到 String 中 char 的正确索引

ios - 在 iOS 中实现本地服务器

iphone - 如何按整数对数组进行排序

ios - Swift:获取 AppDelegate 实例作为类函数会导致线程 1:EXC_BAD_INSTRUCTION

c++ - 没有用于初始化的匹配构造函数 - C++ 和 Objective C

ios - 苹果 watch : how to show image in Glance

ios - 反馈振动像苹果呼吸一样 swift

iOS 错误 : How allowed WatchKit 2. 0? - 此应用程序包含多个 WatchKit 2.0 应用程序。只允许一个 WatchKit 2.0 应用程序

python - 使用python在电脑和手机之间通过wifi简单传输文件

python - 如何通过 Python 套接字播放 mp3 文件?