iOS Tips for beginner

2013年7月16日

ネットサーフィングしながらかき集めたTIPS.
いつか誰かの役に立つはず。(自分含む)

  1. Open an URL in Safari
  2. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/"]];
    
  3. Hide the status bar
  4. [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
    
  5. Dial a Phone Number (iPhone Only)
  6. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://9662256888"]];
    
  7. Launch the Apple Mail
  8. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://mymail@myserver.com"]];
    
  9. stop responding to touch events
  10. [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    
  11. active the touch events
  12. [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    
  13. Show the network Activity Indicator
  14. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
  15. Hide the network Activity Indicator
  16. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
  17. Prevents iPhone goes into sleep mode
  18. [UIApplication sharedApplication].idleTimerDisabled = YES;
    [UIApplication sharedApplication].applicationIconBadgeNumber = 10;
    
  19. Display an alert window
  20. UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"too many alerts" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show]
    
  21. Get the path to the Documents folder
  22. NSArray*  paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDirectory = [paths objectAtIndex:0];
    
  23. Push another view controller onto the Navigation Bar
  24. [self.navigationController pushViewController:anotherVC animated:YES];
    
  25. Fade away a UIView by animating the alpha down to 0
  26. [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];  // fade away over 1 seconds
    [aView setAlpha:0]; 
    [UIView commitAnimations];
    
  27. Get the name of the app
  28. self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
    
  29. Change the status bar to black
  30. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
    Change the style of the navigation bar (from in a view controller):
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
  31. Save a NSString into NSUserDefaults
  32. NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:loginName forKey:kUserLoginName];
    Get an NSString from NSUserDefaults:
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSString* loginName = [defaults stringForKey:kUserLoginName];
    
  33. Check to make sure an objects support a method before calling it
  34. if ([item respondsToSelector:@selector(activateBOP:)]) {
        [item activateBOP:closeBOP];
    }
    
  35. Log the name of the class and function
  36. NSLog(@"%s", __PRETTY_FUNCTION__);
    Add rounded corners and/or a border around any UIView item (self)
    self.layer.borderColor  = [UIColor whiteColor].
    self.layer.cornerRadius = 8;     // rounded corners
    self.layer.masksToBounds = YES;  // prevent drawing outside border
    
  37. Open Google Maps app with directions between two lat/long points
  38. NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirflg=d", start.latitude, start.longitude, finish.latitude, finish.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; 
    
  39. Save bool to User Defaults
  40. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Yes Bool"];
    
  41. Copy a file from x to y
  42. [[NSFileManager defaultManager] copyItemAtPath:x toPath:y error:nil];
    1
    
    <li>Display a new view</li>
    1
    [self presentModalViewController:(UIViewController *) animated:YES];
    
  43. Screen touches method
  44. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}
    
  45. Get documents directory
  46. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
  47. Load URL
  48. [MyWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://couleeapps.hostei.com"]]];  
    
  49. Get Current Date and time
  50. NSCalendar *gregorian = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
    Own enum type:
    typedef enum {
        a = 0, b = 1, c = 2
    } enumName;
    
  51. Quartz draw arc
  52. CGContextRef ctxt = UIGraphicsGetCurrentContext();
    CGContextAddArc(ctxt, x, y, radius, startDeg, endDeg);
    
  53. Make the device vibrate
  54. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    
  55. Open the Messages app with a specific phone number
  56. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:123456789"]];
    
  57. Stop responding to touch events
  58. [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    
  59. Start responding again
  60. [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    
  61. single line of code browser
  62. [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [urlText stringValue]]]];
    
  63. Change the title on the back button on a UINavigationView. Use this code on the UINavigationController before pushing the view
  64. UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];
    self.navigationItem.backBarButtonItem = backBarButtonItem;
    [backBarButtonItem release];
    

    Have your say