Skip to content

Instantly share code, notes, and snippets.

View eiffelqiu's full-sized avatar

Eiffel.GM eiffelqiu

  • Beijing, P.R.China
View GitHub Profile
@eiffelqiu
eiffelqiu / MailComposeViewController.m
Created May 25, 2011 01:10
MFMailComposeViewController with availability check
-(void)sendMailByTitle:(BOOL)title andText:(NSString*)text byViewController:(UIViewController*)vCtr{
self.strText=text;
self.strTitle=title;
vRef=vCtr;
[self emailThisNote];
}
-(void)emailThisNote{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil) {
@eiffelqiu
eiffelqiu / get_random_number.m
Created May 25, 2011 00:49
Get random number in a range
-(int)getRandomNumber:(int)from to:(int)to {
return (int)from + arc4random() % (to-from+1);
}
int randomNumber = [self getRandomNumber:9 to:99];
@eiffelqiu
eiffelqiu / Reachability.m
Created May 25, 2011 00:44
Checking network availability on iOS
#include <netinet/in.h>
#import <SystemConfiguration/SCNetworkReachability.h>
- (BOOL)connectedToNetwork {
// Create zero addy
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
@eiffelqiu
eiffelqiu / GPSViewController.h
Created May 25, 2011 00:38
Get GPS From using CLLocationManager
// view GPSViewController.h file
// Step 1: add CoreLocation framework in Project
// Step 2: include the CoreLocation.h file
// implement the following codes
#import <CoreLocation/CoreLocation.h>
@interface GPSViewController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@eiffelqiu
eiffelqiu / Round_Corner_Image.m
Created May 25, 2011 00:27
How to make an image with rounded corners
void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight);
{
float fw, fh;
if (ovalWidth == 0 || ovalHeight == 0) {
CGContextAddRect(context, rect);
return;
}
CGContextSaveGState(context);
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextScaleCTM (context, ovalWidth, ovalHeight);
@eiffelqiu
eiffelqiu / Macro.pch
Created May 24, 2011 10:07
Common Macro for iphone/ipad project
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#define HEXCOLOR(c) [UIColor colorWithRed:((c>>24)&0xFF)/255.0 \
green:((c>>16)&0xFF)/255.0 \
blue:((c>>8)&0xFF)/255.0 \
alpha:((c)&0xFF)/255.0];
#endif
static inline BOOL isEmpty(id thing) {
@eiffelqiu
eiffelqiu / ValidateEmail.m
Created May 24, 2011 09:59
Validating email address without using RegexKitLite
+ (BOOL)validateEmail:(NSString *)email {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
@eiffelqiu
eiffelqiu / NSString-truncateToSize.h
Created May 24, 2011 06:57
NSString truncation to given size
//
// NSString-truncateToSize
// Fast Fonts
//
// Created by Stuart Shelton on 28/03/2010.
// Copyright 2010 Stuart Shelton.
//
// NSString truncate function for Objective C / iPhone SDK by
// Stuart Shelton is licensed under a Creative Commons Attribution 3.0
// Unported License (CC BY 3.0)
// Load html from local file
NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"localpage" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];
//
// NSArray+BinarySearch.h
// BinarySearch
//
// Created by Ole Begemann on 19.04.10.
// Copyright 2010 Ole Begemann. All rights reserved.
//
#import <Foundation/Foundation.h>