10 useful tips and snippets for Apple iPhone and Xcode
Thursday, November 12, 20091. 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
- <html> <head> \
- <style type= \" text/css \"> \
- body (background: # 000; color: # fff) \
- </ style> \
- <title> Report </ title> </ head> \
- <body> \
- <h1> HTML Test </ h1> \
- <ul> \
- <li> Text HTML </ li> \
- Posted on <li> </ li> \
- <li> more 'rows </ li> \
- </ ul> </ body> </ html> ";
2. Transitions View with effect "folded paper"
Here's a useful feature to browse and risflogliare two UIView
- / / Perform a transition effect from a CURL UIView a UIView
- / / @ Params placeholder (UIView) main
- / / @ Params fview (UIView) starting
- / / @ Params TView (UIView) arrival
- / / @ Params forward (BOOL) puff up or down
- ) 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];
- )
Can be used in this way:
- / / Display - Browse Forward
- window fromView : primaView.view toView : secondaView.view forward : YES ] ; [Self makeTansitionFromView: window fromView: primaView.view toView: secondaView.view forward: YES];
- / / Hide - backward
- 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":
- .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:
- / / Show Network Activity Indicator ...
- .networkActivityIndicatorVisible = YES ; [UIApplication sharedApplication]. NetworkActivityIndicatorVisible = YES;
- / / Your operations ...
- / / Hide Network Activity Indicator ...
- .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:
- / / Global somewhere ... dipo a Delegate (. h)
- UIActivityIndicatorView * activity;
- / / In loadView or viewDidLoad initialize the Activity Indicator
- initWithActivityIndicatorStyle : UIActivityIndicatorViewStyleWhite ] ; activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
- activity ] ; [Self.view addSubview: activity];
- CGPointMake ( 160, 240 ) ] ; [Activity setCenter: CGPointMake (160, 240)];
- ; [StopAnimating activity];
- / / When we need
- ) someMethod { - (Void) (someMethod
- ; [StartAnimating activity];
- / / To doing ...
- ; [StopAnimating activity];
- )
6. Location
With this snippet you can understand "language" and set our device:
- / / 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];
- , [ [ NSLocale currentLocale ] localeIdentifier ] ) ; NSLog (@ "Current Locale:% @", [[NSLocale currentLocale] LocaleIdentifier]);
- , 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
- / / UIDevice-hardware.h
- # import <UIKit/UIKit.h>
- # define IPHONE_1G_NAMESTRING @ "iPhone 1G"
- # define IPHONE_3G_NAMESTRING @ "iPhone 3G"
- # define IPHONE_3GS_NAMESTRING @ "iPhone 3GS"
- # define IPOD_1G_NAMESTRING @ "iPod touch 1G"
- # define IPOD_2G_NAMESTRING @ "iPod touch 2G"
- Hardware ) @ interface UIDevice (Hardware)
- * ) platform; - (NSString *) platform;
- * ) platformString; - (NSString *) platformString;
- @ end
- / / UIDevice-hardware.m
- # import "UIDevice-hardware.h"
- # include <sys/types.h>
- # include <sys/sysctl.h>
- Hardware ) @ implementation UIDevice (Hardware)
- / *
- Platforms
- iPhone1, 1 = iPhone 1G
- iPhone1, 2 = iPhone 3G
- iPhone2, 1 = iPhone 3GS
- iPod1, 1 = iPod touch 1G
- iPod2, 1 = iPod touch 2G
- * /
- size_t size;
- , NULL , & size, NULL , 0 ) ; sysctlbyname (hw.machine ", NULL, & size, NULL, 0);
- , machine, & size, NULL , 0 ) ; ysctlbyname (hw.machine ", machine, & size, NULL, 0);
- return platform;
- )
- * ) platformString { - (NSString *) (platformString
- 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 delegates can enter:
- # import "UIDevice-hardware.h"
- / /
- ) checkDeviceTypeAndCapabilities { - (Void) (checkDeviceTypeAndCapabilities
- , [ [ UIDevice currentDevice ] platform ] ) ; NSLog (@ "type:% @", [[UIDevice currentDevice] platform]);
- , [ [ UIDevice currentDevice ] platformString ] ) ; NSLog (@ "type:% @", [[UIDevice currentDevice] platformString]);
- )
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):
- 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 Album & Photo Library
Similarly to what happens with the Chamber, it should just make the checks even if you access the library of images:
- 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 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:
- / / In your Delegate. H
- @ class Reachability;
- / /
- / / In the delegate. M
- # import "Reachability.h"
- / / Verify Network Coverage
- ) CheckNetworkStatus { - (BOOL) (CheckNetworkStatus
- Reachability reachabilityForInternetConnection ] retain ] != NotReachable ) ; return ([[Reachability reachabilityForInternetConnection] retain]! = NotReachable);
- )
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:
- kSCNetworkReachabilityFlagsConnectionOnTraffic
- kSCNetworkReachabilityFlagsConnectionOnDemand
and the part of code (comments):
- / / 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