Tuesday, December 10, 2013

How to Support Dynamic Type in iOS 7 Apps


Dynamic Type, is a feature added to iOS 7, that gives users the ability to choose a base text size. This text size will be used throughout the device including third-party applications. Here we discuss on How to Support Dynamic Type in iOS 7 Apps by working on a sample iOS app.

Step 1 - Create a View based iOS application.

Step 2 - Add a UILabel to the ViewController via story board and set the label outlet as contentLabel and add some text on it.

Step 3 - Goto ViewController.m and add the following code inside viewdidload.  This code will listen to the notification if text size is changed in the settings of the device.

  [[NSNotificationCenter defaultCenter]  
    addObserver:self  
    selector:@selector(changeTextSize:)  
    name:UIContentSizeCategoryDidChangeNotification  
    object:nil];  

Step 4 - Add the following function just below viewdidload.
- (void)changeTextSize:(NSNotification *)notification {  
   self.contentLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];  
 }  

Step 5 - You can test the feature by changing the Text Size. For that goto Settings App -> General -> Text Size and change the slider value. Come back to your app and check the font size of text in contentLabel .

Done :)

Thursday, December 5, 2013

An unexpected error occurred: ANDROID_BIN="${ANDROID_BIN:=$( which android )}" exited with 1 Deleting project... - PhoneGap

I was trying to create a new android project via command line in Mac and I got: "An error occurred: ANDROID_BIN="${ANDROID_BIN:=$( which android )}" exited with 1 Deleting project..."

Following was the command I used in Terminal to create a new Android PhoneGap Project. This works for iOS but not for Android.
 ./create ~/Desktop/MyPhoneGapApp com.companyname.myphonegapapp MyPhoneGapApp  
And I got an error as follows.
 An unexpected error occurred: ANDROID_BIN="${ANDROID_BIN:=$( which android )}" exited with 1 Deleting project... -  
Most probably this occurs because path to "tools" and "platform-tools"of the the Android SDK is not set to the PATH.

To fix this you should open the bash profile. Enter the following command in the terminal
 open ~/.bash_profile  

Now you will see the .bash_profile opened in the Text Editor. Enter the following line to your .bash_profile, save and close the file. Make sure to edit the following code to use the correct path of yours.
 PATH=$PATH:/Users/myaccount/Documents/adt-bundle-mac-x86_64/sdk/platform-tools:/Users/myaccount/Documents/adt-bundle-mac-x86_64/sdk/tools  
You should execute your .bash_profile to update your PATH.  Enter the following command in Terminal
 source ~/.bash_profile  
Now close and reopen your Teminal. Navigate to the bin folder of Android PhoneGap and enter the following command to create a PhoneGap Android Project.
 ./create ~/Desktop/MyPhoneGapApp com.companyname.myphonegapapp MyPhoneGapApp  
Done :)

Wednesday, December 4, 2013

How to Install PhoneGap in Mac and Create an iOS Project.

Please follow the steps below to install PhoneGap in mac OSX and create an iOS project.

Step 1 - Download and Install Node.js

Step 2 - Goto PhoneGap download page in your browser and download the latest version of PhoneGap and UnZip it.

Step 3 - Open Terminal in your mac and navigate to the folder where you downloaded PhoneGap.

Step 4 - From there navigate to the path in terminal \phonegap-2.9.1\phonegap-2.9.1\lib\ios\bin

Step 5 - Type the following command in Terminal

./create ~/Desktop/MyPhoneGapApp com.companyname.myphonegapapp MyPhoneGapApp

Parameters of the create command are

1 -
"~/Desktop/MyPhoneGapApp" Path to create new project
2 - "com.companyname.myphonegapapp" bundle name
3 - "
MyPhoneGapApp" Project nameThis will create an iOS PhoneGap project named MyPhoneGapApp in Desktop :)

Sunday, December 1, 2013

How To Integrate PFLogInViewController And PFSignUpViewController in iOS App

Please follow the previous tutorial, How To Integrate Parse With iOS App to get started with Parse in your iOS App.

In this tutorial, we are going to edit the Appdelegate.h and .m files to launch PFLogInViewController And PFSignUpViewController. 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  {  

   [Parse setApplicationId:@"YOUR_PARSE_APP_ID" clientKey:@"YOUR_CLIENT_KEY"];  
   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  

   // Override point for customization after application launch.  
   if ([PFUser currentUser]) // if user logged in.  

   {  
    self.homeViewController= [[HomeViewController alloc]initWithNibName:@"HomeViewController" bundle:nil];  
    self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.homeViewController];  

   } else  
   {  
    self.logInViewController = [self getLoginViewController];  
    self.navigationController = [[UINavigationController alloc]initWithRootViewController:logInViewController];  
   }  

   self.window.rootViewController = self.navigationController;  
   [self.window makeKeyAndVisible];  

   return YES;  

  }   

Copy paste this method to Appdelegate.m
 - (PFLogInViewController *)getLoginViewController
 {
   PFLogInViewController *loginController= [[PFLogInViewController alloc] init];
   [loginController setDelegate:self];
   loginController.logInView.logo = nil;

   loginController.fields = PFLogInFieldsUsernameAndPassword
   | PFLogInFieldsLogInButton
   | PFLogInFieldsSignUpButton
   | PFLogInFieldsPasswordForgotten;

   self.signUpViewController = [[PFSignUpViewController alloc] init];
   [signUpViewController setDelegate:self];

   signUpViewController.fields = PFSignUpFieldsUsernameAndPassword
   | PFSignUpFieldsSignUpButton|PFSignUpFieldsDismissButton;

   signUpViewController.signUpView.logo = nil;
   [loginController setSignUpController:signUpViewController];
   return loginController;
 }


Login and SignUp screens are set. Now we should include delegates for Login and SignUp.

Sent to the delegate to determine whether the log in request should be submitted to the server.
 - (BOOL)logInViewController:(PFLogInViewController *)logInController shouldBeginLogInWithUsername:(NSString *)username password:(NSString *)password { 
   // Validate username and pasword field.
   if (username && password && username.length != 0 && password.length != 0) { 
     return YES; // Begin login process 
   } 

   [[[UIAlertView alloc] initWithTitle:@"Missing Information" 
                 message:@"Make sure you fill out all of the information!" 
                 delegate:nil 
            cancelButtonTitle:@"ok" 
            otherButtonTitles:nil] show]; 
   return NO; // Interrupt login process 
 } 

Sent to the delegate when a PFUser is logged in.
 - (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {

   NSLog(@"Login sucessfull !! and username is %@",user.username);
   self.homeViewController= [[HomeViewController alloc]initWithNibName:@"HomeViewController" bundle:nil]; 
   [self.navigationController pushViewController:self.homeViewController animated:YES];\
 }

Sent to the delegate when the log in attempt fails.
- (void)logInViewController:(PFLogInViewController *)logInController didFailToLogInWithError:(NSError *)error 
{
   NSLog(@"Failed to log in...");
}

Sent to the delegate when the log in screen is dismissed.
- (void)logInViewControllerDidCancelLogIn:(PFLogInViewController *)logInController 
{
   NSLog(@"Canceled log in...");
}

Sent to the delegate to determine whether the sign up request should be submitted to the server.
- (BOOL)signUpViewController:(PFSignUpViewController *)signUpController shouldBeginSignUp:(NSDictionary *)info { 
   BOOL informationComplete = YES; 
   //Validate all datafields.
   for (id key in info) { 
     NSString *field = [info objectForKey:key]; 
     if (!field || field.length == 0) { // check completion 
       informationComplete = NO; 
       break; 
     } 
   } 

   // Display an alert if validation failed
   if (!informationComplete) { 
     [[[UIAlertView alloc] initWithTitle:@"Missing Information" 
                   message:@"Make sure you fill out all of the information!" 
                   delegate:nil 
              cancelButtonTitle:@"ok" 
              otherButtonTitles:nil] show]; 
   } 
   return informationComplete; 
 } 

Sent to the delegate when a PFUser is signed up.
- (void)signUpViewController:(PFSignUpViewController *)signUpController didSignUpUser:(PFUser *)user { 
   // SignUp sucessfull, Now dismiss the PFSignUpViewController 
   [self.signUpViewController dismissViewControllerAnimated:YES completion:nil]; 
} 

Sent to the delegate when the sign up attempt fails.
- (void)signUpViewController:(PFSignUpViewController *)signUpController didFailToSignUpWithError:(NSError *)error { 
   NSLog(@"Failed to sign up..."); 
 } 

Sent to the delegate when the sign up screen is dismissed.
- (void)signUpViewControllerDidCancelSignUp:(PFSignUpViewController *)signUpController { 
   NSLog(@"User dismissed the signUpViewController"); 
 } 

Done :)