iOS tips and tricks: Dynamic Default.png

When developing for iOS, from time to time, customers, and some programmers, have a logical question: “Is it possible to replace the image in some way when the application starts?”.

After a little searching on the Internet, and rummaging through the documentation , the answer suggests that this is impossible for the following reasons:
  • The file that is displayed when the application starts is located inside the application folder, and therefore it is not possible to overwrite it
  • For a similar reason, you cannot modify the Info.plist file , in which the relative path to the file with the start image is saved.

However, nevertheless, it is possible to do this in standard ways, without any kind of Jailbreak. However, like every solution has its advantages, disadvantages and features.



Decision


Since we can’t change anything inside the folder with the application, we won’t even try. It is possible there are craftsmen who can do this, but this is not about them.

So, put Default.png in the Documents folder. Now, assuming that Default.png is located in this folder, put the relative path in Info.plist: Voila! We start. Everything is working. Hm. Suppose if everything is clear with the relative path to the Default.png file, then how to put Default.png in the Documents folder? Programmatically. When you first start the program, you must copy it from the same Bundle. In fact, there are already a couple of options here - download Default.png from the Internet, generate it on the fly, attach the photo you just took instead of Default.png. It all depends on the specifics of the application.



image






For example, it may look as follows: A feature (the main disadvantage) of this method is that splash will not be visible on first boot, because there is no way until it was found possible immediately when installing the application to write something to the Documents folder. PS Formally, this method does not violate the HIG , namely: ... All launch images must be PNG files and must reside in the top level of your application's bundle directory ... and ... When the system launches an application, it temporarily displays a static launch image on the screen ... So, using this method in the App-Store is a big question.
// получаем путь к папке Documents
NSArray* dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsPath = [dirs objectAtIndex:0];
NSString * splashDest = [documentsPath stringByAppendingPathComponent:@"Splash.png"];
NSString * splashSrc = [[NSBundle mainBundle] pathForResource:@"Splash" ofType:@"png"];

// Копируем Splash.png
NSFileManager * fm = [NSFileManager defaultManager];
[fm copyItemAtPath:splashSrc toPath:splashDest error:nil];






In addition, it is not clear how this method will work in case of application localization.

But, if all of the above suits you, then you can safely use this method.

Also popular now: