Blog

  • Simplest way to check for an active Internet Connection on iOS

    Sometimes we need to check synchronously, if there is an internet connection. For instance, when an app totally depends on internet connection. This is the simplest way to check internet connection on iOS app synchronously without using Reachability class:

    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    if (data != nil)
        NSLog(@"Device is connected to the internet");
    else
        NSLog(@"Device is not connected to the internet");
    

    Taken from here.

    Instead of Google it is better to use another lightweight website. But I use google.com, since it is always online and reliable and not very heavy page.

  • Title For Navigation Controller In Tab Controller

    If you just set only a self.title and self.tabBarItem.title, your tabBarItem title will change to the title of navigation controller.
    So, this is the solution, taken from StackOverflow:

    self.title = @"Title for TabBarItem";
     // TabBarItem.title inherits the viewController's self.title
    self.navigationItem.title = @"Title for NavigationBar";
    
  • Search in Bootcamp From Mac OS X

    SpotLight doesn’t index Bootcamp drive, since it has another file system. SpotLight indexes only HFS and HFS+ file systems.
    To find files in a Bootcamp drive from Mac OS X drive, do the following:

    • Type in Terminal
      $ cd /Volumes
      
    • Then find all your drive names, you have:
      $ ls
      
    • Now you can search something by pattern. For instance all Microsoft Word files in Bootcamp:
      $ find BOOTCAMP -name "*.doc"
      
    • After you have found what you need, you might want to stop the search, that can be pretty long. To stop search just press Ctrl+C

    Reference

  • Two NSStrings are same but different when you compare them

    Recently I was shocked by the fact that two NSStrings that resemble each other on NSLog may be different. It is because sometimes NSString can contain a zero width space character. Be aware of that and consider:

        #define ZERO_WIDTH_SPACE_STRING @"\u200B"
    

    To conquer this you should clean your string from zero width white space characters before comparing:

        NSMutableString *eMailToAdd = [NSMutableString string];
    	
        NSMutableCharacterSet *charSet = \
    [[NSCharacterSet whitespaceCharacterSet] mutableCopy];
        	//[charSet formUnionWithCharacterSet:\
    [NSCharacterSet punctuationCharacterSet]];
        	
            NSString *rawStr = [[tokenField textField] text];
        	for (int i = 0; i < [rawStr length]; i++)
        	{
        		if (![charSet characterIsMember:\
    [rawStr characterAtIndex:i]])
        		{
        			[eMailToAdd appendFormat:\
    @"%@",[NSString stringWithFormat:@"%c",\
     [rawStr characterAtIndex:i]]];
        		}
        	}
    
  • How to clear cash in Safari in Mac OS X Mountain Lion

    Web browsers and web proxy servers employ web caches to store previous responses from web servers, such as web pages and images. Web caches reduce the amount of information that needs to be transmitted across the network, as information previously stored in the cache can often be re-used. This reduces bandwidth and processing requirements of the web server, and helps to improve responsiveness for users of the web.
    Web browsers employ a built-in web cache, but some internet service providers or organizations also use a caching proxy server, which is a web cache that is shared among all users of that network.

    To clear cache in Safari in Mac OS X Mountain Lion:

    1. Enable Developer Menu by clicking last checkbox in last tab in Safari/Preferences.
    2. Click Developer Menu/Clear caches or Press Alt+Cmd+E

    More about Web Cache in Wikipedia.

  • How to hide a keyboard from view

    Sometimes it is hard to use resignFirstResponder method on every textField, that is present on our view. It is much easier to hide keyboard by the following way:

    [self.view endEditing:YES];
    
  • Delete button title in table view iOS

    To change a delete button title use this:

    - (NSString *)tableView:(UITableView *)tableView \
     titleForDeleteConfirmationButtonForRowAtIndexPath: \
    (NSIndexPath *)indexPath {
        
        return @"Удалить из избранного";
        
    }
    
  • Grouped table view background color

    To change a background color of a grouped table view in iOS we need to do two things:

    self.tableView.backgroundColor = your_color;
    self.tableView.backgroundView = nil;
    

    It is the third answer here.

  • UIView animation pattern

    Animation pattern using blocks:

        [UIView animateWithDuration:1.0
                         animations:^{
                             
                         }
                         completion:^(BOOL finished){
                             
                         }
         ];
    
  • How to create an animated Intro View in iOS

    I will explain, how to create an animation, when you Default image comes to front with dissolving.

    To create an animated Intro View in iOS App, which has a TabBarController you may do the following:

      1. Create an Intro View Controller, which has one UIImageView with your Default.png.
      2. Add the following to your application didFinishLaunchingWithOptions method
        IntroViewController *introViewController = \
        [[IntroViewController alloc] initWithNibName: \
        @"IntroViewController" bundle:nil];
            self.window.rootViewController = introViewController;
            
            [self.window makeKeyAndVisible];
        
      3. Add the following method to your IntroViewController:
        - (void)viewDidAppear:(BOOL)animated
        {
            [super viewDidAppear:animated];
            
            [UIView animateWithDuration:0.5
                             animations:^{
                                 CGRect frame = self.imageView.frame;
                                 frame.origin.x -= frame.size.width;
                                 frame.origin.y -= frame.size.height;
                                 frame.size.width *= 3;
                                 frame.size.height *= 3;
                                 self.imageView.frame = frame;
                                 self.imageView.alpha = 0.0;
                             }
                             completion:^(BOOL finished){
                                 useDelegate
                                 appDelegate.window.rootViewController = \
        appDelegate.tabBarController;
                             }
            ];
        }
        

      Explanation:
      After app loads, Intro View Controller is diplayed. Then, when it’s view appears, it’s UIImageView’s frame is trippled in size and it’s origin is moved to top left. Also the transparency is changed to 100%. After animation finishes, your tab bar controller is displayed.