Category 'Mobile'
On Apple iPhone and iPod we used to manage a single image file when the application loads, the file Default.png . On Apple iPad, however, the different management dell'orientamente requires the adoption of multiple image files, to make sure you see the splash screen all'orientamente correct for the device. When the application starts, as was the case for the iPhone, it is not possible to speak code for "wonder" in this orientation the device. Fortunately it was introduced in the automatic loading of special files depending on the orientation:

The files are currently supported, in addition to the classic Default.png not recommend that you use as it is resized and warped based on the orientation 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 Portrait View and towards the latter.
For application started, then, as recommended by Apple, it is good to "redesign" - where necessary - our views acting in application:didFinishLaunchingWithOptions .
Continued ...
In most cases, either because we are used to or because we have seen in tutorials and in some texts, we use the setAnimationDidStopSelector in this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| nil context : NULL ] ; [UIView beginAnimations: nil context: NULL]; 1.5 ] ; [UIView setAnimationDuration: 1.5]; UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; self ] ; [UIView setAnimationDelegate: self]; @selector ( removeView ) ] ; [UIView setAnimationDidStopSelector: @ selector (removeView)];
; myView.alpha = 0;
; [UIView commitAnimations];
/ /
void ) removeView { - (Void) {removeView ; [MyView removeFromSuperview]; } |
come delegato e tramite la setAnimationDidStopSelector gli invia un messaggio removeView quando l'animazione è terminata. In the code above, the setAnimationDelegate sets self as a delegate and through setAnimationDidStopSelector sends him a message removeView when the animation is finished. The code itself is correct, however, makes use of a message definition ( removeView ) that could be omitted. Now, here is the same code, with the same effect, without the message removeView :
1 2 3 4 5 6 7 8 9
| nil context : NULL ] ; [UIView beginAnimations: nil context: NULL]; 1.5 ] ; [UIView setAnimationDuration: 1.5]; UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; myView ] ; [UIView setAnimationDelegate: myView]; @selector ( removeFromSuperview ) ] ; [UIView setAnimationDidStopSelector: @ selector (removeFromSuperview)];
; myView.alpha = 0;
; [UIView commitAnimations]; |
! The interesting thing about this approach is that myView could be a subclass of a UIView ! It may therefore be a custom class with our own messages and, as explained, quietly callable from setAnimationDidStopSelector . In addition, the setAnimationDidStopSelector accepts selectors with parameters:
1 2 3 4 5 6 7 8 9
| nil context : NULL ] ; [UIView beginAnimations: nil context: NULL]; 1.5 ] ; [UIView setAnimationDuration: 1.5]; UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; myView ] ; [UIView setAnimationDelegate: myView]; @selector ( myMessage : param1 : ) ] ; [UIView setAnimationDidStopSelector: @ selector (myMessage: param1:)];
; myView.alpha = 0;
; [UIView commitAnimations]; |
This example can be extended to all cases here where we set a delegate, Grim is not that a pointer to an instance of any object.
Continued ...
After explaining how to locate our strings in Xcode , let's see now how much is simple - applying the same technique - to locate and view images / interfaces built with Interface Builder.
Locate graphic resources
The process, as mentioned, is the same, if we have an image already inserted in our resources, or we insert a new one, and we want to "localize" - that is to manage two or more images based on the languages supported - simply click the button Right image ( Adium.png in this example) and select Get Info:

Make File Localizable click on the bottom left.

Click on Add and insert Localization Italian :

In order to obtain:

esattamente come accadeva con il testo: Our image is moved (physically, one of the rare times when that happens nell'alberatura Xcode is reflected in the filesystem) under virtual folders English.lproj and Italian.lproj as was the case with the text:

contiene una stessa versione dell'immagine. At this point each of the folders English.lproj and Italian.lproj contains a same version of the image. This image is manipulated in Interface Builder, where we will see - by default - the English version.
At this point, just overwrite one (or both files Adium.png ) to achieve localization of the images "flash".
Locate the file XIB
Even the interfaces built with Interface Builder can be localized in their entirety, when it is deemed necessary. ) con interfaccia XIB , lo selezioniamo, scegliamo Get Info dal menu contestuale, rendiamo il file localizzabile, aggiungiamo la localizzazione in italiano: The procedure is identical to that performed with the graphical resources: add a ViewController (eg infoViewController ) interface XIB, we select it, choose Get Info from the contextual menu, we make the localizable files, add localization in Italian:

Clicking Inglese or English will open Interface Builder! ) all'interno della classica cartella Classes . This time, the filesystem, you will notice that you have created two folders ( English.lproj and Italian.lproj ) in the classical Classes folder. Both will have their file infoViewController.xib . The comfort in this, resolved evident in the code, and when we are going to instantiate our controller code you will have a "clean" like this:
1 2
| [ InfoViewController alloc ] ; InfoViewController * info = [InfoViewController alloc]; info.view ] ; [Self.view addSubview: info.view]; |
As you can see there is no record of any statement concerning the location, fully managed by the system. The two interfaces, of course, can be completely different, as they are to all effects as two separate files XIB.
Continued ...
Perhaps I should write "Very very very, short snippet", however it is an excellent and convenient "trick" that I'm about to show. By running the "two" lines of code shown below, you can get the output of any URL and manipulate it.
Continued ...
Run a method after n seconds
The whole family performSelector is really interesting and can be useful in a multitude of cases. Its application simplest and most common is the following:
1 2 3 4 5
| @selector ( myMethod ) withObject : nil afterDelay : 3 ] ; [Self performSelector: @ selector (myMethod) withObject: nil afterDelay: 3]; / / void ) myMethod { - (Void) myMethod { "Hello World!" ) ; NSLog (@ "Hello World!"); } |
However, consider that the "timer" is not accurate. This procedure, therefore, should be used when it is not prompted for a "significant" temporal precision.
Retrieve the version of the application
1 2
| version = [ [ [ NSBundle mainBundle ] infoDictionary ] objectForKey : @ "CFBundleVersion" ] ; NSString * version = [[[ NSBundle mainBundle] infoDictionary] objectForKey: @ "CFBundleVersion"]; "versione = %@" , version ) ; NSLog (@ "version =% @", version); |
YES, TRUE or false?
e TRUE sono in pratica la stessa edentica cosa: Going to comb through the Kernel Apple iPhone you can realize that YES , true and TRUE edentica are basically the same thing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| / / Definition of the YES # Define YES (BOOL) 1 # Define NO (BOOL) 0
/ / Definition of true # Define true 1 # Define false 0
/ / Definition of TRUE # If! Defined (TRUE) # Define TRUE 1 # Endif
# If! Defined (FALSE) # Define FALSE 0 # Endif |
At least for now ...
Chatter
1 2 3
| # Import <AudioToolbox/AudioToolbox.h> / / ; AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); |
Pointer CGImageRef from a UIImage
1 2 3 4
| [ UIImage imageNamed : @ "LittleHeart.png" ] ; UIImage * heart = [UIImage imageNamed: @ "LittleHeart.png"]; heart CGImage ] ; CGImageRef image = [heart CGImage]; / / The image can now be "rasterized" on a CGContextRef CGRect ) { 0 , 0 , 100 , 100 } , image ) ; CGContextDrawImage (c, (CGRect) {0, 0, 100, 100}, image); |
Animations
1 2 3 4 5
| nil context : NULL ] ; [UIView beginAnimations: nil context: NULL]; 1.5 ] ; [UIView setAnimationDuration: 1.5]; UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; / / ... ; [UIView commitAnimations]; |
NSLog
1 2 3
| "NSString object %@ " , myString ) ; NSLog (@ "NSString object% @", myString); "Float: %f " , myFloat ) ; NSLog (@ "float:% f", myFloat); "Integer: %i " , myInt ) ; NSLog (@ "Integer:% i", myInt); |
Converter RGB to UIColor
1
| # Define RGBA (r, g, b, a) [UIColor colorWithRed: r/255.0 green: blue g/255.0: b/255.0 alpha: a] |
Passing parameters to a NSTimer
Taking advantage of the parameter userInfo you can send a pointer to an object to the method invoked by our timer.
1 2 3 4 5 6 7 8 9 10 11 12 13
| scheduledTimerWithTimeInterval : 1 target : self selector : @selector ( timerMethod ) userInfo : objectPointer repeats : YES ] ; [ NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @ selector (timerMethod) userInfo: objectPointer repeats: YES]; / / ... void ) timerMethod : ( NSTimer * ) timer { - (Void) timerMethod: ( NSTimer *) timer { / / Retrieve the pointer to my object timer userInfo ] ; objectPointer = [timer userInfo]; / / Or myMethod ] ; [[Timer userInfo] myMethod]; [ [ timer userInfo ] myProperty ] ; int a = [[timer userInfo] myProperty]; / / Which is the same [ objectPointer myProperty ] ; int a = [objectPointer myProperty]; } |
Execution time
Here's a simple way to calculate short time needed to check the speed of execution of the code:
1 2 3 4
| ; CFAbsoluteTime initialTime CFAbsoluteTimeGetCurrent = (); / / ... code ; CFAbsoluteTime finalTime CFAbsoluteTimeGetCurrent = (); "Tempo trascorso %f" , finalTime - initialTime ) ; NSLog (@ "Elapsed time% f", finalTime - initialTime); |
Continued ...
I would like to show and discuss some examples on how to add and manipulate a property in Objective-C class. : A classic example, precisely, is as follows, in the definition of our class interface we define two properties nome and cognome :
e setter usati rispettivamente per leggere ed impostare le nostre due proprietà: In the implementation file we insert the declaration @synthesize so that Xcode produce for us the methods getter and setter used to get and set up our two properties:
1 2 3 4 5 6 7 8
| / / MyClass.m # Import "MyClass.h"
@ Implementation MyClass
@ Synthesize name, last name;
@ End |
, possiamo scrive: When you're going to use our class MyClass , ie when istanziaremo an object of type MyClass , we can write:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| / / Any other class, as AppDelegate / / In the file. H # Import <UIKit/UIKit.h> # Import "MyClass.h" @ Class TestViewController; NSObject <UIApplicationDelegate> { @ Interface TesAppDelegate: NSObject {<UIApplicationDelegate> UIWindow * window; TestViewController * viewController; MyClass * myClass; } / / In the file. M MyClass alloc ] ; myClass = [MyClass alloc]; "Giovambattista" ; miaClasse.nome = @ "Giovambattista"; "miaClasse.nome = %@" , miaClasse.nome ) ; NSLog (@ "miaClasse.nome =% @", miaClasse.nome); |
Or, which is equivalent to:
1 2 3
| / / Always in the file. M @ "Undolog" ] ; [MyClass setNome: @ "Undolog"]; "miaClasse.nome = %@" , [ miaClasse nome ] ) ; NSLog (@ "miaClasse.nome =% @", [myClass name]); |
So far so good. However, it could mislead the equivalence of "variabli" internal (ivar) with the name of the property itself. To understand the difference, propose again the same as doing without, this time, the @synthesize . . Now, therefore, we should take us to write the methods getter and setter . To further underline the differences, rinominerò the internal variables by inserting an underscore in front of the name. But let's see the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # Import <Foundation/Foundation.h> NSObject { @ Interface MyClass: NSObject { _nome; NSString * _Name; _cognome; NSString * _cognome; } NSString * ) nome; // get - ( NSString *) name; / / get NSString * ) cognome; // get - ( NSString *) name; / / get void ) setNome : ( NSString * ) stringaIngresso; // set - (Void) setNome: ( NSString *) stringaIngresso; / / set void ) setCognome : ( NSString * ) stringaIngresso; // set - (Void) setCognome: ( NSString *) stringaIngresso; / / set @ End |
. Unlike the previous pointers to internal variables (incapsultate) have become _nome and _cognome . . @property è scomparso, in quanto non serve più. In addition there are four definitions of methods that represent our get and set . @property has disappeared, as no longer needed.
We see the implementation file MyClass.m :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # Import "MyClass.h" @ Implementation MyClass / / Get a "name" NSString * ) nome { - ( NSString *) name { _Name return; } / / Set to "name" void ) setNome : ( NSString * ) stringaIngresso { - (Void) setNome: ( NSString *) {stringaIngresso _Name = stringaIngresso; } / / Get for "surname" NSString * ) cognome { - ( NSString *) name { _cognome return; } / / Set for "surname" void ) setCognome : ( NSString * ) stringaIngresso { - (Void) setCognome: ( NSString *) {stringaIngresso _cognome = stringaIngresso; } @ End |
A class so written can be used exactly as the previous one, namely:
1 2 3 4 5 6 7 8
| MyClass alloc ] ; myClass = [MyClass alloc]; "Giovambattista" ; miaClasse.nome = @ "Giovambattista"; "miaClasse.nome = %@" , miaClasse.nome ) ; NSLog (@ "miaClasse.nome =% @", miaClasse.nome);
/ / Or, which is equivalent to:
@ "Undolog" ] ; [MyClass setNome: @ "Undolog"]; "miaClasse.nome = %@" , [ miaClasse nome ] ) ; NSLog (@ "miaClasse.nome =% @", [myClass name]); |
e set , evidenziando – anche con l'aggiunta dell'underscore – le differenze tra il nome della proprietà e la sua ivar interna _nome . At the level of educational neglect @synthesize forced us to write "yourself" methods get and set , highlighting - even with the addition dell'underscore - the difference between the name of the property and its ivar internal _nome .
permette un reale controllo del dato prima della sua impostazione (o prima della sua lettura) e quindi un reale incapsulamento per proteggere la variabile interna. At a functional level the use of personal methods get and set allows real control of the data before its setting (or before reading it) and then a real encapsulation to protect the internal variable.
For example it would be possible to prevent the passage of empty strings to the property nome :
1 2 3 4
| void ) setNome : ( NSString * ) stringaIngresso { - (Void) setNome: ( NSString *) {stringaIngresso stringaIngresso == @ "" ) stringaIngresso = @ "senza nome" ; if (@ stringaIngresso == "") stringaIngresso = @ "no name"; _Name = stringaIngresso; } |
Further variant
If you wish to use the internal variables with the underscore in front (who rpoviene from Adobe Actionscript could be used as well) do not need to abandon the use of the directive @synthesize . Xcode makes it possible to "merge" the methods described above:
1 2
| _nome; @ Synthesize name = _Name; _cognome; @ Synthesize name = _cognome; |
. By doing so we could use the pointer to internally _nome , "summed up" - to the outside - as a property nome . e setter , è vero anche che lo fa solo se non li trova, quindi se desiderate “implementare” un vostro metodo di getter e/o setter potete farlo anche se avete usato la direttiva @synthesize . In addition, if it is true that the use of @synthesize produces the automatic generation methods (messages) of getter and setter , it is also true that it only does so if you do not find them, so if you want to "implement" a method for your getter and / or setter can do it even if you used the directive @synthesize .
Memory allocations
In the examples above I omitted some important details for a real implementation. First of all, I have not presented any method init() , useful for object initialization and default values. Furthermore, it lacks the addition of a method dealloc() :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| / / File MyClass.m # Import "MyClass.h" @ Implementation MyClass id ) init { - (Id) init { self = [ super init ] ) { if (self = [super init]) { "Nome preimpostato" ; _Name = @ "Preset Name"; "Cognome preimpostato" ; _cognome = @ "Preset Name"; } return self; } void ) dealloc { - (Void) dealloc { ; [_Name Release]; ; [_cognome Release]; ; [Super dealloc]; } NSString * ) nome { - ( NSString *) name { _Name return; } void ) setNome : ( NSString * ) stringaIngresso { - (Void) setNome: ( NSString *) {stringaIngresso stringaIngresso == @ "" ) stringaIngresso = @ "senza nome" ; if (@ stringaIngresso == "") stringaIngresso = @ "no name"; _Name = stringaIngresso; } NSString * ) cognome { - ( NSString *) name { _cognome return; } void ) setCognome : ( NSString * ) stringaIngresso { - (Void) setCognome: ( NSString *) {stringaIngresso _cognome = stringaIngresso; } @ End |
, etc… In the future we will see the details about the property readonly , retain , etc ... 
Continued ...
Our website now is not only displayed by PCs. With the spread of mobile, thanks mainly to Apple iPhone, access to the site or blog is increasingly being made from mobile devices of various types. It is therefore need to know how many Web Developer intercept and identify the different "agents", that is, the means by which a user views (sailing) our pages.
Continued ...
non funzionano quando un UIButton è impostato in modalità UIButtonTypeCustom ! The states UIControlStateSelected or UIControlStateHighlighted not work when a UIButton is set in mode UIButtonTypeCustom ! Or rather, do not work as they should (because reserved for other types of button), for example to create a button with two states: toggle note. If we created two images (stato1.png and stato2.png) for our button, we can proceed in this way:
1 2 3
| / / The header file we create a global variable that usaremo for / / Check the toggle were BOOL toggleFlag; |
Now let's create our button:
1 2 3 4 5 6 7 8 9
| / / Creaiamo a button and we place it first in the "stato1.png" / / Edit initWithFrame: (CGRect) {} 100,100,50,50 with the location and / / Size of your specular imaging ; toggleFlag = YES; [ [ UIButton buttonWithType : UIButtonTypeCustom ] initWithFrame : ( CGRect ) { 100 , 100 , 50 , 50 } ] ; UIButton * ToggleButton = [[UIButton buttonWithType: UIButtonTypeCustom] initWithFrame: (CGRect) {100, 100, 50, 50}]; @ "" forState : UIControlStateNormal ] ; [ToggleButton setTitle: @ "" forState: UIControlStateNormal]; [ UIImage imageNamed : @ "stato1.png" ] forState : UIControlStateNormal ] ; [ToggleButton setBackgroundImage: [UIImage imageNamed: @ "stato1.png"] forState: UIControlStateNormal]; self action : @selector ( onToggle : ) forControlEvents : UIControlEventTouchUpInside ] ; [ToggleButton addTarget: self action: @ selector (onToggle:) forControlEvents: UIControlEventTouchUpInside]; toggleButton ] ; [Self.view addSubview: ToggleButton]; |
When you click the button will be sent a message handled by onToggle :
1 2 3 4 5 6 7
| void ) onToggle : ( id ) sender { - (Void) onToggle: (id) sender { / / Retrieve pointer to UIButton ( UIButton * ) sender; ButtonClicked UIButton * = (UIButton *) sender; / / I run toogle toggleFlag =! toggleFlag; [ UIImage imageNamed : ( toggleFlag ) ? @ "stato1.png" : @ "stato2.png" ] forState : UIControlStateNormal ] ; [ButtonClicked setBackgroundImage: [UIImage imageNamed: (toggleFlag)? @ "Stato1.png": @ "stato2.png"] forState: UIControlStateNormal]; } |
Continued ...
An alternative really easy to run a stream of an mp3 file on the Apple iPhone might be:
Continued ...
o CGPoint , ad esempio. The syntax NSLog(@"%@", ... ); works and is used to obtain information on objects, but does not work on C data types such as struct CGRect or CGPoint , for example. o NSStringFromCGPoint : To take advantage NSLog(@"%@", ... ); also on C-style structs can lean on conversion functions like NSStringFromCGRect() or NSStringFromCGPoint :
1 2 3 4 5
| CGRect ) { 10 , 20 , 30 , 40 } ; CGRect mioRect = (CGRect) {10, 20, 30, 40}; CGPoint ) { 32 , 64 } ; CGPoint mioPoint = (CGPoint) {32, 64}; / / "Info rettangolo: %@" , NSStringFromCGRect ( mioRect ) ) ; NSLog (@ "Info rectangle:% @", NSStringFromCGRect (mioRect)); "Info point: %@" , NSStringFromCGPoint ( mioPoint ) ) ; NSLog (@ "Info point:% @", NSStringFromCGPoint (mioPoint)); |
Specifically, you can refine this procedure scrivendosi of small macros which are useful as:
1
| # Define NSLogRect (rect) NSLog (@ "% s: (% 0.0f,% 0.0f)% 0.0fx% 0.0f", # rect, rect.origin.x, rect.origin.y, rect.size.width , rect.size.height) |
Or:
1 2 3 4
| # Define NSLogCGPoint (point) NSLog (@ "% s: (% 0.0f,% 0.0f)", # point.x point, Point.y)
CGPoint ) { 32 , 64 } ; CGPoint mioPoint = (CGPoint) {32, 64}; ; NSLogCGPoint (mioPoint); |
That will give as output:
1
| 32 , 64 ) mioPoint: (32, 64) |
Continued ...
Latest Comments
Daniela : Hello! I try to ask a help to solve my problem. I'm migrating my blog from tumblr to ...
Ludovica : Hello! I'll explain my doubt. When I write a post not add pictures in the article (if so ...
Giovambattista Fazioli : @ ale: As shown @ Kevin see on GitHub repo: https://github.com/gfazioli/Ch roma-Key
Giovambattista Fazioli : @ Kevin: See https://github.com/gfazioli/Ch roma-Key
Kevin : Very nice example - would like to see the. fla too!