In this last period I worked on three applications for Apple iPhone, virtually simultaneously. Two have already been approved by Apple and are located on the App Store: thefirst and iMakeLove .
In both applications have adopted the use of an object UIWebView to display the instructions.
The object UIWebView ilevato was really comfortable in this case. It allowed me to quickly view and a nice text, images, and accompanied, if anything else is displayed on a Web page (Flash on iPhone not included).

The object UIWebView is really versatile and needs very few settings to be used. The only defect is the presence of a mysterious shadow that appears when you make a scroll out of the area of control is at the bottom than at the top:

After several searches I realized that this behavior depends on the SDK, especially the latest updates. And Apple has not released any official statement on how to remove it, on the contrary, he rejected as code is not allowed a series of hacks that make use of sensitive functions, or documenting. Eventually I gave up, both in terms of time, and because the solutions seemed all out of the box Apple.
Fortunately I got the solution, I hope fairly regular basis, which seems to solve the problem (thanks to Adolfo ):
1 2 3 4 5 6
| / / WebView is the control UIWebView [ webView.subviews objectAtIndex : 0 ] ; id = scroller [webView.subviews objectAtIndex: 0];
UIView * subView in [ scroller subviews ] ) for (UIView * subView in [scroller subviews]) subView class ] description ] isEqualToString : @ "UIImageView" ] ) if ([[[subView class] description] isEqualToString: @ "UIImageView"]) ; subView.hidden = YES; |
As pointed out by Adolfo , however this is a hack that may stop working with future releases of the SDK.
Continued ...
After explaining how to locate our strings in Xcode , it is easy to see now how - by applying the same technique - to locate and view images / interfaces created with Interface Builder.
Locate graphics resources
The process, as mentioned, is the same, if we have an image already included in our resources, or they insert a new one, and we want to "locate" - that is, to manage two or more images based on the languages supported - just click the button Right image ( Adium.png in this example) and select Get Info:

Make File Localizable we click in the lower left.

We 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 on the filesystem) under virtual folders English.lproj and Italian.lproj exactly as happened with the text:

contiene una stessa versione dell'immagine. At this point each of the folders English.lproj and Italian.lproj contains the 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 obtain a location of images "flash".
Locate the files XIB
Even the interfaces built with Interface Builder can be located 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 capabilities: add a ViewController (eg infoViewController ) XIB interface, we select it, choose Get Info from the contextual menu, give the file localizable, add the Italian localization:

Clicking Italiano or Italian will open Interface Builder! ) all'interno della classica cartella Classes . This time, the filesystem, you will notice that you have created two directories ( English.lproj and Italian.lproj ) within the classical Classes folder. Both will have their files infoViewController.xib . The comfort in this, clearly resolved in the code, and when we're going to instantiate our controller code we 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 trace of any statement concerning the location, totally managed by the system. The two interfaces, of course, can be completely different, as are in effect as two separate XIB file.
Continued ...
Execute a method after n seconds
The whole family performSelector is really interesting and can be useful in a multitude of cases. Its simplest and most common application is as follows:
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 there is required a "significant" temporal precision.
Retrieve the version of the
1 2
| version = [ [ [ NSBundle mainBundle ] infoDictionary ] objectForKey : @ "CFBundleVersion" ] ; NSString * version = [[[ NSBundle mainBundle] infoDictionary] objectForKey: @ "CFBundleVersion"]; "versione = %@" , version ) ; NSLog (@ "Version =% @", version); |
YES, true or TRUE?
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 are basically the same thing Edentia:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| / / Definition of 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 ...
Vibration
1 2 3
| # Import <AudioToolbox/AudioToolbox.h> / / ; AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); |
CGImageRef pointer from a UIImage
1 2 3 4
| [ UIImage imageNamed : @ "LittleHeart.png" ] ; UIImage * heart = [UIImage imageNamed: @ "LittleHeart.png"]; heart CGImage ] ; CGImageRef image = [CGImage heart]; / / 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); |
RGB converter 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 our method invoked by 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]; } |
Running time
Here is a simple way to calculate a short time needed to check the speed of execution of the code:
1 2 3 4
| ; CFAbsoluteTime initialTime CFAbsoluteTimeGetCurrent = (); / / ... tails ; CFAbsoluteTime finalTime CFAbsoluteTimeGetCurrent = (); "Tempo trascorso %f" , finalTime - initialTime ) ; NSLog (@ "elapsed time% f", finalTime - initialTime); |
Continued ...
non funzionano quando un UIButton è impostato in modalità UIButtonTypeCustom ! The states UIControlStateSelected or UIControlStateHighlighted not work when a UIButton mode is set UIButtonTypeCustom ! Or rather, do not work (because reserved for other types of button), for example to create a two-state button: toggle note. If we have created two images (and stato1.png stato2.png) for our button, we can proceed as follows:
1 2 3
| / / The header files that we create a global variable for usaremo / / Check the toggle were ToggleFlag BOOL; |
Now we create our button:
1 2 3 4 5 6 7 8 9
| / / We create a button and we place it initially in the state "stato1.png" / / Edit initWithFrame: (CGRect) {100,100,50,50} with the location and / / Size of your image has ; 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: @ "" Forst: UIControlStateNormal]; [ UIImage imageNamed : @ "stato1.png" ] forState : UIControlStateNormal ] ; [ToggleButton setBackgroundImage: [UIImage imageNamed: @ "stato1.png"] Forst: UIControlStateNormal]; self action : @selector ( onToggle : ) forControlEvents : UIControlEventTouchUpInside ] ; [ToggleButton addTarget: self action: @ selector (onToggle:) forControlEvents: UIControlEventTouchUpInside]; toggleButton ] ; [Self.view addSubview: ToggleButton]; |
When you click on the button will send a message to be managed onToggle :
1 2 3 4 5 6 7
| void ) onToggle : ( id ) sender { - (Void) onToggle: (id) sender { / / Retrieve pointer to UIButton ( UIButton * ) sender; UIButton ButtonClicked * = (UIButton *) sender; / / Execute Toogle toggleFlag =! toggleFlag; [ UIImage imageNamed : ( toggleFlag ) ? @ "stato1.png" : @ "stato2.png" ] forState : UIControlStateNormal ] ; [ButtonClicked setBackgroundImage: [UIImage imageNamed: (toggleFlag)? @ "Stato1.png" @ "stato2.png"] Forst: UIControlStateNormal]; } |
Continued ...
An alternative very simple to run a stream a mp3 file on the Apple iPhone could be:
Continued ...
1. Strings on multiple lines
In Xcode, you can "break" a string across multiple lines by inserting at the end with a backslash "\". This feature can be useful when, for example, we want to insert the HTML text in a control UIWebView :
Continued ...
The control UIDatePicker is perhaps the most beautiful present graphical control on the Apple iPhone. Besides the beauty of it is also easy to use, extremely versatile and used in many situations.
Continued ...
o CGPoint , ad esempio. The syntax NSLog(@"%@", ... ); works and is used to obtain information about objects, but does not work on C data types such as struct CGRect or CGPoint , for example. o NSStringFromCGPoint : To take advantage of NSLog(@"%@", ... ); even on C-style structs can rely 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, it is possible to improve this procedure writes of small useful macros like:
1
| # Define NSLogRect (rect) NSLog (@ "% s (% 0.0f,% 0.0f)% 0.0f% 0.0fx", # 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 ...
Application Icon
57 × 57 pixel icon that will represent our application is "impaired" by the Apple iPhone automatically: you add a rounded edge, a bright and 3D effects. This setting can be changed by selecting the file [nome applicazione]-Info.plist and adding the property "Icon includes gloss and bevel effects Already":
Continued ...
If you need to generate random numbers in an application must set aside Apple iPhone Objective-C, because it does not propose any class order. The solution is proposed that the C: rand(), srand(), random(), srandom() e arc4random() .
Continued ...
Latest Comments
Mark : @ Mark: Thanks for the answer. For example, if you have a code to save / use the folder ...
Mark : Thanks for the answer. For example, if you have a code to save / use the folder ...
Giovambattista Fazioli : @ Mark: in principle, absolutely, a lot depends on how and what to download. La. ..
Mark : Hello, good guide! I wanted to ask, you can use the same code to run a sql database? Instead of ...
Joseph : Hi I wanted to ask you how you can do in the post I uploaded a gallery of 50 images, for example, ...