Articles Tagged 'iPhone'


iPhone: remove the shadow effect when scrolling in a UIWebView

In this last period I worked on three applications for Apple iPhone, almost simultaneously. Two have already been approved by Apple and can be found on the App Store: thefirst and iMakeLove .

TheFirst iMakeLove

In both applications have adopted the use of an object UIWebView to display instructions.
The object UIWebView ilevato it is really convenient in this case. It allowed me to quickly view and a nice text, accompanied, in the case of images, and anything else it can be viewed on a web page (Flash on iPhone not included).

TheFirst

The object UIWebView is really versatile and requires very few settings to be used. Only defect is the presence of a mysterious shadow that appears when you scroll outside the control of, or at the bottom than at the top:

UIWebView Shadow

After some research I realized that this behavior depends on the SDK, in particular the latest updates. In addition, Apple has not released any official statement on how to remove it, on the contrary, he rejected as code does not permit a series of hacks that make use of private functions or not documenting. Eventually I gave up, both for 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 this is still a hack that may stop working with future releases of the SDK.

Read more ...

How to locate images and views of Interface Builder

After explaining how to locate our strings in Xcode , let's see now how it is simple - applying the same technique - locate images and views / interfaces built with Interface Builder.

Find graphics resources

The process, as mentioned, is the same; if we have an image already inserted in our resources, or do we insert a new one, and we want to "localize" - that is managing two or more images based on the languages ​​supported - just click with the right button on the image ( Adium.png in this example) and select Get Info:

Make File Localizable we 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 on the file system) 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 within 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, select it, choose Get Info from the contextual menu, we make the localizable files, add localization in Italian:

ViewController

Clicking Inglese or English will open Interface Builder! ) all'interno della classica cartella Classes . This time, on 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, clearly resolved in the code; 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 trace 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.

Read more ...

10 useful snippets for Apple iPhone

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 a request is not "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 are basically the same thing edentica:

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 ...

Vibration

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);

UIColor to RGB converter

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

Exploiting the parameter userInfo you can send a pointer to our object to the method invoked by the 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'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);

Read more ...

Apple iPhone: create a custom toggle button

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 have 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 say 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: @ "" 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 to run 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;
/ / Executing the toogle
toggleFlag =! toggleFlag;
[ UIImage imageNamed : ( toggleFlag ) ? @ "stato1.png" : @ "stato2.png" ] forState : UIControlStateNormal ] ; [ButtonClicked setBackgroundImage: [UIImage imageNamed: (toggleFlag)? @ "Stato1.png" @ "stato2.png"] forState: UIControlStateNormal];
}

Read more ...

Very short snippet: streaming mp3 files on the Apple iPhone

An alternative really easy to run a stream a mp3 file on the Apple iPhone might be:

Read more ...

10 useful tips and snippets for Apple iPhone and Xcode

1. Strings on multiple lines

In Xcode, you can "break" a string on 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 :

Read more ...

Very short snippet: format a date to a UIDatePicker control

The control UIDatePicker is perhaps the most beautiful graphical control present on the Apple iPhone. Besides the beauty of it is also easy to use, versatile and used in many situations.

Read more ...

Objective-C: NSLog () on C struct

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 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, it is possible to improve this by being written procedures of small useful macros such 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)

Read more ...

Very short trick: 3 tricks for the Apple iPhone developer

Application Icon

The 57 × 57 pixel icon that will represent our application is "impaired" automatically dall'Apple iPhone: adds a rounded edge, a luminous effect and 3D. This setting can be changed by selecting the file [nome applicazione]-Info.plist and adding the property "Icon Already includes gloss and bevel effects":

Read more ...

Very short snippet: iPhone, random numbers

If you need to generate random numbers in an application Apple iPhone you have to put aside Objective-C, as it does not propose any class for that purpose. The solution is that proposed by the C: rand(), srand(), random(), srandom() e arc4random() .

Read more ...