10 Useful tricks and snippets for Apple iPhone and Xcode

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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
htmlData = @ " \ NSString * htmlData @ = "\
<html> <head> \
<style type= \" text/css \"> \
body {background: # 000; color: # fff} \
</ Style> \
<title> Report </ title> </ head> \
<body> \
Try <h1> HTML </ h1> \
<ul> \
<li> Text HTML </ li> \
Posted on <li> </ li> \
<li> more 'lines </ li> \
</ Ul> </ body> </ html> ";
htmlData baseURL : [ NSURL URLWithString : @ "http://www.saidmade.com/" ] ] ; [WebView loadHTMLString: htmlData baseURL: [ NSURL URLWithString: @ "http://www.saidmade.com/"]];

2. Transitions between View with effect "folded paper"

Here is a useful feature to browse and risflogliare, two UIView :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/ / Performs a transition with effect from CURL to a UIView UIView
/ / @ Params placeholder (UIView) main
/ / @ Params fview (UIView) of departure
/ / @ Params tview (UIView) arrival
/ / @ Params forward (BOOL) browse up or down

void ) makeTansitionFromView : ( UIWindow * ) placeholder fromView : ( UIView * ) fview toView : ( UIView * ) tview forward : ( BOOL ) forward { - (Void) makeTansitionFromView: (UIWindow *) placeholder fromView: (UIView *) fview toView: (UIView *) tview forward: (BOOL) {forward
nil context : NULL ] ; [UIView beginAnimations: nil context: NULL];
1.5 ] ; [UIView setAnimationDuration: 1.5];
forward?UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown forView : placeholder cache : YES ] ; [UIView setAnimationTransition: forward? UIViewAnimationTransitionCurlUp: UIViewAnimationTransitionCurlDown forView: placeholder cache: YES];
; [Tview removeFromSuperview];
fview ] ; [Placeholder addSubview: fview];
; [Fview removeFromSuperview];
tview ] ; [Placeholder addSubview: tview];
; [UIView commitAnimations];
}

It can be used in this way:

1
2
3
4
/ / Display - Scroll forward
window fromView : primaView.view toView : secondaView.view forward : YES ] ; [Self makeTansitionFromView: window fromView: primaView.view toView: secondaView.view forward: YES];
/ / Hide - flip through back
window fromView : secondaView.view toView : primaView.view forward : NO ] ; [Self makeTansitionFromView: window fromView: secondaView.view toView: primaView.view forward: NO];

3. Prevent auto dimming of the display

To be used with care, on pain of immediate consumption of the battery, this line of code makes it possible to keep our application always visible, preventing the Apple iPhone goes into "standby"

1
.idleTimerDisabled = YES ; [UIApplication sharedApplication]. IdleTimerDisabled = YES;

4. Network Activity Indicator

The animation that appears on the status bar at the top of the screen of the Apple iPhone, can also be used to highlight an application in our network:

1
2
3
4
5
6
7
/ / Show Network Activity Indicator ...
.networkActivityIndicatorVisible = YES ; [UIApplication sharedApplication]. NetworkActivityIndicatorVisible = YES;

/ / Your operations ...

/ / Hide Network Activity Indicator ...
.networkActivityIndicatorVisible = NO ; [UIApplication sharedApplication]. NetworkActivityIndicatorVisible = NO;

5. Personal Activity Indicator

If you do not have the status bar, can be useful to show one of our Activity Indicator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/ / Global somewhere ... Oedipus in a Delegate (. h)
* UIActivityIndicatorView activity;

/ / Initialize the viewDidLoad In loadview or Activity Indicator
UIActivityIndicatorView alloc ] initWithActivityIndicatorStyle : UIActivityIndicatorViewStyleWhite ] ; activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
activity ] ; [Self.view addSubview: activity];
CGPointMake ( 160 , 240 ) ] ; [SetCenter activity: CGPointMake (160, 240)];
; [StopAnimating activity];

/ / When we need
void ) someMethod { - (Void) {someMethod
; [StartAnimating activity];
/ / To doing ...
; [StopAnimating activity];
}

6. Location

With this snippet you can understand that "language" is set our device:

1
2
3
4
5
6
7
/ / Checking language and localization
defaults = [ NSUserDefaults standardUserDefaults ] ; NSUserDefaults * defaults = [ NSUserDefaults standardUserDefaults];
languages = [ defaults objectForKey : @ "AppleLanguages" ] ; NSArray * languages ​​= [defaults objectForKey: @ "AppleLanguages"];
currentLanguage = [ languages objectAtIndex : 0 ] ; NSString * currentLanguage = [languages ​​objectAtIndex: 0];

"Current Locale: %@" , [ [ NSLocale currentLocale ] localeIdentifier ] ) ; NSLog (@ "Current Locale:% @", [[ NSLocale currentLocale] LocaleIdentifier]);
"Current language: %@" , currentLanguage ) ; NSLog (@ "Current language:% @", currentLanguage);

7. iPhone or iPod?

This class is useful to determine which device Apple is running our application. Not only can we determine the model (iPhone or iPod Touch) but also the version. : Create two files UIDevice-hardware.h and UIDevice-hardware.m :

1
2
3
4
5
6
7
8
9
10
11
12
13
/ / UIDevice-hardware.h
# Import <UIKit/UIKit.h>

@ # Define IPHONE_1G_NAMESTRING "iPhone 1G"
@ # Define IPHONE_3G_NAMESTRING "iPhone 3G"
@ # Define IPHONE_3GS_NAMESTRING "iPhone 3G"
@ # Define IPOD_1G_NAMESTRING "iPod touch 1G"
@ # Define IPOD_2G_NAMESTRING "iPod Touch 2G"

Hardware ) @ Interface UIDevice (Hardware)
NSString * ) platform; - ( NSString *) platform;
NSString * ) platformString; - ( NSString *) platformString;
@ End
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
/ / UIDevice-hardware.m
# Import "UIDevice-hardware.h"
# Include <sys/types.h>
# Include <sys/sysctl.h>

Hardware ) @ Implementation UIDevice (Hardware)

/ *
Platforms
iPhone1, 1 = 1G iPhone
iPhone1, 2 = iPhone 3G
iPhone2, 1 = iPhone 3G
iPod1, 1 = iPod touch 1G
iPod2, 1 = iPod touch 2G
* /

NSString * ) platform { - ( NSString *) {platform
size_t size;
, NULL , & size, NULL , 0 ) ; sysctlbyname ("hw.machine", NULL, & size, NULL, 0);
machine = malloc ( size ) ; char * machine = malloc (size);
, machine, & size, NULL , 0 ) ; ysctlbyname ("hw.machine", machines, & size, NULL, 0);
platform = [ NSString stringWithCString : machine ] ; NSString * platform = [ NSString stringWithCString: machine];
machine ) ; free (machine);
return platform;
}

NSString * ) platformString { - ( NSString *) {platformString
platform = [ self platform ] ; NSString * platform = [self platform];
platform isEqualToString : @ "iPhone1,1" ] ) return IPHONE_1G_NAMESTRING; if ([platform isEqualToString: @ "iPhone1, 1"]) return IPHONE_1G_NAMESTRING;
platform isEqualToString : @ "iPhone1,2" ] ) return IPHONE_3G_NAMESTRING; if ([platform isEqualToString: @ "iPhone1, 2"]) return IPHONE_3G_NAMESTRING;
platform isEqualToString : @ "iPhone2,1" ] ) return IPHONE_3GS_NAMESTRING; if ([platform isEqualToString: @ "iPhone2, 1"]) return IPHONE_3GS_NAMESTRING;
platform isEqualToString : @ "iPod1,1" ] ) return IPOD_1G_NAMESTRING; if ([platform isEqualToString: @ "iPod1, 1"]) return IPOD_1G_NAMESTRING;
platform isEqualToString : @ "iPod2,1" ] ) return IPOD_2G_NAMESTRING; if ([platform isEqualToString: @ "iPod2, 1"]) return IPOD_2G_NAMESTRING;
; return NULL;
}
@ End

In our delegation to enter:

1
2
3
4
5
6
# Import "UIDevice-hardware.h"
/ /
void ) checkDeviceTypeAndCapabilities { - (Void) {checkDeviceTypeAndCapabilities
"type: %@" , [ [ UIDevice currentDevice ] platform ] ) ; NSLog (@ "type:% @", [[UIDevice currentDevice] platform]);
"type: %@" , [ [ UIDevice currentDevice ] platformString ] ) ; NSLog (@ "type:% @", [[UIDevice currentDevice] platformString]);
}

8. Room

When you write an application that uses access to the functions of the Chamber is to verify that the "device" supports these features (as per Apple guidelines):

1
2
3
4
5
6
* UIImagePickerController imgPicker;
/ / ...
UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypeCamera ] ) ) { if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])) {
self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imgPicker animated : YES ] ; [Self presentModalViewController: self.imgPicker animated: YES];
}

9. Photo Albums & Photo Library

Similar to what happens with the Chamber, it's best to just check out when you access the image library:

1
2
3
4
5
6
7
8
9
10
11
* UIImagePickerController imgPicker;
/ / ...
UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypeSavedPhotosAlbum ] ) ) { if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum])) {
self.imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
self.imgPicker animated : YES ] ; [Self presentModalViewController: self.imgPicker animated: YES];
}
/ / Or ...
UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypePhotoLibrary ] ) ) { if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])) {
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imgPicker animated : YES ] ; [Self presentModalViewController: self.imgPicker animated: YES];
}

10. Availability and type of network

Fortunately, Apple has written a class for us to determine the network status. This can be used in our application is to check that your network topology: Ethernet or WiFi. Download Reachability.h and Reachability.m . Add the Framework SystemConfiguration.framework . Here you can write a function like:

1
2
3
4
5
6
7
8
9
10
/ / In your Delegate. H
Reachability @ class;
/ /
/ / Delegates in. M
# Import "Reachability.h"

/ / Check the network coverage
BOOL ) CheckNetworkStatus { - (BOOL) {CheckNetworkStatus
Reachability reachabilityForInternetConnection ] retain ] != NotReachable ) ; return ([[Reachability reachabilityForInternetConnection] retain]! = NotReachable);
}

Please note that the code of Apple, which is available in full source Reachability.zip is for SDK 3.1.2. If you try to fill in for SDK 2.2.1 you need to comment on some pieces of code are not supported by the "old" (even if they still exist) SDK, such as define:

1
2
kSCNetworkReachabilityFlagsConnectionOnTraffic
kSCNetworkReachabilityFlagsConnectionOnDemand

and the piece of code (for comment):

1
2
3
4
5
6
7
8
9
10
11
12
/ / If ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand)! = 0) | |
/ / (Flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)! = 0))
/ / {
/ / / / ... and the connection is on-demand (or on-traffic) if the
/ / / / Calling application is using the APIs or higher CFSocketStream
/ /
/ / If ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
/ / {
/ / / / ... and no [user] intervention is needed
/ / RetVal = ReachableViaWiFi;
/ /}
/ /}

There are no comments for this post

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA