2016-03-28 1 views
0

J'ai une application iOS qui a une extension watchOS, toutes deux construites en Objective-C. L'application doit envoyer sur un tableau lorsqu'elle se charge (viewDidLoad) ou apparaît (viewDidAppear:(BOOL)animated). Lorsque l'application se charge, rien ne se passe, mais si j'ajoute un fichier à l'application (soit via l'additionneur intégré, soit en l'important depuis mon bureau ou AirDrop), l'application de surveillance se met à jour. Si vous appuyez sur le bouton d'actualisation, l'application ne fonctionne pas. Cependant, une fois que j'ajoute un fichier, il se met à jour correctement et démarre la mise à jour parfaitement. Je ne sais pas pourquoi cela arrive, mais voici un code que j'ai utilisé. Je découpe un code moins utile.Les informations ne sont pas envoyées à Apple Watch sur viewDidLoad

En 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]; 
} 

En -(void)connectToWatch, il y a

-(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"); 
    } 
} 
} 

Dans -(void)sendStuffToWatch

-(void)sendStuffToWatch 
{ 
if(self.watchSession) 
{ 
    NSError *error; 
    NSDictionary *applicationDict = @{@"array":recipes}; 
    [self.watchSession updateApplicationContext:applicationDict error:&error]; 
    NSLog(@"sent info to watch"); 
} 
} 

En -(void)viewDidAppear:(BOOL)animated, il n'y a que cette

-(void)viewDidAppear:(BOOL)animated 
{ 
[self sendStuffToWatch]; 
} 

Le code pour le bouton de rafraîchissement est

-(IBAction)Refresh:(id)sender 
{ 
[recipeTable reloadData]; 
[self sendStuffToWatch]; 
[self viewDidLoad]; 
} 

Répondre

0

Vos méthodes sont hors d'usage. Vous voulez d'abord appeler super, puis configurer la connectivité, filtrer le tableau, puis enfin envoyer les données à la montre.

[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]; 

En outre, vous ne devriez pas appellerez viewDidLoad dans votre propre code. Il ne doit être appelé qu'une seule fois, pour vous, pas pour vous, lorsque le contrôleur de vue est chargé en mémoire.

Si vous avez du code que vous souhaitez réexécuter, supprimez-le de viewDidLoad dans sa propre méthode et appelez simplement cette méthode.

+0

De plus, si vous voulez forcer le 'view' à charger et' viewDidLoad' à être appelé, vous pouvez appeler 'loadViewIfNeeded'. – EmilioPelaez

+0

Malheureusement, cela n'a pas fonctionné, mais pour l'appel de '' viewDidLoad'' dans le bouton d'actualisation, où j'ai besoin de tout recharger, devrais-je prendre/everything/from '' viewDidLoad'' et coller le tout dans leur propre méthodes? – JustMe

+0

Oui (tout sauf appeler super). Comme cela ne fonctionne pas, vous devriez pouvoir utiliser le débogueur, parcourir votre code et vous assurer qu'aucune erreur ne se produit. Comme cela ne fonctionne pas initialement, mais fonctionne lorsque vous ajoutez un fichier, alors quelque chose n'est pas configuré ou prêt la première fois qu'il est exécuté. Vous devez juste déterminer quelle partie est à l'origine du problème. –