2012年2月29日 星期三

NSString 實體化 Objective-c class

    NSString* class = @"myViewController";
    Class detailClass = NSClassFromString(class);
    UIViewController* detailViewController = [[detailClass alloc] init];

2012年2月24日 星期五

監聽 UIKeyboard 事件

點擊 UITextField 時會出現鍵盤,想要在鍵盤上放 UIToolbar 並跟隨鍵盤上下滑動,可以使用以下方法:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];


建立2個監聽,當UIKeyboard出現或消失時都會通知selector裡的method

- (void)keyboardWillShow:(NSNotification *)notification 
{
    //透過 notification userInfo 可以得到UIKeyboard的資訊
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect kf = [aValue CGRectValue];    
    [self slideViewWithOptions:userInfo PosY:kf.size.height];
}

- (void)keyboardWillHide:(NSNotification *)notification 
{
    NSDictionary *userInfo = [notification userInfo];
    [self  slideViewWithOptions:userInfo PosY:0];
}

- (void)slideViewWithOptions:(NSDictionary *)options PosY:(CGFloat)PosY 
{    
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardEndFrame;
    //取得UIKeyboard滑動的動畫曲線、秒數與位置
    [[options objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[options objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[options objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
    
    int toolBarHeight = toolBar.frame.size.height;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:animationCurve];
    [UIView setAnimationDuration:animationDuration];
    CGRect frame = toolBar.frame;
    frame.origin.y = CGRectGetHeight(self.view.frame) - toolBarHeight - PosY;
    toolBar.frame = frame;
    [UIView commitAnimations];
}

UIWebview 清除 Cookie 暫存資料

使用 UIWebview 有遇到登入帳密時,其實就跟web登入一樣會自動暫存帳密的cookie
如果不想讓user可以自動登入,則必須清除暫存在iphone裡的cookie

清除暫存cookie方法:
NSHTTPCookieStorage *cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *loginCookies = [cookies cookiesForURL:[NSURL URLWithString:[your requestURL]]];
for (NSHTTPCookie* cookie in loginCookies) {
    [cookies deleteCookie:cookie];
}
return request;


參考資料:http://stackoverflow.com/questions/7567129/delete-cookies-uiwebview

2012年2月9日 星期四

ios 重繪等比例縮放照片

選取手機相簿裡尺寸較為大張的照片
且push或presentModalViewController時,
在滑動整個畫面的過程會出現非常不順的情形
原因在於圖片的size太大了
解決方法就是重繪照片並且resize就可以囉
- (void) resizeImage:(UIImage *)newImage 
{    
    float maxSizeWidth = 640;
    float maxSizeHeight = 640;
    
    if (newImage.size.width > maxSizeWidth || newImage.size.height > maxSizeHeight) 
    {
        float actualHeight = newImage.size.height;
        float actualWidth = newImage.size.width;
        
        float imgRatio = actualWidth/actualHeight;
        float maxRatio = maxSizeWidth/maxSizeHeight;
  
        if(imgRatio!=maxRatio)
        {
             if(imgRatio < maxRatio)
             {
                imgRatio = maxSizeHeight / actualHeight;
                actualWidth = imgRatio * actualWidth;
                actualHeight = maxSizeHeight;
             }
             else
             {
                imgRatio = maxSizeWidth / actualWidth;
                actualHeight = imgRatio * actualHeight;
                actualWidth = maxSizeWidth;
             }
         }        
         CGRect rect = CGRectMake(0, 0, (int)actualWidth, (int)actualHeight);
         UIGraphicsBeginImageContext(rect.size);
         [newImage drawInRect:rect];
         uploadImageView.image = UIGraphicsGetImageFromCurrentImageContext();
         UIGraphicsEndImageContext();
     }
}

Missing AccessibilitySettingsLoader bundle with iOS 5.0.1 debugging

在實機上測試時遇到的狀況

warning: Unable to read symbols for /Users/xjimi/Library/Developer/Xcode/iOS DeviceSupport/5.0.1 (9A405)/Symbols/System/Library/AccessibilityBundles/AccessibilitySettingsLoader.bundle/AccessibilitySettingsLoader (file not found).
warning: No copy of AccessibilitySettingsLoader.bundle/AccessibilitySettingsLoader found locally, reading from memory on remote device. This may slow down the debug session.

問題在於此目錄的底下沒有 AccessibilityBundles
/Users/[your login name]/Library/Developer/Xcode/iOS DeviceSupport/5.0.1 (9A405)/Symbols/System/Library/

所以只要到這個目錄把 AccessibilityBundles 複製到上述的目錄底下即可
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/System/Library/AccessibilityBundles