简体   繁体   中英

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). My code looks like this

In 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)

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

How can I make it work when the iPhone is in the background/ inactive/ in the locked state?

The documentation on transferFile(_:metadata:) clearly states:

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

So to answer your question, if the iPhone app is "inactive"; as in isReachable is false then the file transfer will not happen.


Ref:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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