本文介绍了iOS:只调用一次方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我想知道如何才能调用一个方法只有一次在应用程序的生活...我的应用程序应该从服务器下载一些文件,我需要做一次;我的意思是每次安装只有一次
Hi I was wondering how can I call a method just for one time in application life ... My application should download some files from server and I need do it just for one time; I mean mean just one time per installation
这里是我的方法
//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:
and this method set images as 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");
}
推荐答案
标志作为NSUserDefaults键,并在您的downloadCovers方法中检查此NSUserDefault值。如果已设置,则不执行任何操作,否则下载文件并将标记设置为true。
Set a flag as a NSUserDefaults key and check for this NSUserDefault value in your downloadCovers method. If it is already set, do nothing, else download files and set the flag to true.
像这样:
-(void) downloadCovers {
BOOL downloaded = [[NSUserDefaults standardUserDefaults] boolForKey: @"downloaded"];
if (!downloaded) {
//download code here
[[NSUserDefaults standardUserDefaults] setBool:YES forKey: @"downloaded"];
}
}
干杯
这篇关于iOS:只调用一次方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!