Common code list for iPhone Development

Source: Internet
Author: User

DiscussionIPhone DevelopmentCommon Code listing is the content to be introduced in this article.Iphone DevelopmentLet's talk about some common content. Let's take a look at the details first.

UIEdgeInsets sets the coordinates of tableView

 
 
  1. typedef struct UIEdgeInsets {  
  2.     CGFloat top, left, bottom, right;  // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'  
  3. } UIEdgeInsets; 

It contains the upper, left, bottom, and right surround length. When you drag down, if top 0, it will be displayed. If it is less than 0, it will be hidden.

Returns the length of a string.

 
 
  1. CGSize detailSize = [@ "your string" sizeWithFont: [UIFont systemFontOfSize: 15]
  2. ConstrainedToSize: CGSizeMake (200, MAXFLOAT) lineBreakMode: UILineBreakModeWordWrap];

The back key of navigationbar triggers other events.

 
 
  1. UIButton * back = [[UIButton alloc] initWithFrame: CGRectMake (200, 25, 63, 30)];
  2. [Back addTarget: self act
  3. Ion: @ selector (reloadRowData :) forControlEvents: UIControlEventTouchUpInside];
  4. [Back setImage: [UIImage imageNamed: @ ".png"] forState: UIControlStateNormal];
  5. UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] initWithCustomView: back];
  6. Self. navigationItem. leftBarButtonItem = loginButtonItem
  7. [Back release];
  8. [BackButtonItem release];

Prevent the screen from being dimmed

 
 
  1. [[UIApplication sharedApplication] setIdleTimerDisabled:YES]; 

Network Activity Status Indicator

This is a rotating icon displayed in the status bar on the upper left of the iPhone, indicating that a network activity occurs on the background.

 
 
  1. UIApplication* app = [UIApplication sharedApplication];  
  2. app.networkActivityIndicatorVisible = YES; 

Obtain UUID

 
 
  1. [[UIDevice currentDevice] uniqueIdentifier]  
  2.  
  3. UIDevice *myDevice = [UIDevice currentDevice];  
  4. NSString *deviceID = [myDevice uniqueIdentifier]; 

Screenshot

 
 
  1. UIGraphicsBeginImageContext (CGSizeMake (200,400); // create a bitmap-based image context and specify the size of CGSizeMake (200,400)
  2. [Self. view. layer renderInContext: UIGraphicsGetCurrentContext ()]; // renderInContext presents the receiver and Its subrange to the specified context.
  3. UIImage * aImage = UIGraphicsGetImageFromCurrentImageContext (); // returns an image based on the current image context.
  4. UIGraphicsEndImageContext (); // remove the graph context based on the current bitmap at the top of the stack.
  5. ImageData = UIImagePNGRepresentation (aImage); // return the data of the specified image in png format

Application border size

We should use "bounds" to obtain the application border. Instead of "applicationFrame ". "ApplicationFrame" also contains a 20-pixel status bar. Unless we need the extra 20-pixel status bar.

Vibrate and sound playback

 
 
  1. Sound will work in the Simulator, however some sound (such as looped) has been 
  2. reported as not working in Simulator or even altogether depending on the audio format.
  3.  Note there are specific filetypes that must be used (.wav in this example). 
 
 
  1. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  
  2. SystemSoundID pmph;  
  3. id sndpath = [NSBundle mainBundle] pathForResource:@"mySound"   ofType:@"wav"  inDirectory:@"/"];  
  4. CFURLRef baseURL = (CFURLRef) [NSURL alloc] initFileURLWithPath:sndpath];  
  5. AudioServicesCreateSystemSoundID (baseURL, &pmph);  
  6. AudioServicesPlaySystemSound(pmph);      
  7. [baseURL release]; 

Iphone obtains the local IP Address

 
 
  1. -(NSString *)getAddress {  
  2.     char iphone_ip[255];  
  3.     strcpy(iphone_ip,"127.0.0.1"); // if everything fails  
  4.     NSHost* myhost =[NSHost currentHost];  
  5.     if (myhost)  
  6.     {  
  7.         NSString *ad = [myhost address];  
  8.         if (ad)  
  9.             strcpy(iphone_ip,[ad cStringUsingEncoding:NSASCIIStringEncoding]);  
  10.     }  
  11.     return [NSString stringWithFormat:@"%s",iphone_ip];  

Summary:IPhone DevelopmentThe list of frequently-used code has been introduced. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.