简体   繁体   English

如果iPhone App处于非活动状态,是否可以响应通过Apple Watch传输的文件?

[英]Can the iPhone App respond to file transferred via Apple watch if it is inactive?

I am developing an apple watch application which records an audio file, saves it and then transfers the file URL to the iPhone app via WCSession (Watch Connectivity framework). 我正在开发一个Apple Watch应用程序,该应用程序记录一个音频文件,将其保存,然后通过WCSession(Watch Connectivity框架)将文件URL传输到iPhone应用程序。 My code looks like this 我的代码看起来像这样

In InterfaceController.m 在InterfaceController.m中

NSURL *directory = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.name.watchtest"];
__block NSString *recordingName = @"myTestFile.mp4";
__block NSURL * outputURL = [directory URLByAppendingPathComponent:recordingName];

if ([WCSession isSupported]) {
   if ([self.watchSession isReachable]) {
         [self.watchSession transferFile:outputURL metadata:nil];
   }
}

In ViewController.m (WCSession delegate) 在ViewController.m中 (WCSession委托)

-(void)session:(WCSession *)session didReceiveFile:(WCSessionFile *)file
{
    NSError *error;
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                            NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    NSFileManager *filemgr = [NSFileManager defaultManager];
    NSString *filePath = [docsDir stringByAppendingString:@"/myTestFile.mp4"];

    [filemgr moveItemAtPath:file.fileURL.path toPath:filePath error:&error];

    if ([filemgr fileExistsAtPath:file.fileURL.path]) {
        urlOfAudioFile = [[NSURL alloc] initFileURLWithPath:filePath];
        [self uploadToServer:urlOfAudioFile];
    }
}

This works absolutely fine if both the WatchApp & the iPhone App are Active. 如果WatchApp和iPhone App都处于活动状态,则此方法绝对正确。

How can I make it work when the iPhone is in the background/ inactive/ in the locked state? 当iPhone处于后台/非活动/处于锁定状态时,如何使它工作?

The documentation on transferFile(_:metadata:) clearly states: 关于transferFile(_:metadata:)文档明确指出:

Use this method to send a file that is local to the current device. 使用此方法发送当前设备本地的文件。 Files are transferred to the counterpart asynchronously on a background thread. 文件在后台线程上异步传输到对方。 The system attempts to send files as quickly as possible but may throttle delivery speeds to accommodate performance and power concerns. 该系统尝试尽快发送文件,但可能会限制传输速度以适应性能和功耗方面的考虑。 Use the outstandingFileTransfers method to get a list of files that are queued for delivery but have not yet been delivered to the counterpart. 使用杰出的文件传输方法来获取已排队等待传递但尚未传递给对应文件的文件列表。

... ...

This method can only be called while the session is active—that is, the activationState property is set to activated. 仅在会话处于活动状态时才能调用此方法,即,activationState属性设置为Activated。 Calling this method for an inactive or deactivated session is a programmer error. 为非活动或停用的会话调用此方法是程序员错误。

So as per your code: 因此,根据您的代码:

if ([WCSession isSupported]) {
   if ([self.watchSession isReachable]) {
         [self.watchSession transferFile:outputURL metadata:nil];
   }
}

If the isSupported & isReachable checks fail, then basically WCSession is inactive and your code will not reach the transferFile(_:metadata:) part. 如果isSupportedisReachable检查失败,则基本上WCSession处于不活动状态,并且您的代码将不会到达transferFile(_:metadata:)部分。
This is the correct behavior and you would have to handle this case manually. 这是正确的行为,您必须手动处理这种情况。

But... when you have a valid session and transferFile(_:metadata:) does get called then whether the iPhone is locked, or the app is in background, or even if the app is not running, it will receive the file via a background thread. 但是...当您拥有有效的会话并调用transferFile(_:metadata:) ,无论iPhone是锁定的,还是应用程序处于后台,或者即使应用程序未运行,它也会通过以下方式接收文件:背景线程。

So to answer your question, if the iPhone app is "inactive"; 因此,如果iPhone应用程序处于“非活动状态”,请回答您的问题; as in isReachable is false then the file transfer will not happen. isReachable为false,则文件传输不会发生。


Ref: 参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM