Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
codeswimmer / ios_draw_path_in_custom_uiview.txt
Created January 7, 2012 21:56
iOS: Drawing a path in a custom UIView
/* Drawing a path in a custom UIView
-(void)drawRect:(CGRect)rect
{
CGContextRect context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextMoveToPoint(context, 75, 10);
CGContextAddLineToPoint(context, 160, 150);
CGContextAddLineToPoint(context, 10, 150);
@codeswimmer
codeswimmer / ios_random_value.txt
Created January 7, 2012 21:59
iOS: Using arc4random() to get a floating point value between 0 and a given maximum value
/*
Using arc4random() to get a floating point value between 0 and a given maximum value. Note that this produces a value that is double the precision of using rand().
*/
-(double)randomValueFromZero:(double)toValue
{
return double val = floorf(((double)arc4random() / toValue) * 100.0f);
}
@codeswimmer
codeswimmer / ios_logAllKeys.m
Created January 10, 2012 13:25
iOS: Log all keys and values from a NSDictionary
-(void)logAllKeys:(NSDictionary *)userInfo
{
NSArray *allKeys = [userInfo allKeys];
for (NSString *key in allKeys) {
id value = [userInfo valueForKey:key];
NSString *valueString = @"";
if ([value isKindOfClass:[NSString class]])
valueString = (NSString *)value;
else if ([value isKindOfClass:[NSNumber class]])
@codeswimmer
codeswimmer / ios_getMacAddress.m
Created January 15, 2012 13:23
iOS: Determine the MAC address of a device
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
// From http://iphonedevelopertips.com/device/determine-mac-address.html
- (NSString *)getMacAddress
{
int mgmtInfoBase[6];
@codeswimmer
codeswimmer / ios_IPAddress.c
Created January 15, 2012 16:49
iOS: Get Current IP Address
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
@codeswimmer
codeswimmer / ios_is_view_on_screen.txt
Created January 22, 2012 19:03
iOS: How to determine if your view is currently on screen
From UIView: if (self.window)… // I'm on screen
From UIViewController: if (self.view.window)…
@codeswimmer
codeswimmer / ios_how_big_is_screen.txt
Created January 22, 2012 19:14
iOS: How big is the current screen, mofo
CGRect screenBounds = [[UIScreen mainScreen] bounds]; // In points
@codeswimmer
codeswimmer / ios_date_from_string.h
Created January 23, 2012 03:11
iOS: Create a NSDate from NSString
-(NSDate *)dateFromString:(NSString *)dateString;
@codeswimmer
codeswimmer / ios_convenience_initializer.txt
Created January 23, 2012 16:47
iOS: Convenience Initializer
-(id)initWithSomething:(id)somethingIn
{
self = [self init];
self.something = somethingIn;
return self;
}
@codeswimmer
codeswimmer / iso_conforms_to_protocol.txt
Created January 23, 2012 20:11
iOS: How to determine if an object conforms to a specific protocol
if ([someObject conformsToProtocol:@protocol(NSCopying)]) …