2012年3月1日 星期四

iPhone/iPod Touch: Icon.png: icon dimensions (72 x 72) don't meet the size requirements. The icon file must be 57x57 pixels, in .png format

iPhone/iPod Touch: Icon.png: icon dimensions (72 x 72) don't meet the size requirements. The icon file must be 57x57 pixels, in .png format

Edit Project Settings -> Build -> uncheck Compress PNG Files,就可以了

2012年1月31日 星期二

取得keyboard 座標和寬高(CGRect)

- (void) keyboardWillShow:(NSNotification*) notification
{
    NSDictionary* info = [notification userInfo];
    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    NSLog(@"keyboardRect y:%f height:%f", keyboardRect.origin.y, keyboardRect.size.height);
}

2011年12月1日 星期四

iOS MD5編碼

#import <CommonCrypto/CommonDigest.h>

@implementation NSString(MD5)

- (NSString*)MD5
{
// Create pointer to the string as UTF8
  const char *ptr = [self UTF8String];

  // Create byte array of unsigned chars
  unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];

// Create 16 bytes MD5 hash value, store in buffer
  CC_MD5(ptr, strlen(ptr), md5Buffer);

// Convert unsigned char buffer to NSString of hex values
  NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) 
[output appendFormat:@"%02x",md5Buffer[i]];

  return output;
}

@end

iOS 建立UUID

#import "CreateUUID.h"

@implementation CreateUUID

+ (NSString *)createUUID
{
    // Create universally unique identifier (object)
    CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
    
    // Get the string representation of CFUUID object.
    NSString *uuidStr = [(NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject) autorelease];
    
    // If needed, here is how to get a representation in bytes, returned as a structure
    // typedef struct {
    //   UInt8 byte0;
    //   UInt8 byte1;
    //   ...
    //   UInt8 byte15;
    // } CFUUIDBytes;
    CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuidObject);
    
    CFRelease(uuidObject);
    
    return uuidStr;
}

@end

iOS ThreeDESBase64

#import "ThreeDESBase64.h"
#import <CommonCrypto/CommonCryptor.h>
#import "GTMBase64.h"

@implementation ThreeDESBase64

+ (NSString*)TripleDES:(NSString*)plainText encryptOrDecrypt:(CCOperation)encryptOrDecrypt key:(NSString*)key 
{    
    const void *vplainText;
    size_t plainTextBufferSize;
    
    if (encryptOrDecrypt == kCCDecrypt)
    {
        NSData *EncryptData = [GTMBase64 decodeData:[plainText dataUsingEncoding:NSUTF8StringEncoding]];
        plainTextBufferSize = [EncryptData length];
        vplainText = [EncryptData bytes];
    }
    else
    {
        NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
        plainTextBufferSize = [data length];
        vplainText = (const void *)[data bytes];
    }
    
    CCCryptorStatus ccStatus;
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;
    // uint8_t ivkCCBlockSize3DES;
    
    bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);
    // memset((void *) iv, 0x0, (size_t) sizeof(iv));
    
    //    NSString *key = @"123456789012345678901234";
    NSString *initVec = @"init Vec";
    const void *vkey = (const void *) [key UTF8String];
    const void *vinitVec = (const void *) [initVec UTF8String];
    
    ccStatus = CCCrypt(encryptOrDecrypt,
                       kCCAlgorithm3DES,
                       kCCOptionPKCS7Padding,
                       vkey, //"123456789012345678901234", //key
                       kCCKeySize3DES,
                       vinitVec, //"init Vec", //iv,
                       vplainText, //"Your Name", //plainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);
    //if (ccStatus == kCCSuccess) NSLog(@"SUCCESS");
    /*else if (ccStatus == kCC ParamError) return @"PARAM ERROR";
     else if (ccStatus == kCCBufferTooSmall) return @"BUFFER TOO SMALL";
     else if (ccStatus == kCCMemoryFailure) return @"MEMORY FAILURE";
     else if (ccStatus == kCCAlignmentError) return @"ALIGNMENT";
     else if (ccStatus == kCCDecodeError) return @"DECODE ERROR";
     else if (ccStatus == kCCUnimplemented) return @"UNIMPLEMENTED"; */
    
    NSString *result;
    
    if (encryptOrDecrypt == kCCDecrypt)
    {
        result = [[[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr 
                                                                length:(NSUInteger)movedBytes] 
                                        encoding:NSUTF8StringEncoding
                  autorelease];
    }
    else
    {
        NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
        result = [GTMBase64 stringByEncodingData:myData];
    }
    
    return result;    

如何在 XCode 4.2 設定部分程式碼不使用 ARC 方式分享

1. 選擇專案,此時會出現專案設定畫面。
2. 選擇你的 Target,並切換到 Build Phases 畫面。
3. 找到 Compile Sources 這個畫面,然後選擇你要設定不使用 ARC 的程式碼
4. 按下 Enter 鍵後,會跳出一個視窗要你輸入東西,在裏面輸入 -fno-objc-arc 就可以了。

2011年8月16日 星期二

UITableView 新增下拉更新(Pull-refresh)


https://github.com/enormego/EGOTableViewPullRefresh

一、找到你的UITableView / UITableViewController 的.h 文件,結合以下代碼,添加相應的元素:

#import "EGORefreshTableHeaderView.h"

@interface RootViewController : UITableViewController  {

EGORefreshTableHeaderView *_refreshHeaderView;

//  Reloading var should really be your tableviews datasource
//  Putting it here for demo purposes
BOOL _reloading;
}

- (void)reloadTableViewDataSource;
- (void)doneLoadingTableViewData;
@end


二、切換到你的UITableView / UITableViewController 的.m 文件,結合以下代碼,添加相應的元素:

- (void)viewDidLoad {
[super viewDidLoad];

if (_refreshHeaderView == nil) {

EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
view.delegate = self;
[self.tableView addSubview:view];
_refreshHeaderView = view;
[view release];

}

//  update the last update date
[_refreshHeaderView refreshLastUpdatedDate];
}

#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{

//  should be calling your tableviews data source model to reload
//  put here just for demo
_reloading = YES;

}

- (void)doneLoadingTableViewData{

//  model should call this when its done loading
_reloading = NO;
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];

}

#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];



}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];

}

#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{

[self reloadTableViewDataSource];
[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];

}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{

return _reloading; // should return if data source model is reloading

}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{

return [NSDate date]; // should return date data source was last changed

}


三、最後呢,為了彰顯你良好的內存管理習慣,別忘了釋放掉相應的UI 元素:

- (void)viewDidUnload {
_refreshHeaderView=nil;
}

- (void)dealloc {

_refreshHeaderView = nil;
[super dealloc];
}

四、編譯之前,別忘了將EGORefreshTableHeaderView.h、EGORefreshTableHeaderView.m兩個文件,以及Enormego提供的那一套圖片包拖進你的工程裡。

五、很抱歉沒有第五步了,編譯你的工程,打開simulator ,看看效果吧。包你滿意。
其實還沒完,這裡補充一點花絮。關於這個Pull & Refresh 功能的完整版來龍去脈。其實呢,最早出現這個Pull & Refresh 功能的app 是Tweetie 2 ,也就是現在大家每天都在用的Twitter for iPhone 的前身。因為這個小功能實在是很好用很貼心,所以就被Enormego 團隊發現了。但是Tweetie 2 是閉源的,於是很牛逼很強大的Enormego 就自己寫了一個,並且很大方的放到GitHub 上開源了。但是諸位如果仔細回憶一下,不難發現,真正讓這個Pull & Refresh 走紅的,是Facebook 旗下的Three20 開源框架。