Easy way to save an image in the photo of the device inside of our application (or loaded remotely):
Category 'Objective-C'
Very short snippet: recording an image in the Photo Album
NSURLConnection: Example of use
NSURLConnection allows to perform a connection cotrollata to a remote server. To use it:
1 2 3 4 5 6 7 8 9 10 11 | / / The objects sufficient to perform a NSURLConnection urlConnection; NSURLConnection * URLConnection; mutableData; NSMutableData mutableData *; / / ... urlString = @ "http://www.miodominio.com/documento.txt" ; NSString * urlString @ = "http://www.miodominio.com/documento.txt"; urlRequest = [ NSURLRequest requestWithURL : [ NSURL URLWithString : urlString ] ] ; NSURLRequest urlRequest * = [ NSURLRequest requestWithURL: [ NSURL URLWithString: urlString]]; / / The delegate will respond to connection states NSURLConnection alloc ] initWithRequest : urlRequest delegate : self ] ; URLConnection = [[ NSURLConnection alloc] initWithRequest: urlRequest delegate: self]; |
Here are the delegate methods to check the status of the connection:
[Cc_objc]
Very short snippet: Objective-C, Selector from NSString
Objective-C is a wonderful language that allows you to do amazing things. One of the most interesting aspects is its dynamic invocation of methods (messages). It is possible, in fact, to obtain the address of a message from a string.
Very short snippet: UIWebView, and display PDF files inside
The object UIWebView can be used for the display of numerous files. For example you can use it to display - as well as QuickTime movies or YouTube - PDF or HTML files into our own code.
NSString
NSString is a very powerful class, let me show you some of the most used properties:
printf ()
1 2 |
Run the split ()
1 2 3 |
Convert from string to value
1 2 3 | / / Converting doubleString = @ "123" ; NSString * doubleString @ = "123"; [ doubleString doubleValue ] ; double value = [doubleString doubleValue]; |
Within a string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | / / Substring searchString = @ "age" ; NSString * searchString = @ "age"; beginsTest = @ "Agencies" ; NSString * beginsTest @ = "Agencies"; [ beginsTest rangeOfString : searchString NSRange prefixRange = [beginsTest rangeOfString: searchString NSAnchoredSearch | NSCaseInsensitiveSearch ) ] ; options: (NSAnchoredSearch | NSCaseInsensitiveSearch)]; / / PrefixRange = {0, 3} endsTest = @ "BRICOLAGE" ; NSString * endsTest = @ "DIY"; [ endsTest rangeOfString : searchString NSRange suffixRange = [endsTest rangeOfString: searchString NSAnchoredSearch | NSCaseInsensitiveSearch | NSBackwardsSearch ) ] ; options: (NSAnchoredSearch | NSCaseInsensitiveSearch | NSBackwardsSearch)]; / / SuffixRange = {6, 3} |
Very short snippet: composing emails to an iPhone, iPod or iPad
To compose an email in iPhone / iPod just add the framework MessageUI . In our controller to enter the inclusion of the framework and implement the protocol MFMailComposeViewControllerDelegate :
Objective-C class methods and self alloc
davanti al prototipo, tipo: When we define and use methods (messages) under Objective-C, we are often faced with the curious syntax that shows a sign - or + in front of the prototype, type:
1 2 3 4 5 6 7 | / / In the definition void ) mioMessaggio; - (Void) mioMessaggio; / / Similarly, in the implementation void ) mioMessaggio { - (Void) {mioMessaggio / / ... } |
Or:
1 2 3 4 5 6 7 | / / In the definition void ) mioMessaggio; + (Void) mioMessaggio; / / Similarly, in the implementation void ) mioMessaggio { + (Void) {mioMessaggio / / ... } |
The difference resides in the fact that the methods defined by the symbol - are methods for instance, and then linked to an object. The methods defined by the symbol + are called class methods, as they can be performed without allocating and instantiate the object in question.
sono due classi, molto usate, che contengono svariati metodi di classe. NSString or UIView are two classes, widely used, which contain several class methods. Class methods are used constantly, like when we initialize or allocate any object:
1 | [ UIView alloc ] ; MyView UIView * = [UIView alloc]; |
The method alloc is a classic example, present in all objects and, as evidenced by the code, is a class method as claimed before allocation of the object itself.
Class methods can be useful in many cases, particularly when we create our object and we want to allocate and initialize it in fewer lines of code possible. Imagine having to collect an array in a set of objects defined by us. We define our first object, writing the code in the simplest way, without using class methods:
1 2 3 4 5 6 7 8 9 10 | / / DEFINITION the interface in myObject.h # Import <Foundation/Foundation.h> NSObject { @ Interface myObject: NSObject { name; NSString * name; lastname; NSString * lastname; } nonatomic, retain ) NSString * name; @ Property (nonatomic, retain) NSString * name; nonatomic, retain ) NSString * lastname; @ Property (nonatomic, retain) NSString * lastname; |
The implementation, in the simplest case, it may be nothing or:
1 2 3 4 5 6 7 8 9 10 11 12 | / / MyObject.m # Import "myObject.h" @ Implementation myObject @ Synthesize name, lastname; void ) dealloc { - (Void) dealloc { ; [Name release]; ; [Lastname release]; ; [Super dealloc]; } |
When are we going to use our object, we would use code like this:
1 2 3 | [ myObject alloc ] ; myObject * obj = [myObject alloc]; "Mario" ; obj.name @ = "John"; "Rossi" ; obj.lastname @ = "Smith"; |
If we wanted to create many objects of this type, and place them in an NSArray , the situation becomes a little awkward:
1 2 3 4 5 6 7 8 9 10 11 12 | [ myObject alloc ] ; objA * myObject = [myObject alloc]; "Mario" ; objA.name @ = "John"; "Rossi" ; objA.lastname @ = "Smith"; [ myObject alloc ] ; objB * myObject = [myObject alloc]; "Carlo" ; objB.name @ = "Charles"; "Bianchi" ; objB.lastname @ = "Smith"; elenco = [ NSArray arrayWithObjects : objA, objB, nil ] ; NSArray * list = [ NSArray arrayWithObjects: objA, objB, nil]; ; [ObjA release]; ; [ObjB release]; |
per aggiungere man mano gli oggetti nel nostro elenco. It could improve the code by creating a loop for or using an NSMutableArray to add as objects in our directory. . However, the situation migliorebbe slightly, occasionally remain outside the property settings name and lastname . Would then be spontaneous, to start, add a method - object - initWithName that allow you to jump to any property settings, semplificandoci things a bit. In the implementation file myObject.m add:
1 2 3 4 5 6 7 | id ) initWithName : ( NSString * ) stringName lastname : ( NSString * ) stringLastname { - (Id) initWithName: ( NSString *) StringName lastname: ( NSString *) {stringLastname self = [ super init ] ) { if (self = [super init]) { self.name = StringName; self.lastname = stringLastname; } return self; } |
In doing so we have improved the situation, they can now write:
1 2 3 4 5 6 7 | [ [ myObject alloc ] initWithName : @ "Mario" lastname : @ "Rossi" ] ; objA * myObject = [[myObject alloc] initWithName: @ "Mario" lastname: @ "Smith"]; [ [ myObject alloc ] initWithName : @ "Carlo" lastname : @ "Bianchi" ] ; objB * myObject = [[myObject alloc] initWithName: @ "Charles" lastname: @ "Smith"]; elenco = [ NSArray arrayWithObjects : objA, objB, nil ] ; NSArray * list = [ NSArray arrayWithObjects: objA, objB, nil]; ; [ObjA release]; ; [ObjB release]; |
, necessari per l'inserimento nell'array e liberare la memoria. Abbiammo yet still pointers objA and objB , necessary for the entry in the array and free memory. Wishing we could directly enter the creation of an object in populating the array, using autorelease to free memory, but the code still would not idle. Let me demonstrate how to resolve the issue with a class method. First of all we replace our - (id)initWidthName with:
1 2 3 4 5 6 7 8 9 10 | id ) initWithName : ( NSString * ) name lastname : ( NSString * ) lastname { + (Id) initWithName: ( NSString *) name lastname: ( NSString *) {lastname myObject * item; item = [ [ self alloc ] init ] ) { if (item = [[self alloc] init]) { / / Init item.name = name; item.lastname = lastname; } item autorelease ] ; return [item autorelease]; } |
In doing so we created a class method that allocates (in autorelase) and iniziallizza our subject, before you have the pointer to the instance. The code used is then:
1 2 3 4 |
Much, much better ...
iPad: handle boot screens
On Apple iPhone and iPod were used to manage a single image file to load the application, the file Default.png . On Apple iPad, however, the different management dell'orientamente requires the adoption of multiple image files, to be sure you see the splash screen all'orientamente corrected for the device. During application startup, as it did for the iPhone, it is not possible to take the code for "wonder" as the device-oriented. Fortunately it was introduced in automatic loading of special files on the orientation:

The files are currently supported, in addition to the classic Default.png that you should never use because it is scaled and deformed according to guidance, are:
- Default-Portrait.png
- Default-PortraitUpsideDown.png
- Default-Landscape.png
- Default-LandscapeLeft.png
- Default-LandscapeRight.png
e LandscapeRight possono essere utilizzate per determinare orietamento e verso di quest'ultimo. The versions PortraitUpsideDown , LandscapeLeft and LandscapeRight can be used to determine orietamento and towards the latter.
For application starts, then, as recommended by Apple, it is good to "redesign" - where necessary - our views acting within application:didFinishLaunchingWithOptions .
Customize the sections in a UITableView Grouped
potremmo aver necessità di personalizzare la grafica dei titoli delle sezioni, come California o New York dell'esempio qui sotto. When we use a UITableView style Grouped we may need to customize the layout of section titles, such as California or New York the example below.

To do so, please use the following code, placing it in the delegate, ie the class that responds to the protocol UITableViewDelegate :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | / / I return my custom View, in this case an object / / Type UILabel UIView * ) tableView : ( UITableView * ) tableView - (UIView *) tableView: (UITableView *) tableView NSInteger ) section { viewForHeaderInSection: (NSInteger) section { [ [ [ UILabel alloc ] initWithFrame : CGRectZero ] autorelease ] ; UILabel * label = [[[UILabel alloc] initWithFrame: CGRectZero] autorelease]; UIFont boldSystemFontOfSize : 20 ] ; label.font = [UIFont boldSystemFontOfSize: 20]; label.textAlignment = UITextAlignmentCenter; UIColor blackColor ] ; label.shadowColor = [UIColor blackColor]; 1 , 1 ) ; label.shadowOffset CGSizeMake = (1, 1); "Sezione" ; // Sostituire con un array come al solito Label.Text @ = "Section", / / Replace with an array as usual UIColor whiteColor ] ; label.textColor = [UIColor whiteColor]; UIColor clearColor ] ; label.backgroundColor = [UIColor clearColor]; ; label.opaque = NO; return label; } / / We must also support this message will not work CGFloat ) tableView : ( UITableView * ) tableView - (CGFloat) tableView: (UITableView *) tableView NSInteger ) section { heightForHeaderInSection: (NSInteger) section { ; return 44; } |
It is also important to include heightForHeaderInSection , it will not work.
Notes of Interest
o UIImageView , ho utilizzato per inizializzare il frame CGRectZero che corrisponde a CGRectMake(0,0,0,0) . In the creation of our UILabel , that wanting could also be a more complex object such as a UIView or UIImageView , I used to initialize the frame CGRectZero which corresponds to CGRectMake(0,0,0,0) .
How to get Latitude and Longitude in Objective-C
The MapKit framework provides many useful features, except the return of Latitude and Longitude from an address. In JavaScript, for example, you can use the service provided by Google Geocoding and discussed in Google Maps: How to get Latitude and Longitude from an address . On Apple iPhone or iPad, however, you can overcome the obstacle by using a different Google services. Specifically, you can directly call the url:
1 | http://maps.google.com/maps/geo?q = [address] & output = csv |
Where is [indirizzo] to enter the string with the address you want to transform coordinates. The output returned is of type:
1 | 200,8,41.9128300,12.2241172 |
). The first value, 200 , indicates that everything went well ( 200 OK ). The second, 8 , is the Google accuracy parameter (1-10). The last two values are, finally, latitude and longitude. Now we see a prototype of a method can be included in our applications:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | CLLocationCoordinate2D ) getLocationFromAddress : ( NSString * ) address { - (CLLocationCoordinate2D) getLocationFromAddress: ( NSString *) address { urlString = [ NSString stringWithFormat : @ "http://maps.google.com/maps/geo?q=%@&output=csv" , NSString * urlString = [ NSString stringWithFormat: @ "% @ http://maps.google.com/maps/geo?q = & output = csv" NSUTF8StringEncoding ] ] ; [Address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; listItems = [ locationString componentsSeparatedByString : @ "," ] ; NSArray * ListItems = [locationString componentsSeparatedByString: @ ""]; / / Int zoom = 0; 0.0 ; double latitude = 0.0; 0.0 ; double longitude = 0.0; listItems count ] > = 4 && [ [ listItems objectAtIndex : 0 ] isEqualToString : @ "200" ] ) { if ([ListItems count]> = 4 && [[ListItems objectAtIndex: 0] isEqualToString: @ "200"]) { / / Zoom = [[ListItems objectAtIndex: 1] intValue]; listItems objectAtIndex : 2 ] doubleValue ] ; latitude = [[ListItems objectAtIndex: 2] doubleValue]; listItems objectAtIndex : 3 ] doubleValue ] ; longitude = [[ListItems objectAtIndex: 3] doubleValue]; { Else {} / / Error } CLLocationCoordinate2D location; location.latitude = latitude; location.longitude = longitude; return location; } |
Notes of Interest
, alla stregua della funzione explode ( ) del PHP per intenderci. The string returned in locationString is "splitted" by the method componentsSeparatedByString , like the function explode ( ) of PHP for instance. In the example I proposed I entered - but commented - the code to retrieve even the Google parameter accuracy, precision or scale factor, denoted by zoom .
Source as
For completeness, I made a small example application with which you can try the method proposed above, enter any address and the iPhone will display on the map.
I thank the team devAPP for the inspiration of this article.








Latest Comments
Subject : very helpful indeed! I tried it and it is just what I needed. Now I wonder how do I get ...
vik : With strategic help!
Pepper : Hi there, I do not know if you're one of the creators of the WP plugin Bannerize. I have spotted a ...
Rosanna : Can anyone tell me how do I delete the Snap Shots window that opens automatically when I ...
blessed Maresca : I can not download any skypemote me spiegaaa