I have an iOS app that has a watchOS extension, both of which are build in Objective-C. The app needs to send over an array when it loads up (viewDidLoad
) or appears (viewDidAppear:(BOOL)animated
). When the app loads up, nothing happens, but if I somehow add a file to the app (either through the built-in adder or by importing one from my desktop or AirDrop), the watch app updates. Pressing the refresh button does not make the app work. However, once I add a file, it updates fine and starts updating perfectly. I'm not sure why this happens, but here is some code I used. I cut out some less useful code.
In viewDidLoad
:
- (void)viewDidLoad
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appFolderPath = [path objectAtIndex:0];
NSString *inboxAppFolderPath = [appFolderPath stringByAppendingString:@"/Inbox"]; //changes the directory address to make it for the inbox
//move all files from inbox to documents root
//get to inbox directory
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *inboxContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:inboxAppFolderPath,documentsDirectory] error:nil];
//move all the files over
for (int i = 0; i != [inboxContents count]; i++)
{
NSString *oldPath = [NSString stringWithFormat:@"%@/%@", inboxAppFolderPath, [inboxContents objectAtIndex:i]];
//NSLog(oldPath);
NSString *newPath = [NSString stringWithFormat:@"%@/%@", appFolderPath, [inboxContents objectAtIndex:i]];
//NSLog(newPath);
NSError* error;
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:&error];
NSLog(@"error: %@", error.localizedDescription);
}
//end of moving all files
//Turn every file inside the directory into an array
//add directory in case it doesn't already exist
if (![[NSFileManager defaultManager] fileExistsAtPath: inboxAppFolderPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath: inboxAppFolderPath withIntermediateDirectories:NO attributes:nil error:nil];
NSLog(@"directory created");
}
//a bunch of NSPredicates go here to filter out the array
[self sendStuffToWatch];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self connectToWatch];
}
In -(void)connectToWatch
, there is
-(void)connectToWatch
{
//set up WatchConnectivity session
if([WCSession isSupported])
{
self.watchSession = [WCSession defaultSession];
self.watchSession.delegate = self;
[self.watchSession activateSession];
if([WCSession defaultSession].paired){
NSLog(@"Watch session active");
}
else
{
NSLog(@"WATCH NOT CONNECTED");
}
}
}
In -(void)sendStuffToWatch
-(void)sendStuffToWatch
{
if(self.watchSession)
{
NSError *error;
NSDictionary *applicationDict = @{@"array":recipes};
[self.watchSession updateApplicationContext:applicationDict error:&error];
NSLog(@"sent info to watch");
}
}
In -(void)viewDidAppear:(BOOL)animated
, there's only this
-(void)viewDidAppear:(BOOL)animated
{
[self sendStuffToWatch];
}
The code for the refresh button is
-(IBAction)Refresh:(id)sender
{
[recipeTable reloadData];
[self sendStuffToWatch];
[self viewDidLoad];
}