0

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];
}
JustMe
  • 306
  • 1
  • 4
  • 15

1 Answers1

0

Your methods are all out of order. You want to first call super, then setup connectivity, filter the array, and then finally send the data to the watch.

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self connectToWatch];

// rest of code here to create recipe array

[self sendStuffToWatch];

Also, you shouldn't be calling viewDidLoad in your own code. It should only be called once, for you, not by you, when the view controller is first loaded into memory.

If you have code you want to rerun, then move it out of viewDidLoad into its own method, and just call that method.

  • Also, if you want to force the `view` to load and `viewDidLoad` to be called, you can call `loadViewIfNeeded`. – EmilioPelaez Mar 28 '16 at 04:55
  • Unfortunately, it didn't work, but for the calling of ``viewDidLoad`` in the refresh button, where I need to reload everything, should I take /everything/ from ``viewDidLoad`` and stick it all into their own methods? – JustMe Mar 28 '16 at 05:50
  • Yes (everything except for calling super). As for it not working, you should be able to use the debugger, step through your code, and make sure that no errors are occurring. Since it doesn't work initially, but works when you add a file, then something isn't set up or ready the first time it's run. You've just got to figure out which part is causing the problem. –  Mar 28 '16 at 06:36