iphone - iOS : Call a method just one time

标签 iphone objective-c ios xcode cocoa-touch

您好,我想知道如何在应用程序生命周期中只调用一次方法...我的应用程序应该从服务器下载一些文件,而我只需要执行一次;我的意思是每次安装一次

这是我的方法

//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/

相关文章:

iphone - 检测 iOS5 上何时禁用蓝牙

iphone - uiview屏幕淡入淡出

ios - 在 iOS8 中显示带有 UIActionSheet 的 UIPickerView 不起作用

ios - 如何更改 iOS 应用程序的显示名称?

android - Bootstrap 下拉菜单不适用于 Android

iphone - [NSString stringByReplacingOccurrencesOfString : @"""withString: @""""];

iphone - 关于在应用程序代码中检查基础 sdk

iphone - 在iPhone SDK中使用套接字通信黑白两个应用程序

ios - 为什么CALayer的动态@property可以调用setter方法?

ios - 在 iOS 8 中使用 Storyboard 时 FBLoginView 不显示