Monday, March 18, 2013

Converting Meaningful Addresses to Longitude and Latitude


This part of code is from Book , Vandad Nahavandipoor.


1. Declare a property of type CLGeocoder


     #import
     #import

     @interface
     Converting_Meaningful_Addresses_to_Longitude_and_LatitudeViewController : UIViewController
    @property (nonatomic, strong) CLGeocoder *myGeocoder;
    @end 


2. In view did load


  • NSString *oreillyAddress =
    @"1005 Gravenstein Highway North, Sebastopol, CA 95472, USA";
    self.myGeocoder = [[CLGeocoder alloc] init];
    [self.myGeocoder
    geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {
    if ([placemarks count] > 0 && error == nil){

    NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]); CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
    NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);
    NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude); 
    }
    else if ([placemarks count] == 0 &&
    error == nil){ NSLog(@"Found no placemarks.");
    }
    else if (error != nil){
    NSLog(@"An error occurred = %@", error); }
    }]; 










Sunday, March 17, 2013

Ten online resource for learning coding


  1. Code/Racer
  2. MIT OpenCourseWare
  3. Udacity
  4. Mozilla Developer Network
  5. The CodePlayer
  6. Coursera
  7. Codeacademy
  8. Khan Academy
  9. Learn Python the hard way
  10. HTML5 Rocks

Wednesday, March 13, 2013

ios- Map shows all annotations

The following code is to show all the pin in the map,  it finds the most top pin and the most bottom pin, and count the center and delta.


Here is the code:

-(void)zoomToFitMapAnnotations:(MKMapView*)mapView
{
    if([mapView.annotations count] == 0)
        return;

CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

    CLLocationCoordinate2D bottomRightCoord;
    bottomRightCoord.latitude = 90;
    bottomRightCoord.longitude = -180;

    for(MapAnnotation* annotation in mapView.annotations)
    {
        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

        bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
        bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
    }

MKCoordinateRegion region;
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
    region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.15; // Add a little extra space on the sides
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.15; // Add a little extra space on the sides

    region = [mapView regionThatFits:region];
    [mapView setRegion:region animated:YES];
}

Sunday, February 24, 2013

iOS -- Constructing and Displaying Styled Texts

To display rich text formatted :

for example :     This is a car! , to display different styled text in one label.


The solution is use attributed text, here is the code,


To set the text to a label, use self.label.attributedText = [self attributedText];



Sunday, February 3, 2013

ios -- understand strong, weak, unsafe_unretained


In IOS 5, to use Automatic Reference Counting (ARC) in the latest LLVM compiler, we need to deal with storage that is strong, weak, or unsafe and unretained. Any object under ARC is managed with one of these storage attributes. Here is a short explanation for each one: 


strong
An object of this type is automatically retained at runtime and will be valid until the end of its scope, where it will automatically be released. For those familiar with Objective-C’s traditional way of memory management, this keyword is similar to the retain keyword. 



weak
This is zeroing weak referencing. If a variable is defined with this keyword, when the object to which this variable points gets deallocated, this value will get set to nil. For instance, if you have a strong string property and a weak string property and set the weak property’s value to the strong property’s value, when the strong property gets deallocated, the weak property’s value will get set to nil.


unsafe_unretained
This is simply pointing one variable to another. This will not retain the object into the new variable, it will simply assign the object to the variable. 


source from :  iOS6 Programming Cookbook 

Tuesday, January 1, 2013

iOS -- JSON to Core Data

Now, using JSON to pass data from web service to iOS app is quite popular. JSON is much light weight than XML or others.  But how can we mapping JSON data to Core data class or object ?

Here is the answer:,

At first, you have get your json data from web service or wcf. Here is the format of the JSON,

For example:


{"BOSS":[{"name":"John Carlos","mobile":"12345678","email":"a@b.com"},{"name":"Steve Jobs","mobile":"87654321","email":"d@e.com"}]}

 The class name is BOSS, and it has three attributes, whihc is name, mobile and email.


Second, you must have a json library to parse the json string, I am using SBJSON framework.

And the you parse the json string into a NSDictionary. Like below

      
Thirdly, mapping the json data to your Core data object using setValuesForKeysWithDictionary method


In the core data you created, the Boss entity's attribute must same as the JSON data attribute,
like name, mobile and email.

In this way, you can quickly insert you json data to your core data.

And more, here is a very useful link about JSON to core data, you can have a look:

http://www.cimgf.com/2011/06/02/saving-json-to-core-data/



Sunday, December 9, 2012

Network Link Conditioner -- iOS

In Xode, there is a tool called "Network Link Conditioner",  it can allow the user to simulator different network connection and bandwidth. Mac and iOS developer to test their app under different network environment.


In Xcode, Xcode -> Open Developer Tool -> More Developer Tools




After enter Apple Developer Center, download the Hardware IO Tools





After install, you can see "Network Link Conditioner.prefPane"




After double click "Network Link Conditioner.prefPane",  Network Link Conditioner will added to System Preferences.




Click on "Network Link Conditioner", in Profile, it have 3G, DSL,Edge,Wifi.





You can also define your own profile, click on "Manage Profiles..."



After test, must turn off "Network Link Conditioner".