我有一个带有 watchOS 扩展的 iOS 应用程序,它们都是用 Objective-C 构建的。viewDidLoad
应用程序在加载 ( ) 或出现 ( )时需要发送一个数组viewDidAppear:(BOOL)animated
。当应用程序加载时,什么也没有发生,但如果我以某种方式将文件添加到应用程序(通过内置加法器或从我的桌面或 AirDrop 导入文件),手表应用程序会更新。按刷新按钮不会使应用程序工作。但是,一旦我添加了一个文件,它就会很好地更新并开始完美更新。我不确定为什么会发生这种情况,但这是我使用的一些代码。我删掉了一些不太有用的代码。
在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];
}
中-(void)connectToWatch
,有
-(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");
}
}
}
在-(void)sendStuffToWatch
-(void)sendStuffToWatch
{
if(self.watchSession)
{
NSError *error;
NSDictionary *applicationDict = @{@"array":recipes};
[self.watchSession updateApplicationContext:applicationDict error:&error];
NSLog(@"sent info to watch");
}
}
中-(void)viewDidAppear:(BOOL)animated
,只有这个
-(void)viewDidAppear:(BOOL)animated
{
[self sendStuffToWatch];
}
刷新按钮的代码是
-(IBAction)Refresh:(id)sender
{
[recipeTable reloadData];
[self sendStuffToWatch];
[self viewDidLoad];
}