您好,我想知道如何在应用程序生命周期中只调用一次方法...我的应用程序应该从服务器下载一些文件,而我只需要执行一次;我的意思是每次安装一次
这是我的方法
//Download some images from server and save it into directory
- (void) downloadCovers {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[self saveFile:@"mag1" ofType:@"png" fromURL:@"http://myweb.com/mag1.png" inDirectory:documentsDirectory];
}
并且此方法将图像设置为 UIButton BG :
- (void)buttonsBGImage {
UIImage * bgMag1 = [self loadImage:@"mag1" ofType:@"png" inDirectory:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
[mag1 setBackgroundImage:bgMag1 forState:UIControlStateNormal];
NSLog(@"BG IS SET");
}
最佳答案
为什么不只测试文件是否存在于本地存储中!
//Download some images from server and save it into directory
- (void) downloadCovers {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathToImg = [NSString stringWithFormat:@"%@/mag1.png",documentsDirectory];
BOOL isExist = [[NSFileManager defaultManager]fileExistsAtPath:pathToImg];
if (!isExist) {
[self saveFile:@"mag1" ofType:@"png" fromURL:@"http://myweb.com/mag1.png" inDirectory:documentsDirectory];
}
}
关于iphone - iOS : Call a method just one time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9968310/