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?