Showing posts with label XCode. Show all posts
Showing posts with label XCode. Show all posts

Friday, 22 January 2016

Install CocoaPods

POD Install:

1) Open terminal 

2) $ sudo gem install cocoapods (gem will get installed in Ruby inside System library)

3) Enter this command in Terminal to complete the setup:
    $ pod setup


4) Install Command line tool of Xcode.
    $ xcode-select --install

5) Verify that you’ve successfully installed Xcode Command Line Tools:
$ xcode-select -p /Library/Developer/CommandLineTools

6) Create a xcode project. 

7) cd "path to your project root directory"

8) Create Podfile in your project.
    $ pod init 

9) open -a Xcode Podfile 

Podfile will get open in text mode. 
You shouldn’t use TextEdit to edit the Podfile because it likes to replace standard quotes with more graphically appealing typeset quotes. This can cause CocoaPods to become confused and results in errors being thrown, so it’s best to use Xcode or another programming text editor to edit your Podfile.


The default Podfile looks like this:

# Uncomment this line to define a global platform for your project
#platform :ios, '8.0'
# Uncomment this line if you're using Swift
#use_frameworks!

target ‘TestPod’ do

end




Replace the commented lines with the following:

# Uncomment this line to define a global platform for your project
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
# Uncomment this line if you're using Swift
use_frameworks!

This tells CocoaPods that your project is targeting iOS 8.0 and will be using frameworks instead of static libraries.



10) pod 'AFNetworking', '~> 3.0'

  It’s finally time to add your first dependency using CocoaPods! Copy and paste the following into your pod file, right after target “TestPod” do:

11) Now close pod file and execute below command on terminal
      $ pod install

Now close your xcode project and open .xcworkspace xcode project file.





Tuesday, 5 August 2014

Get Address from Coordinate or CLLocation


CLLocation *loc = [[CLLocation alloc]initWithLatitude:37.3104 longitude:-122.0108];


[self getAddressFromLocation:loc];



-(void)getAddressFromLocation:(CLLocation*)loc
{
    CLGeocoder *ceo = [[CLGeocoder alloc]init];
    
    
    [ceo reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error)
     {
         
         NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
         
         
         
         NSLog(@"placemarks=%@",[placemarks objectAtIndex:0]);
         CLPlacemark *placemark1 = [placemarks objectAtIndex:0];
         
         NSLog(@"placemark.ISOcountryCode =%@",placemark1.ISOcountryCode);
         NSLog(@"placemark.country =%@",placemark1.country);
         NSLog(@"placemark.postalCode =%@",placemark1.postalCode);
         NSLog(@"placemark.administrativeArea =%@",placemark1.administrativeArea);
         NSLog(@"placemark.locality =%@",placemark1.locality);
         NSLog(@"placemark.subLocality =%@",placemark1.subLocality);
         NSLog(@"placemark.subThoroughfare =%@",placemark1.subThoroughfare);
         
         
         NSString *locatedaddress = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
         
         //Print the location in the console
         NSLog(@"Currently address is: %@",locatedaddress);
         
         
         if (error == nil && [placemarks count] > 0)
         {
             placemark = [placemarks lastObject];
             
             // strAdd -> take bydefault value nil
             NSString *strAdd = nil;
             
             if ([placemark.subThoroughfare length] != 0)
                 strAdd = placemark.subThoroughfare;
             
             if ([placemark.thoroughfare length] != 0)
             {
                 // strAdd -> store value of current location
                 if ([strAdd length] != 0)
                     strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark thoroughfare]];
                 else
                 {
                     // strAdd -> store only this value,which is not null
                     strAdd = placemark.thoroughfare;
                 }
             }
             
             if ([placemark.postalCode length] != 0)
             {
                 if ([strAdd length] != 0)
                     strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark postalCode]];
                 else
                     strAdd = placemark.postalCode;
             }
             
             if ([placemark.locality length] != 0)
             {
                 if ([strAdd length] != 0)
                     strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark locality]];
                 else
                     strAdd = placemark.locality;
             }
             
             if ([placemark.administrativeArea length] != 0)
             {
                 if ([strAdd length] != 0)
                     strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark administrativeArea]];
                 else
                     strAdd = placemark.administrativeArea;
             }
             
             if ([placemark.country length] != 0)
             {
                 if ([strAdd length] != 0)
                     strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark country]];
                 else
                     strAdd = placemark.country;
             }
             
             
             NSLog(@"Address : %@", strAdd);
             
             
             
             address = strAdd;
             
         }
         
         
     }];
    
}

Thursday, 5 June 2014

Add UIBarButtonItem on NavigationBar

a) UIBarButtonItem *btn = [[UIBarButtonItem alloc]
                                initWithTitle:@"Add"
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(buttonAddClick)];
    
    self.navigationItem.rightBarButtonItem = btn;



b) UIBarButtonItem * btn = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                  target:self action:@selector(buttonAddClick)];
    
    self.navigationItem.rightBarButtonItem = btn;



c) UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(0, 0, 30, 30);

    [btn setImage:[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];

    //[btn setImage:[UIImage imageNamed:@"logout-hover"] forState:UIControlStateHighlighted];

    [btn addTarget:self action:@selector(buttonAddClick) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: btn];

    self.navigationItem.rightBarButtonItem = rightBarButtonItem;

Monday, 2 June 2014

What is the difference between @class and #import ?

@class is used when you need to know the name of a class in a particular file, but you don't need to know any details about the class (its methods, for example). #import is used when you actually need to use the class (i.e., send it a message).

@class is called a forward declaration. You're basically telling the compiler that the class exists but not anything about the class. Thus, it doesn't know stuff like its superclass and what methods it declares.

For example, 
if you're declaring instance variables in a header file, you can use @class to declare an instance variable of a certain type:


@class MyOtherClass;

@interface MyClass : NSObject
{
    MyOtherClass *myIvar;
}
@end



Since you're not using myIvar yet, you don't need to know anything about it except that the type MyOtherClass exists.

However:


#import "MyOtherClass.h"

- (void)doSomething
{
    [myIvar doSomethingElse];
}


In this case, you're sending the doSomethingElse message to myIvar; the compiler needs to know that instances of MyOtherClass define this method, so you have to import the header file or the compiler will complain.




Why worry about this?



It mostly has to do with dependencies. When you #import file A into file B, file B becomes dependent upon file A -- that is, if file A changes, you'll have to recompile file B. If you use @class in file B, file B is not dependent on file A, and thus doesn't need to be recompiled when file A changes -- so if you're just declaring a type and not actually dependent upon the implementation of file A, you can save yourself compilation time by not #importing file A.

Difference between Copy and MutableCopy

The difference between 'copy' and 'mutableCopy' can be simply understood with polymorphism in Object Oriented Programming concepts.

We will take the example of Array in objective-C. MutableArray is the extension of NSArray class. Therefore, all the methods available in NSArray is available in NSMutableArray, but the additional methods present in NSMutableArray is not known to NSArray class.

Now moving ahead, the copy method on an NSArray will return an object of type NSArray(The array that can not be modified). And mutableCopy method will return an object of mutable type (The array that can be modified).

Now, there can below cases :

Case 1:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C", nil];
    NSArray *arr2=[arr1 copy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);

In this case an NSArray object is returned and is received in an NSArray object.
So, the array received can not be modified.

Therefore, we will not be able to use below statement
[arr2 insertObject:@"Z" atIndex:0];


Case 2:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C", nil];
    NSArray *arr2=[arr1 mutableCopy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);

In this case an NSMutableArray object is returned and is received in an NSArray object.
Since the receiver object is of type NSArray, it doesn't know the methods present in NSMutableArray, arr2 will not be able to use any of the methods of NSMutableArray.
That is, the method mutableArray will make no sense in this scenario.

So, we will not be able to use below statement
[arr2 insertObject:@"Z" atIndex:0];

Case 3:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C", nil];
    NSMutableArray *arr2=[arr1 copy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);

In this case an NSArray object is returned and is received in an NSMutableArray type object. The receiver arr2 is now pointing to an object address that is of type NSArray. However arr2 has the additional methods than NSArray, it will not be able to use those methods coz the pointed object NSArray does not know the additional methods present in arr2(NSMutableArray).

Hence, we will not be able to use below statement
[arr2 insertObject:@"Z" atIndex:0];


Case 4:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C", nil];
    NSMutableArray *arr2=[arr1 mutableCopy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);
    [arr2 insertObject:@"Z" atIndex:0];

In this scenario, the receiver(arr2) of type NSMutableArray receives an object of type NSMutableArray. Therefore, the receiver knows the additional methods of NSMutableArray as well as the object that is being pointed by receiver(arr2).

And finally below statement will work like charm,
[arr2 insertObject:@"Z" atIndex:0];

Log before insertion will be:
arr1:(
    A,
    B,
    C
)
arr2:(
    A,
    B,
    C
)
And log after insertion will be:
arr1:(
    A,
    B,
    C
)

arr2:(
    Z,
    A,
    B,
    C
)




Hope above description helped you.

Simply:

  1. mutableCopy always returns a mutable result.
  2. copy always returns an immutable result.


Friday, 23 May 2014

Remove white space & newline from both ends of a string and replace space by %20



This will remove white space and newline from both ends of a string:

NSString *newString = [oldString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

NSString *newString = [oldString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];



This will remove white space and newline from any where in string:

//give %20 on space
NSString *string = [[NSString alloc]initWithFormat:@"%@",[yourString stringByReplacingOccurrencesOfString:@" " withString:@"%20" options:NSRegularExpressionSearch range:NSMakeRange(0, yourString.length)]];