Category 'Mobile'


XCode 3.1.3: Restore the 2.2.1 SDK

Only to registered developers have already released a new version of the SDK for Apple iPhone, so it's time to install the 3.0 SDK, thus updating the entire development environment XCode to version 3.1.3. If you have already upgraded you will have noticed that creating a new project seem to be missing the old SDK 2.2.1 (including other).

xcode313

In fact it's all here on our machine, you can just perform a simple procedure to restore magically all our previous SDK, so try out our application for the Apple iPhone is the SDK 3.0 that with previous versions 2.2.1 and lower. Select the project, open the context menu and select Get Info:

xcode131menu

In the window that opens, first in the General tab, select the lower Base SDK for All Configurations and restored SDK 2.2.1 (or whatever).

xcode313info

Without that, magically, the menu will redisplay all the XCode SDK 3.0 including

xcode313ok

Continued ...

iPhone: all system fonts

The Apple iPhone provides a limited number of fonts to developers. The list of available fonts is easily achieved by code, as we shall see. If you want to use your own fonts, such as including it in the resources, it is a little more articulate and conivolge also the issue of licenses (rights) fonts "embed" ... we'll talk later. Turning instead to the official sources present in the iPhone they are (click on image to enlarge):

iphonefonts

The Apple iPhone SDK provides access to the special system fonts. These are identified by special constants and are:

1
2
3
[ UIFont boldSystemFontOfSize : 12.0 ] ; UIFont myBoldFont * = [UIFont boldSystemFontOfSize: 12.0];
[ UIFont SystemFontOfSize : 12.0 ] ; UIFont mySystemFont * = [UIFont SystemFontOfSize: 12.0];
[ UIFont italicSystemFontOfSize : 12.0 ] ; UIFont myItalicFont * = [UIFont italicSystemFontOfSize: 12.0];

If you actually get a pointer to a odei fonts shown in the image above just use:

1
[ UIFont fontWithName : @ "Helvetica-Bold" size : 22.0 ] ; UIFont myCustomFont * = [UIFont fontWithName: @ "Helvetica-Bold" size: 22.0];

As you can see the management of the font is special, in addition to the family (Helvetica, Courier, etc ...) must specify the type (bold, italic, etc ...). In practice, therefore, a font must be equipped with these features. The Helvetica, for example, is represented by:

1
2
3
4
Helvetica
Helvetica-Bold
Helvetica-Oblique
Helvetica-BoldOblique

If you want to display your fonts directly on the iPhone, here are some useful code line:

1
2
3
4
5
6
7
8
9
10
11
12
listOfFonts = [ [ NSArray alloc ] initWithArray : [ UIFont familyNames ] ] ; NSArray * listOfFonts = [[ NSArray alloc] initWithArray: [UIFont familyNames]];
subFontTypes; NSArray * subFontTypes;

int i = 0 ; i< [ listOfFonts count ] ; i ++ ) { for (int i = 0; i <[listOfFonts count]; i + +) {
"Font Family: %@" , [ listOfFonts objectAtIndex : i ] ) ; NSLog (@ "Font Family:% @", [listOfFonts objectAtIndex: i]);
NSArray alloc ] initWithArray : [ UIFont fontNamesForFamilyName : [ listOfFonts objectAtIndex : i ] ] ] ; subFontTypes = [[ NSArray alloc] initWithArray: [UIFont fontNamesForFamilyName: [listOfFonts objectAtIndex: i]]];
int j = 0 ; j< [ subFontTypes count ] ; j ++ ) { for (int j = 0 j <[subFontTypes count] j + +) {
"+----->Type: %@" , [ subFontTypes objectAtIndex : j ] ) ; NSLog (@ "+-----> Type:% @ ", [subFontTypes objectAtIndex: j]);
}
; [SubFontTypes release];
}
; [ListOfFonts release];

With the SDK 2.2.1 on my simulator, I got:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Font Family: Courier
+-----> Type: Courier
+-----> Type: Courier-BoldOblique
+-----> Type: Courier-Oblique
+-----> Type: Courier-Bold
Font Family: AppleGothic
+-----> Type: AppleGothic
Font Family: Arial
+-----> Type: ArialMT
+-----> Type: Arial-BoldMT
+-----> Type: Arial-BoldItalicMT
+-----> Type: Arial-ItalicMT
Font Family: sthe TC
+-----> Type: Light-STHeitiTC
+-----> Type: Medium-STHeitiTC
Font Family: Hiragino Kaku Gothic Pron
+-----> Type: HiraKakuProN-W6
+-----> Type: HiraKakuProN-W3
Font Family: Courier New
+-----> Type: CourierNewPS-BoldMT
+-----> Type: CourierNewPS-ItalicMT
+-----> Type: CourierNewPS-BoldItalicMT
+-----> Type: CourierNewPSMT
Font Family: Zapfino
+-----> Type: Zapfino
Font Family: Arial Unicode MS
+-----> Type: ArialUnicodeMS
Font Family: sthe SC
+-----> Type: Medium-STHeitiSC
+-----> Type: Light-STHeitiSC
Font Family: American Typewriter
+-----> Type: AmericanTypewriter
+-----> Type: AmericanTypewriter-Bold
Font Family: Helvetica
+-----> Type: Helvetica-Oblique
+-----> Type: Helvetica-BoldOblique
+-----> Type: Helvetica
+-----> Type: Helvetica-Bold
Font Family: Marker Felt
+-----> Type: Thin-MarkerFelt
Font Family: Helvetica Neue
+-----> Type: HelveticaNeue
+-----> Type: HelveticaNeue-Bold
Font Family: DB LCD Temp
+-----> Type: DBLCDTempBlack
Font Family: Verdana
+-----> Type: Verdana-Bold
+-----> Type: Verdana-BoldItalic
+-----> Type: Verdana
+-----> Type: Verdana-Italic
Font Family: Times New Roman
+-----> Type: TimesNewRomanPSMT
+-----> Type: TimesNewRomanPS-BoldMT
+-----> Type: TimesNewRomanPS-BoldItalicMT
+-----> Type: TimesNewRomanPS-ItalicMT
Font Family: Georgia
+-----> Type: Georgia-Bold
+-----> Type: Georgia
+-----> Type: Georgia-BoldItalic
+-----> Type: Georgia-Italic
Font Family: sthe J
+-----> Type: Medium-STHeitiJ
+-----> Type: Light-STHeitiJ
Font Family: Arial Rounded MT Bold
+-----> Type: ArialRoundedMTBold
Font Family: Trebuchet MS
+-----> Type: TrebuchetMS-Italic
+-----> Type: TrebuchetMS
+-----> Type: Trebuchet-BoldItalic
+-----> Type: TrebuchetMS-Bold
Font Family: sthe K
+-----> Type: Medium-STHeitiK
+-----> Type: Light-STHeitiK

Continued ...

How to delete NSLog () from source XCode

<a target="_blank" href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSLog">NSLog()</a> a useful function during the early stages of a project for testing and debugging an application for Apple iPhone or, more generally, in the XCode environment. Being just a function, just like the other, his presence will be felt even when releasing (release) our executable. potrebbero influire sulle performance della nostra applicazione, soprattutto se abbiamo inserito NSLog() all'interno di loop. It then becomes necessary to remove, in some way, all the rows in NSLog() out of our code, because it is no longer needed, and because calls to NSLog() could affect the performance of our application, especially if we put NSLog() to 'inner loop.

Immediately rule out the solution to the "look" and "cancel" because one day there could be used again. We exclude the solution of the "look" and "comments", uncomfortable for the same reason as before. Fortunately, a clean, simple, and are using the correct compiler conditionals. What we will do in practice, and tell the compiler to exclude - if there is a specific condition - when compiling our source lines that contain NSLog() .

The compiler directives and compiler conditional statements, are a very powerful and widespread. Those coming from the development of the ANSI-C knows very well and will definitely utilizzte in many situations. The peculiarity of these "instructions" lies in the fact, mentioned above, to be seen by the compiler and not executable. This feature makes them useful in many cases and can solve problems otherwise very annoying.

Let's see an example of code that, as predicted, you can "delete" from the compilation of code share, in our case NSLog() :

1
2
3
4
5
6
# Define ACTIVE_NSLOG 1
/ / If the constant is defined ACTIVE_NSLOG fill
/ / The block of code between # ifdef and # endif
# Ifdef ACTIVE_NSLOG
" ... bla bla" ) ; NSLog (@ "... blah blah");
# Endif

). Conditional statements are part of the compiler of the same family as #define , also, in fact, are preceded by a "pound" ( # ). solo se ACTIVE_NSLOG è definito. In the example shown we have defined a constant ACTIVE_NSLOG ; subsequent lines of code tells the compiler to "include" line NSLog() only if ACTIVE_NSLOG is defined. If we took care, during the writing of our code, to place calls to NSLog() within the block #ifdef ... #endif per far sparire, alla prossima compilazione, tutti i nostri NSLog() . #ifdef ... #endif , just delete the definition of the constant ACTIVE_NSLOG to disappear, the next compilation, all our NSLog() .

A best and final

Let us now see how to set the XCode environment to improve even more what we have done here! First of all we choose a constant name that we use in our projects to exclude from the compilation NSLog() . o quello che preferite. You can choose the name that you like, from DEBUG to MIO_DEBUG or whatever you prefer. Open your project, new or old. Enter all the NSLog() within the block (or block):

1
2
3
# Ifdef MIO_DEBUG
" ... bla bla" ) ; NSLog (@ "... blah blah");
# Endif

Select the main file of your project, click the right button and choose the Get Info item.

getinfo

This opens the window with information about the project:

userdefine

Select the Build tab, verify that you are in the Debug configuration (this is the gem), go to the User-Defined section and add, via the button at the bottom left, a new field called OTHER_CFLAGS . At this value assegnamoli -DMIO_DEBUG=1 . The syntax is -D{mia define}=1 .

This procedure has two advantages:

  1. We must not put in the code #define MIO_DEBUG 1 , but we do it through the project information. So, when we go to fill out the release version (the one without the NSLog() ) do not have to remember to delete the line #define MIO_DEBUG 1
  2. The constant is defined in relation to the configuration, in our case Debug. So, switching to Release configuration (release) will be absent and the constant lines with NSLog() will not be filled

Conclusions

The above procedure may be useful in a moltidutine other cases, with NSLog() , have nothing to do. Conditional statements can help the compiler in a wide range of contexts. They are often used by programmers to determine the type of operating system, version, the target, the presence of mathematical processors, while maintaining the same "same" source.

To understand, as an example, we can utilizzre our constant MIO_DEBUG also intervene in other areas of the code:

1
2
3
4
5
6
7
8
9
10
/ / Debug if they win the game
/ / With a score of 100 instead of 10000:)
# Ifdef MIO_DEBUG
score == 100 ) if (score == 100)
# Else
score == 10000 ) if (score == 10000)
# Endif
{
; [HaiVinto self];
}

To finish, here are some examples and variants:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/ / In general there is
# If expression
/ / If statement similar to the traditional, so with a full expression

/ / Check if a constant is defined
# Ifdef constant

/ / Check if it is NOT a constant defined
# Ifndef constant

/ / Else
# Else

/ / Close the block
# Endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/ / For example ...
# Define DEBUG 1
5 # define MIA_ALTRA_COSTANTE

...
# If DEBUG
/ / Fill in this
# Else
/ / Otherwise fill this other
# Endif

# If MIA_ALTRA_COSTANTE> 4
"..." ) ; NSLog (@ "...");
# Endif
1
2
3
4
# Ifndef INCLUDE_MIO_FILE
# Define INCLUDE_MIO_FILE
# Include "mio_file.h"
# Endif

Continued ...

Objective-C: type conversion

With some high-level languages ​​such as JavaScript or PHP, where the data are not typed or otherwise can not be, we are "spoiled" in compare or convert strings and integers and vice versa, all hidden or manipulated by the interpreter (or compiler ). For example, in JavaScript functions are "forced" (like parseInt() for example) required in special cases. However, in other circumstances we treat without worrying about a particular integer conversions (explicit), such as:

1
2
3
5 ; mioNumero var = 5;
/ / ...
"Il valore di mioNumero è " + mioNumero ) ; alert ("The value of mioNumero is" + mioNumero);

In Objective-C, however, the data type is important and the type conversion must be explicit. Besides the casting (better typecasting) we can use the features and functionality specific to the type conversion. For example, here is how to convert an integer to a string:

1
2
3
4
5 ; mioNumero int = 5;
miaStringa = [ NSString stringWithFormat : @ "%d" , mioNumero ] ; NSString * myString = [ NSString stringWithFormat: @ "% d", mioNumero];
/ /
"miaStringa=%@ mioNumero=%i" , miaStringa, mioNumero ) ; NSLog (@ "myString = @ mioNumero% =% i", myString, mioNumero);

In contrast, here is how to convert a string to an integer:

1
2
3
4
miaStringa = @ "128" ; NSString * myString = @ "128";
[ miaStringa integerValue ] ; mioNumero int = [myString integerValue];
/ /
"miaStringa=%@ mioNumero=%i" , miaStringa, mioNumero ) ; NSLog (@ "myString = @ mioNumero% =% i", myString, mioNumero);

Continued ...

iPhone code snippet: UIAlertView

The class UIAletrView allows you to display a modal window (animated) to inform and / or ask you something. The UIAlterView (view alerts) are very comfortable and easy to use. The management of user response is solved by intercepting the index of the button pressed. To view a simple UIAlterView just write:

1
2
3
4
/ / Alert with a simple button, with no callback
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" message : @ "Messaggio" cancelButtonTitle : @ "Annulla" otherButtonTitles : nil ] ; UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title" message: @ "Message" cancelButtonTitle: @ "Cancel" otherButtonTitles: nil];
; [MyAlert show];
; [MyAlert release];

And 'possible to add additional buttons otherButtonTitles :

1
2
3
4
/ / Alert with two simple buttons, no callback
[ [ UIAlertView UIAlertView MyAlert * = [[UIAlertView
; [MyAlert show];
; [MyAlert release];

: To determine which button was pressed, simply add the delegate:self object initialization UIAlterView :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/ / Simple alert with two buttons and callbacks
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" message : @ "Messaggio" delegate : self cancelButtonTitle : @ "Annulla" otherButtonTitles : @ "Ok" , nil ] ; UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title" message: @ "Message" delegate: self cancelButtonTitle: @ "Cancel" otherButtonTitles: @ "OK", nil];
; [MyAlert show];
; [MyAlert release];

/ / The callback looks like this

void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
buttonIndex ) { switch (buttonIndex) {
: case 1:
/ / Code
break;
}
}

The callback alertView need to have that name, for more information see UIAlertViewDelegate

punteranno sempre a quella. With only the callbck alertView:clickedButtonAtIndex more UIAletrView will always point to that. Using the tag of the field UIAletrView , we can determine what the "caller":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/ / Simple alert with two buttons and callbacks
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" message : @ "Messaggio" delegate : self cancelButtonTitle : @ "Annulla" otherButtonTitles : @ "Ok" , nil ] ; UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title" message: @ "Message" delegate: self cancelButtonTitle: @ "Cancel" otherButtonTitles: @ "OK", nil];
; myAlertA.tag = 1;
; [MyAlert show];
; [MyAlert release];

/ /
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" message : @ "Messaggio" delegate : self cancelButtonTitle : @ "Annulla" otherButtonTitles : @ "Ok" , nil ] ; UIAlertView myAlertB * = [[UIAlertView alloc] initWithTitle: @ "Title" message: @ "Message" delegate: self cancelButtonTitle: @ "Cancel" otherButtonTitles: @ "OK", nil];
; myAlertA.tag = 2;
; [MyAlertB show];
; [MyAlertB release];

/ / Multiple management UIAlertView

void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
"Alert Tag: %i" , alertView.tag ) ; NSLog (@ "Alert Tags:% i", alertView.tag);
"buttonIndex: %i" , buttonIndex ) ; NSLog (@ "buttonIndex:% i", buttonIndex);
}

Continued ...

iPhone Objective-C syntax equivalence

The syntax with square brackets in Objective-C is one of the reasons most frustration for those coming from other languages ​​(for objects). However, get used to it, you realize how readable the code becomes too with this mix of classical and more distinctly syntax Smalltalk. For example we see how the same procedure can be written in both syntax. Take the initialization of a UIAlertView :

1
2
3
4
5
6
7
8
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title"
"Messaggio" message: @ "Message"
delegate: self
"Annulla" cancelButtonTitle: @ "Cancel"
"Ok" , nil ] ; otherButtonTitles: @ "OK", nil];
; myAlert.tag = 1;
; [MyAlert show];
; [MyAlert release];

The setting of the tag potavamo it even so:

1
2
3
4
5
6
7
8
[ [ UIAlertView alloc ] initWithTitle : @ "Titolo" UIAlertView MyAlert * = [[UIAlertView alloc] initWithTitle: @ "Title"
"Messaggio" message: @ "Message"
delegate: self
"Annulla" cancelButtonTitle: @ "Cancel"
"Ok" , nil ] ; otherButtonTitles: @ "OK", nil];
1 ] ; [MyAlert SETTING: 1];
; [MyAlert show];
; [MyAlert release];

Same thing in reading during the event:

1
2
3
4
5
void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
alertView.tag == 1 ) { // ... if (alertView.tag == 1) {/ / ...
/ / Or
alertView tag ] == 1 ) { // ... if ([alertView tag] == 1) {/ / ...
}

Note: Not all properties or methods have this dual syntax. In the case of ' UIAlertView we have this dual functionality. For other objects, or in other cases, however, only one of the two might work ... but do not ask why (depending on how the developers have defined the interface).

Continued ...

Trace Actionscript, Objective-C NSLog ()

, usata per il debug delle applicazioni. Taking up the post from Actionscript to Objective-C (where you compare the code and syntax ActionScript and Objective-C), we have the convenient feature in ActionScript trace ( ) , used for debugging applications. This function outputs a console on the Adobe Flash development environment. It is mainly used for debugging and testing of "movie" / application. In XCode / Objective-C we have: NSLog ( ) . di Actionscript: The syntax of this function is very similar to the trace ( ) in Actionscript:

1
"Sono una linea di debug" ) ; NSLog (@ "I am debugging a line");

In Actionscript we have:

1
"Sono una linea di debug" ) ; trace ("I am debugging a line");

Apart from the use of the sign (@), as you can see, are identical. The differences (and similarities) start when you want to display values ​​of variables, for example in Actionscript we have:

1
2
3
"Coordinata x:" + x + " coordinata y:" + y ) ; trace ("x-coordinate:" + x + "y coordinate:" + y);
/ / Or
"Coordinate: " , x , y ) ; trace ("Coordinates", x, y);

In Objective-C we have:

1
"Coordinata x:%i coordinata y:%i" , x, y ) ; NSLog (@ "x-coordinate: the y coordinate%% i", x, y);

che opera sull'Apple System Log. Note: NSLog() actually calls the more generic function NSLogv ( ) that operates on the implementa System Log. The functions are in fact identical, changing only the input parameters.

o sprintf ( ) . The developers will find very familiar with the C string formatting, as with printf ( ) or sprintf ( ) . For details, see String Format Specifiers .

Continued ...

Very short trick: stop NSTimer

The use of timers ( NSTimer ) to develop applications for Apple iPhone is very frequent, not only in making games. One of the problems with which one can collide is the asynchronicity of the timer, which can lead, during the arrest of one or more timers, the crash of our application. , si invoca la invalidate che, appunto, arresta il timer. When you want to stop one or more timers, where repeats is set to YES , it invokes the invalidate who, precisely, the timer stops.

Continued ...

Apple iPhone 3.0 SDK: Fixed bug on UIButton buttonWithType

As indicated in the post Apple iPhone SDK 3.0: first incompatibility of the Apple SDK 3.0 show different behaviors with respect to release 2.2.1. Support service for developers gave me the answer, and then the solution to the problem. . The answer was that the technical support [UIButton buttonWithType:] already called - inside - the initWithFrame . It follows that, according to the technical support, writing:

Continued ...

From Actionscript to Objective-C

I thought it might be useful to those who have recently approached the development of applications for Apple iPhone, compare Adobe ActionScript - the language used in Adobe Flash and Adobe Flex, more common among the neo-programmers - and Objective-C language used by Apple to develop its applications. Objective-C is in effect an object-oriented language in the pure sense, not that it is not actionscript, but Objective-C is definitely a plus because it is an extension of ANSI C and its syntax is a mix between C / C + + and Smalltalk, is a true OO (Object-Oriented Language).

Continued ...