Pages

Tuesday 30 December 2014

Application's own spotlight

Have you ever searched for a menu in an application, i.e, you know the name of the menu and you know it's somewhere in the menu hierarchy, but you don't know where it is.

It's not that complicated, actually it's very easy to find a menu of an application.

You know about the spotlight of the Mac which is a great utility to search for all the files and stuffs in your machine. Fortunately, in Mac, most of the applications have their own spotlight. You can find it in the help menu of an application.

For e.g., I need to find a menu named "Create SuperClass" in the XCode, but I forgot the exact location of it in the menu bar. What I will do is, I simply open the XCode's spotlight (under the Help menu) and start to type "Create SuperClass". When I start to type, the XCode matches my search string and shows the matched menus in a list. When I hover the mouse over each item, I can see the exact location of the menu.


Wednesday 24 December 2014

Using constraints in storyboard and also by code


Sample code is in Github.


Three years before, all the iPhones sizes (iPhone3, iPhone3GS, iPhone4, iPhone4S) are the same. So we didn't care about the compatibility issues like the android developers. When apple introduced iPhone5 and later iPhone5S, iPhone5C, it was not a big deal for us. All we did was, add a launch image for those new screen sizes and we had to adjust our views a little to use the newly added 0.5 inches screen. So the auto layout helped us to achieve this.

Now apple introduces even big screen sizes, and the old style auto layout cannot help us like in the past. Because old auto layout only works with the super view i.e, if the super view expands it expands, when the superview move the control also will move. But you cannot specify how much you want to move or how much you want to expand. There is nothing more than this.

With using the auto layout, you can place a control in a view in its top left corner, but it is merely impossible to place a control at 30% of left. Also you cannot make a text field expands its width, half the width of its superview. But the Constraints does this trick.

When trying to learn this technology, it'll like a hell at first. But once you get into it, you will find the constraints as one of the best tools apple introduced ever. 

If you use the constraints in your app, you don't have to worry about these screen sizes. Even you don't have to worry about the iPad screen sizes. You can have a universal storyboard for all your devices.

Here I am trying start this, taking the following sample screen. The final output should look like this in Portait and Landscape.




1) The screen has a header in the top of the main view with 60 pixels height.
2) The header has two buttons in each corner with 10 pixels of padding and a header label in the middle.
3) A textfield is added to the view, which is 75 pixels below from the headerview and center to the main view. The width of this text field is half the width of the main view.
4) There are two buttons 25 pixels below the textfield. One button is left aligned with the textfield and the other one is right aligned with the textfield.

This is easy if you identify which constraint is to be added for each control in storyboard. But, it is little difficult to do in code. To meet all the above requirements, we have to add the following constraints.

You can find a sample project in Github illustrating this example both in storyboard and code. The link is here.


Create all the controls like you do normally, add those to the viewcontroller's view. Don't care about their positions. The constraints will do that.

Before adding the constraints to a subview, make sure you give No to its "translatesAutoresizingMaskIntoConstraints" property. Otherwise you will get an exception.


Constraints for the subviews of the header view.


Constraints for the header view.


Constraints for the textfield and the two button below it. (Make sure you disable the translatesAutoresizingMaskIntoConstraints for these controls before adding constraints.)


The alignment of the code here is worse, but believe me, it works.

- (void)viewDidLoad {
BOOL addControlProgrammatically = YES;
if (addControlProgrammatically) {
[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[headerView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
[self.view addSubview:headerView];
UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
[headerView addSubview:cancelBtn];
UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[saveBtn setTitle:@"Save" forState:UIControlStateNormal];
[headerView addSubview:saveBtn];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 31)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
[button1 setTitle:@"Button1" forState:UIControlStateNormal];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
[button2 setTitle:@"Button2" forState:UIControlStateNormal];
cancelBtn.translatesAutoresizingMaskIntoConstraints = NO;
saveBtn.translatesAutoresizingMaskIntoConstraints = NO;
[headerView addConstraints:@[[NSLayoutConstraint constraintWithItem:cancelBtn
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeLeading
multiplier:1.0 constant:10],
[NSLayoutConstraint constraintWithItem:cancelBtn
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeCenterY
multiplier:1.0 constant:0]
]];
[headerView addConstraints:@[[NSLayoutConstraint constraintWithItem:saveBtn
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeTrailing
multiplier:1.0 constant:-10],
[NSLayoutConstraint constraintWithItem:saveBtn
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeCenterY
multiplier:1.0 constant:0]
]];

headerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:@[[NSLayoutConstraint constraintWithItem:headerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0 constant:60],
[NSLayoutConstraint constraintWithItem:headerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0 constant:0],
[NSLayoutConstraint constraintWithItem:headerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0 constant:0],
[NSLayoutConstraint constraintWithItem:headerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0 constant:0]
]];
[self.view updateConstraintsIfNeeded];
textField.translatesAutoresizingMaskIntoConstraints = NO;
button1.translatesAutoresizingMaskIntoConstraints = NO;
button2.translatesAutoresizingMaskIntoConstraints = NO;
// [textField setText:@"Hello World"];
[textField setBackgroundColor:[UIColor redColor]];
[self.view addSubview:textField];
[self.view addSubview:button1];
[self.view addSubview:button2];
[self.view addConstraints:@[[NSLayoutConstraint constraintWithItem:textField
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0 constant:0],
[NSLayoutConstraint constraintWithItem:textField
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeBottom
multiplier:1.0 constant:75],
[NSLayoutConstraint constraintWithItem:textField
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:0.5 constant:0]]];
[self.view addConstraints:@[[NSLayoutConstraint constraintWithItem:button1
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:textField
attribute:NSLayoutAttributeLeft
multiplier:1.0 constant:0],
[NSLayoutConstraint constraintWithItem:button1
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:textField
attribute:NSLayoutAttributeBottom
multiplier:1.0 constant:25]]];
[self.view addConstraints:@[[NSLayoutConstraint constraintWithItem:button2
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:textField
attribute:NSLayoutAttributeRight
multiplier:1.0 constant:0],
[NSLayoutConstraint constraintWithItem:button2
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:textField
attribute:NSLayoutAttributeBottom
multiplier:1.0 constant:25]]];

}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

Friday 19 December 2014

Google Easter Eggs

Google have some amusing Easter Eggs. Type the following words in the google search bar and see the magic. You will be amazed.

1) google Leet   "goto www.google.com enter 'google leet' and press I am feeling lucky"

2) recursion (shows "Did you mean: "recursion"->same word)



3) do a barrel roll (will rotate the results page a full 360 degrees)

4) tilt (makes the page 'lean' to the right a bit.)




5) let it snow (simulates snowfall and frost on the search results.)

6) binary (changes the number of found results to read out in binary)

7) zerg rush (returns a search page with 'O's eating the search results. Clicking each 'O' a few times kills it.)

8) conway's game of life (shows the the results along with a set of controls to play, pause, maximize/restore, and step.)

9) Search "timer" in the google. The google will initiate a timer for you.




10) Search any number in the google followed by "=english". You will find the typed number in words. For example if you search "123456=english", you will see the following output.



Thursday 18 December 2014

iOS8 - Easter eggs

With the latest update, the iOS began the best OS ever. With the iOS8, apple introduced many feature which you may not aware.

1. With iOS8, you can continue sharing your current location to someone (let's say your friend) forever or for a particular time.

2. iOS introduced extension with helps you do actions without opening the entire app. For example, you can send a quick reply to an incoming mail without leaving the current app. You can accept/reject an iCal invitation without opening the Calender app. Almost all the apps from the apple have this feature. In future, third party apps will make use of this feature and we will get some amazing extensions.

3. Battery status is available in iOS8, although the android have this feature already. The battery status option will list the apps based on their battery usage, you can kill an app which takes so much battery usage.

4. You can hide few photos/videos from your photo library, so that your friend/family members can't see them. You can see your hidden photos from the hidden photos folder.

5. You don't have to enter your credit card info everytime in your iPhone. You can just scan your card, the iPhone will scan the details from your card.

6. If you delete a photo by mistake you can easily recover them. You can recover the photos which are deleted in the past 30 days.

7. HandsOff: Apple introduced a new feature called HandsOff with iOS8 and OS X 10. You can start a work in any apple device and finish it in your another apple device. For example, you can start to compose a mail from your iPhone and finish and send it from your iPad/Mac when they are nearby. Apple also makes these API's public, so third party apps can make use of this.

8. You can send a mail with attachments upto 5GB in iOS8. In apple devices, the attachments are loaded with the mail itself, and in other devices the links of the attachments will be added with the body of the mail.

9. Interactive actions within the app. For e.g when you swipe a mail from right to left, in previous iOS, you will see only the delete menu. Now apps can have multiple menus when you do that swipe action. For e.g., the mail app have delete, reply, Flag...

10. Widgets are introduced like android. Although, they are only limited to the notification center (the UI appears when you swipe from top in the iPhone), not in the home screen like android.

11. Custom keyboards. Third party apps can supply their own keyboard which you can use in your device. These keyboards will appear throughout the iPhone, and you can control these from the settings app like the inbuild keyboards. Apple also updated their keyboard with swipe feature, predictive text...

12. Your recent contact list will appear when you use the app switcher (when you double click the home button). Although this feature is not available for the third party apps.

13. iCloud Keychain: The passwords stored in your all the apple devices are synchronised. But Google Chrome has this feature already.


Not only these, apple has introduced so many API's with iOS8 like HealthKit, Photo Editing, Extensions... Third party apps will use these features and give us some amazing apps in the future.

Wednesday 17 December 2014

XCode Snapshots

When you are going to start a major milestone, it is very important to keep the backup of the current final source. Using source control is one of the best solution here. But I have seen people who take manual copies of the project when they start a major work. Basically, they copy the entire project's root folder and paste it somewhere else. At a point of time, these backups occupies over GB's of space, and it is also very difficult to restore to the backup.  But the XCode has a amazing feature which will take of this.

It is none other than "SnapShots". You should have already seen this. When you do some major editing processes such refactoring the project, whole find or replace and some other processes. We can also take manual snapshots when we start some major change. You can take snapshots by selecting File->Take Snapshot





Also if you want the XCode to take automatic snapshots when the build succeeds or finishing some other processes, you can do that by enabling a checkbox in the XCode->Behaviour->Select option->Create Snapshot.



You can restore the project to these snapshots at any time in the future. This will update the current source code to its origin source during the time of the snapshot. This may be very helpful than taking manual copies. File->Restore Snapshot->Choose the snapshot to be restored->Restore.



The XCode will open an interface comparing the files before and after restoring the snapshot. You can manually select any change and discard it if you don't want it.




Git Vs Snapshot
If you are using Git or some other source control software, you may confuse both of them. Git and Snapshot are not same and they cannot be used interchangeably. Snapshot saves the changes of a file in a format only the XCode will understand but the Git is an universal technology. But in some situations you may have to use both of them. Using Git is a must one for the project, but using Snapshot will help you to do restore temporary backups.

When you start work on a milestone, you can discard all local change in Git at any time. But you can't have many commits like "Temp Commit", "temp commit2"... Because when working with a team, you can't commit a project often. So in this case you can use the snapshots. Because the snapshots are local copies which will not affect the remote source.

Also you can share the snapshots to other people. All your snapshots are located under the folder
~/Library/Application Support/Developer/Shared/SnapshotRepository.sparseimage

Monday 15 December 2014

iPhone tips and tricks

There are many hidden features in iPhone which are not mentioned anywhere in the apple website, but works.

1. Swipe left on the Compass app and your iPhone will work as a level meter.

2. You can see the timestamps of your text messages by sliding your texts over to the left.

3. As long as the camera app is open, you can use the volume buttons to take a photos.

4. Double-tapping the spacebar will automatically end the sentence with a period and start a new one for you.

5. There is an undo function that allows you to go back if you make a mistake writing a text message, email, or editing a photo. Simply shake your phone and this option will pop up.

6. By holding down the capture button in the camera app, your phone will take a series of photos so you can get the perfect shot.

7. You can enable the emoji keyboard that appears in the whatsapp for all your apps.
Go to Settings > General > Keyboard > Add New Keyboard and select Emoji.

8. HeadPhones: Double click the volume button in of the head phone to play the next songs, and triple click to play the previous song. When the camera app is open, you can use the volume buttons to take the photographs.

9. Triple click the Home button to enable/disable the assistive touch.

10. If you press and hold the dot (.) button in the Safari when entering the web address, you will see multiple options like .org, .net. .com…

11. Double tap the shift button in the keyboard will do the “Caps Lock”. Double tap again to disable Caps Lock.

12. Press the home button and the Off button of the iPhone at the time to take screenshot of the current iPhone’s screen. But few apps can block their screens while taking screenshot to protect their sensitive information.

Wednesday 10 December 2014

Selecting a control among multiple in Interface Builder.

When you are working on the Interface Builder, you will find it hard to select a particular control when lot of controls are near by it. Also it is hard to select a control which is beneath another control.

For e.g., if you have a text field over a UIView, then you can't select the UIView directly sometimes. You may to select that view from the Document Outline.

Instead, you can use this shortcut which is provided with the XCode. 

Hold the "Shift" button, and right click over the control. 
(Or Hold the "Shift" and the "Option" button and click over the control)



Now XCode will list all the subviews and super views present at the clicked location, like in the above image. You can easily select your desired control from this menu. This is like selecting a layer in the Photoshop.


Sunday 7 December 2014

XCode Snippets - How to use?

If you are an iOS developer, you would be spending most of your time with the XCode. One of the most advanced feature of XCode is Code Completion, which is available now with most of the SDKs.

But the sad part is, many SDKs provide the code completion only for their inbuild functions/methods. But in the XCode, we can create our own code completion shortcuts with the help of snippets. Snippets are the small piece of code saved with the XCode, which can be used whenever needed without having to type the same piece of code again and again.

By default the XCode have many code snippets. You can see that in the bottom of the Utilities panel.

Snippets Menu


To create your custom snippet, prepare the piece of code you want, and drag it to the utilities panel. The XCode will open a popup, in which you have to enter the title, description, scope, shortcut. You have to do this only once. Once you finish this, you don't have to type the same piece of code ever. You can make the code appear in the editor either by typing the shortcut or by dragging the snippet to the editor.



The snippets are basic XML and they can be shared to other people. You can find all your snippets in the following path.
/Users/<Your_Name>/Library/Developer/Xcode/UserData/CodeSnippets
Just copy the desired snippet and paste in the same path in another machine.





Monday 1 December 2014

How to convert an existing iPhone project into an ARC enabled project?

It's very simple as it looks like. All you have to do is select a menu from the XCode menu bar, and the XCode do all the stuffs for you.

The menu is,

Edit -> Refactor -> Convert to Objective-C ARC...