Articles Tagged 'Apple iPhone'


10 useful snippets Objective-C

Move the double-tap on the simulator

The simulator iPhones / iPad Xcode allows you to simulate the double tap the Alt key is pressed. This is useful to simulate the function of Pinch, that used to enlarge or scroll Keep away content in the view with objects or UIWebView . Well, some of you have noticed that the simulation of the "two fingers" proceeds in a symmetrical manner always starting from the center of the screen. To move this "center" is also just hold down the SHIFT key.

Continued ...

Very short snippet: detect iPad on the device and on the Web

Not only browsers portatitli desktop or browsing the sites developed by us, now the era of mobile - the mobile browser - is widespread, thanks to devices like the Apple iPhone, Android, etc ... Furthermore, even for developers for the Apple iPhone has become mandatory to understand to which device the application is running.

On the Web, the situation is similar to that displayed in the Browser or Mobile Browser? , with the variant:

Continued ...

Private APIs in the removal of the scroll in a UIWebView object

In Very short snippet: Remove scroll from a UIWebView object we saw how it was possible to remove the scroll from an object UIWebView :

Continued ...

Objective-C: obtain information from Latitude and Longitude

I respond to Mirko asking me how to make the opposite case proposed in Getting Latitude and Longitude in Objective-C .
Starting from the values ​​of Latitude and Longitude you can use the class MKReverseGeocoder to obtain a range of information, such as: city, state, address in full, chap!

Continued ...

Very short snippet: Remove scroll from one object to UIWebView

Documented or undocumented, a feature, sometimes, you are forced to implement by force! Useful in some situations, the scroll is to remove an object from UIWebView . : Interestingly, in general, see how you can access the components 'internal' part of the object in question: in this example retrieves the object pointer UIScrollView present in the subject UIWebView :

Continued ...

How to get Latitude and Longitude in Objective-C

The framework provides MapKit many useful features, except the return of Longitude and Latitude 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 . Apple iPhone or iPad, however, you can overcome this obstacle by using a different Google services. Specifically, you can call directly to 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 , Google is the 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 ] ] ; [StringByAddingPercentEscapesUsingEncoding address: 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 "split" by the method componentsSeparatedByString , like the function explode ( ) in PHP for instance. I put the example I proposed - but commented - the code to retrieve even the Google accuracy parameter, 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 show on the map.


Download Source

I thank the team devAPP for the inspiration of this article.

Continued ...

setAnimationDidStopSelector: different uses and advanced

In most cases, or because we are used or because we have seen in tutorials and in some texts, we use the setAnimationDidStopSelector in this manner:

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 set self as delegate and through setAnimationDidStopSelector sends 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 UIView ! It may therefore be a custom class with our own messages and, as stated, easily callable from setAnimationDidStopSelector . In addition, the setAnimationDidStopSelector selectors agree 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, Atro is not a pointer to an instance of any object.

Continued ...

iPhone: Eliminate the shadow effect when scrolling a UIWebView

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 .

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

TheFirst

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:

UIWebView Shadow

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

Objective-C: expose properties in a class

I would like to show and discuss some examples on how to add and manipulate properties in Objective-C class. : A classic example, precisely, is the following, in the definition of our interface class defines two properties nome and cognome :

1
2
3
4
5
6
7
8
9
10
11
/ / MyClass.h
# Import <Foundation/Foundation.h>

NSObject { @ Interface MyClass: NSObject {
nome; NSString * name;
cognome; NSString * name;
}

retain ) NSString * nome; @ Property (retain) NSString * name;
retain ) NSString * cognome; @ Property (retain) NSString * name;
@ End

e setter usati rispettivamente per leggere ed impostare le nostre due proprietà: In the implementation file the statement insert @synthesize so that Xcode will produce for us the methods getter and setter , respectively, used to read and set our two properties:

1
2
3
4
5
6
7
8
/ / MyClass.m
# Import "MyClass.h"

@ Implementation MyClass

@ Synthesize name, surname;

@ End

, possiamo scrive: When you're going to use our class MyClass , that is 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" ] ; [SetNome myClass: @ "Undolog"];
"miaClasse.nome = %@" , [ miaClasse nome ] ) ; NSLog (@ "miaClasse.nome =% @", [myClass name]);

So far so good. However, it could mislead the equivalence of the "variable" 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 care to write the methods getter and setter . To further emphasize the differences, will rename the internal variables by inserting an underscore before the name. But we 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

Written as a class can be used exactly like 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" ] ; [SetNome myClass: @ "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 "their own" methods get and set , highlighting - even with the addition dell'underscore - the differences between the property name and its internal ivar _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 the functional level the personal use of the methods get and set allows real control of the data before its setting (or prior to its reading) 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 variation

If you want to use the internal variables with the underscore in front (who rpoviene Adobe Actionscript could be used as well) is not necessary to abandon the use of the directive @synthesize . Xcode makes it possible to "merge" the methods mentioned above:

1
2
_nome; @ Synthesize name = _Name;
_cognome; @ Synthesize name = _cognome;

. In doing so we could use the pointer to the 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 . Moreover, it is true that the use of @synthesize produces automatic generation methods (messages) of getter and setter , it is also true that if he does not find them, so if you want to "implement" a method for your getter and / or setter you can do this even if you used the directive @synthesize .

Memory allocations

In the examples above I have omitted some important details for a real implementation. First of all, I have not shown any method init() , useful for object initialization and default values. Moreover, there is 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 then see the details on the properties readonly , retain , etc ... :)

Continued ...

Browser or mobile browser?

Our website is now only displayed by PCs. With the spread of mobile, thanks to Apple's iPhone, to access the site or blog is increasingly being performed by a variety of mobile devices. It is therefore need to know how many Web Developer intercept and identify the different "agents", ie the means by which a user is viewing (browsing) our pages.

Continued ...