Mc.*_*ver 0 iphone xcode cocoa-touch objective-c ios
嗨,我想知道如何在应用程序生命中只调用一次方法...我的应用程序应该从服务器下载一些文件,我只需要做一次; 我的意思是每次安装只需要一次
这是我的方法
//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];
}
Run Code Online (Sandbox Code Playgroud)
并且此方法将图像设置为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");
}
Run Code Online (Sandbox Code Playgroud)
小智 6
为什么不测试文件是否存在于本地存储中!
//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];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4893 次 |
| 最近记录: |