Shift Nibble

Happy Coding ...

Launch first view every time in iOS application

If you want to show first view on every launch of the application, you can set "Application does not run in background" to "yes" in plist file.

1. Open your project in XCode.

2. Goto "Supporting Files" folder.

3. select plist file projectname-info.plist, it will show you properties on right side. Now click + sign with "Information Property List" to add new property, Type "Application does not run in background" and choose "yes" in the value.

Now your application will show first view on every launch.

How to develop image gallery universal app in iOS

It is a simple app which will load and show images already stored within the app. I will keep this example simple so that beginners can learn from it.

I have used scroll view control so that user can swipe through the images to see them. UI Image View controls are added in scroll view using for loop. I have used for loop so that it is easy to change the images any time, in-fact my images had the fixed prefix for all files and number was added after that and no number was missing in series. If this is not the case for you then you will need to have collection with image file names.

Check out the below code for function "loadImages", this function uses for loop to add UI Image Views to Scroll View and also set image file name to each image view as it is added so that images are loaded. Here is the code

- (void)loadImages{

    NSString *fileName = @"pic";
    imageArray = [[NSMutableArray alloc] init];
    NSString *string2;
    for (int i=1; i<=42; i++){
        string2 = [fileName stringByAppendingFormat:@"%d.jpg", i];

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(width * (i-1), 0, width, height)];
        [imageArray addObject:imgView];
        imgView.image = [UIImage imageNamed: string2];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [imgView setBackgroundColor:[UIColor blackColor]];
        scrollView.contentSize = CGSizeMake((i+1) * width, 0);
        [scrollView addSubview:imgView];

First line initialises the string variable with the prefix "Pic" because all of my images has this string in the start, you can change it with yours. In the loop I am adding number with string "Pic" which makes it "Pic1.jpg" for first image as so on.

Next initialising image view object and setting its height and width to the height and width of the screen. Width and height variables contains the width and height of the screen and it does not do it automatically I will share the code to set these variables shortly.

The first parameter is "width * (i-1)", this tells where to place the image view on x axis. It is the offset value, since every image should be placed at the end of previous image I have multiplied the width of the screen with image number. But I have decreased image number by 1 before multiplying because first image "1" should be placed at 0 offset.

Next three lines in-fact loading the image, setting to image view and setting the background colour of image view.

Next I am increasing the width of scroll view because new image view is added, yes you are right I could have done it before starting the loop :). After that adding image view to scroll view.

Below is the code for "viewDidLoad" event method, it is called on load of the view.

- (void)viewDidLoad
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //Set the height and width variables by screen height and width. Since landscape mode is supported so height is width.
    width = [UIScreen mainScreen].bounds.size.height;
    height = [UIScreen mainScreen].bounds.size.width - 20;
    [self loadImages];

Here I am simply setting the height variable to the width of the screen minus 20 and width variable to the height of the screen. These are done because application is supported in the landscape only, if you want to support both orientations then you would need to inverse them on change of orientation. Minus 20 is done for setting the height excluding the top bar size where battery is shown.

You can make this universal app and same code will work for both because I have set height and width variables based on the screen size so whatever the screen size is code will work. You can add iAds / Goole Ads and put some nice images and upload your app on app store with your name :).