10 useful tips and snippets for Apple iPhone and Xcode

Thursday, November 12, 2009

1. Strings on multiple lines

In Xcode you can "break" a string on multiple lines by inserting at the end a backslash. This feature can be useful when, for example, we want to insert HTML text in a control UIWebView

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

2. Transitions View with effect "folded paper"

Here's a useful feature to browse and risflogliare two UIView

Objective-C:
  1. / / Perform a transition effect from a CURL UIView a UIView
  2. / / @ Params placeholder (UIView) main
  3. / / @ Params fview (UIView) starting
  4. / / @ Params TView (UIView) arrival
  5. / / @ Params forward (BOOL) puff up or down
  6. ) 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 (
  7. nil context : NULL ] ; [UIView beginAnimations: nil context: NULL];
  8. 1.5 ] ; [UIView setAnimationDuration: 1.5];
  9. forward?UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown forView : placeholder cache : YES ] ; [UIView setAnimationTransition: forward? UIViewAnimationTransitionCurlUp: UIViewAnimationTransitionCurlDown forView: placeholder cache: YES];
  10. ; [TView removeFromSuperview];
  11. fview ] ; [Placeholder addSubview: fview];
  12. ; [Fview removeFromSuperview];
  13. tview ] ; [Placeholder addSubview: TView];
  14. ; [UIView commitAnimations];
  15. )

Can be used in this way:

Objective-C:
  1. / / Display - Browse Forward
  2. window fromView : primaView.view toView : secondaView.view forward : YES ] ; [Self makeTansitionFromView: window fromView: primaView.view toView: secondaView.view forward: YES];
  3. / / Hide - backward
  4. window fromView : secondaView.view toView : primaView.view forward : NO ] ; [Self makeTansitionFromView: window fromView: secondaView.view toView: primaView.view forward: NO];

3. Prevent auto dimming display

To be used with care, on pain of immediate consumption of the battery, this line of code allows you to keep our application always visible, preventing the Apple iPhone goes into "stand-by":

Objective-C:
  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 in our application to emphasize networking activity:

Objective-C:
  1. / / Show Network Activity Indicator ...
  2. .networkActivityIndicatorVisible = YES ; [UIApplication sharedApplication]. NetworkActivityIndicatorVisible = YES;
  3. / / Your operations ...
  4. / / Hide Network Activity Indicator ...
  5. .networkActivityIndicatorVisible = NO ; [UIApplication sharedApplication]. NetworkActivityIndicatorVisible = NO;

5. Personnel Activity Indicator

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

Objective-C:
  1. / / Global somewhere ... dipo a Delegate (. h)
  2. UIActivityIndicatorView * activity;
  3. / / In loadView or viewDidLoad initialize the Activity Indicator
  4. initWithActivityIndicatorStyle : UIActivityIndicatorViewStyleWhite ] ; activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
  5. activity ] ; [Self.view addSubview: activity];
  6. CGPointMake ( 160, 240 ) ] ; [Activity setCenter: CGPointMake (160, 240)];
  7. ; [StopAnimating activity];
  8. / / When we need
  9. ) someMethod { - (Void) (someMethod
  10. ; [StartAnimating activity];
  11. / / To doing ...
  12. ; [StopAnimating activity];
  13. )

6. Location

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

Objective-C:
  1. / / Checking language and localization
  2. defaults = [ NSUserDefaults standardUserDefaults ] ; NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
  3. languages = [ defaults objectForKey : @ "AppleLanguages" ] ; NSArray * languages = [defaults objectForKey: @ "AppleLanguages"];
  4. currentLanguage = [ languages objectAtIndex : 0 ] ; NSString * currentLanguage = [languages objectAtIndex: 0];
  5. , [ [ NSLocale currentLocale ] localeIdentifier ] ) ; NSLog (@ "Current Locale:% @", [[NSLocale currentLocale] LocaleIdentifier]);
  6. , currentLanguage ) ; NSLog (@ "Current language:% @", currentLanguage);

7. iPhone or iPod?

This class is useful to determine which device Apple is working on 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

Objective-C:
  1. / / UIDevice-hardware.h
  2. # import <UIKit/UIKit.h>
  3. # define IPHONE_1G_NAMESTRING @ "iPhone 1G"
  4. # define IPHONE_3G_NAMESTRING @ "iPhone 3G"
  5. # define IPHONE_3GS_NAMESTRING @ "iPhone 3GS"
  6. # define IPOD_1G_NAMESTRING @ "iPod touch 1G"
  7. # define IPOD_2G_NAMESTRING @ "iPod touch 2G"
  8. Hardware ) @ interface UIDevice (Hardware)
  9. * ) platform; - (NSString *) platform;
  10. * ) platformString; - (NSString *) platformString;
  11. @ end

Objective-C:
  1. / / UIDevice-hardware.m
  2. # import "UIDevice-hardware.h"
  3. # include <sys/types.h>
  4. # include <sys/sysctl.h>
  5. Hardware ) @ implementation UIDevice (Hardware)
  6. / *
  7. Platforms
  8. iPhone1, 1 = iPhone 1G
  9. iPhone1, 2 = iPhone 3G
  10. iPhone2, 1 = iPhone 3GS
  11. iPod1, 1 = iPod touch 1G
  12. iPod2, 1 = iPod touch 2G
  13. * /
  14. * ) platform { - (NSString *) platform (
  15. size_t size;
  16. , NULL , & size, NULL , 0 ) ; sysctlbyname (hw.machine ", NULL, & size, NULL, 0);
  17. machine = malloc ( size ) ; char * machine = malloc (size);
  18. , machine, & size, NULL , 0 ) ; ysctlbyname (hw.machine ", machine, & size, NULL, 0);
  19. platform = [ NSString stringWithCString : machine ] ; NSString * platform = [NSString stringWithCString: machine];
  20. machine ) ; free (machine);
  21. return platform;
  22. )
  23. * ) platformString { - (NSString *) (platformString
  24. platform = [ self platform ] ; NSString * platform = [self platform];
  25. platform isEqualToString : @ "iPhone1,1" ] ) return IPHONE_1G_NAMESTRING; if ([platform isEqualToString: @ "iPhone1, 1"]) return IPHONE_1G_NAMESTRING;
  26. platform isEqualToString : @ "iPhone1,2" ] ) return IPHONE_3G_NAMESTRING; if ([platform isEqualToString: @ "iPhone1, 2"]) return IPHONE_3G_NAMESTRING;
  27. platform isEqualToString : @ "iPhone2,1" ] ) return IPHONE_3GS_NAMESTRING; if ([platform isEqualToString: @ "iPhone2, 1"]) return IPHONE_3GS_NAMESTRING;
  28. platform isEqualToString : @ "iPod1,1" ] ) return IPOD_1G_NAMESTRING; if ([platform isEqualToString: @ "iPod1, 1"]) return IPOD_1G_NAMESTRING;
  29. platform isEqualToString : @ "iPod2,1" ] ) return IPOD_2G_NAMESTRING; if ([platform isEqualToString: @ "iPod2, 1"]) return IPOD_2G_NAMESTRING;
  30. return NULL;
  31. )
  32. @ end

In our delegates can enter:

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

8. Room

When you write an application that uses the access to the functions of the Chamber is good to verify that the "device" supports such features (such as directives from Apple):

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

9. Photo Album & Photo Library

Similarly to what happens with the Chamber, it should just make the checks even if you access the library of images:

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

10. Availability and type of network

Fortunately, Apple wrote a class for us to determine the status of the network. This can be used in our application is to verify the presence of its network topology: Ethernet or WiFi. Download Reachability.h and Reachability.m. Add the Framework SystemConfiguration.framework Now you can write a function like this:

Objective-C:
  1. / / In your Delegate. H
  2. @ class Reachability;
  3. / /
  4. / / In the delegate. M
  5. # import "Reachability.h"
  6. / / Verify Network Coverage
  7. ) CheckNetworkStatus { - (BOOL) (CheckNetworkStatus
  8. Reachability reachabilityForInternetConnection ] retain ] != NotReachable ) ; return ([[Reachability reachabilityForInternetConnection] retain]! = NotReachable);
  9. )

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

Objective-C:
  1. kSCNetworkReachabilityFlagsConnectionOnTraffic
  2. kSCNetworkReachabilityFlagsConnectionOnDemand

and the part of code (comments):

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

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

There are no comments for this post

Leave a comment

TAG XHTML PERMISSIONS: <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 [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL