2012年8月31日 星期五

[iOS] 發送 Local Notification 方法小記

最近剛研究完 Apple Push Notification Service (APNS) 來發送訊息給 user device。
有空再來整理整個設定流程,今天同事討論到也有直接本機發送的方式,上網找了一下寫個函式記錄以方便使用。

- (void)pushLocalNotification:(NSDate *)date
{
    //直接抓取目前時間後1分鐘

    //NSDate *after = [NSDate dateWithTimeIntervalSinceNow:(1 * 60)];

    //若有舊訊息則把它刪除
    NSArray *oldNotificatoins = [application scheduledLocalNotifications];
    if([oldNotificatoins count] > 0)
        [application cancelAllLocalNotifications];

    //建立一個訊息來發送
    UILocalNotification *alarm = [[[UILocalNotification alloc] init] autorelease];
    if(alarm)
    {
        alarm.fireDate = date;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = @"beep-beep.caf";
        alarm.alertBody = @"test local notification message!";
        [application scheduleLocalNotification:alarm];
    }

}

2012年8月30日 星期四

[iOS] 將輸入框 (UITextField) 收起的方法

每次使用 UITextField 來讓使用者輸入文字,但在收起來這件事總是不少麻煩。
記錄一下幾個收起的方法:

1.實作 UITextFieldDelegate 的函式在按下確定 (Return、Done) 後收起

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}


2.手指點擊畫面後收起

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackgroundTap:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];

- (void)handleBackgroundTap:(UITapGestureRecognizer *)sender
{
    [textField resignFirstResponder];
}

2012年8月28日 星期二

[iOS] 內建 UIAlertView 顯示輸入框的方法


常常使用 UIAlertView 來顯示一些告知使用者訊息的功能,但在登入或需要使用者輸入一些欄位時(ex:密碼)可用以下的方法來呈現:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
UITextField *textField = [alertView textFieldAtIndex:0];
[alertView show];

紅色字的部份可參考以下的列舉使用。

typedef enum {
    UIAlertViewStyleDefault = 0,
    UIAlertViewStyleSecureTextInput,
    UIAlertViewStylePlainTextInput,
    UIAlertViewStyleLoginAndPasswordInput
} UIAlertViewStyle;

2012年8月23日 星期四

[iOS] 判斷 app 是第一次打開或更新版本後第一次打開的方法

最近在找怎麼判斷 app 是第一次打開或更新版本後第一次打開的方法,這個可用來製作首次操作教學的提示,網路(參考連結)上有好心人教學,程式碼如下:


- (BOOL)isFirstLoad
{
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *lastRunVersion = [defaults objectForKey:LAST_RUN_VERSION_KEY];
    
    if(!lastRunVersion)
    {
        [defaults setObject:currentVersion forKey:LAST_RUN_VERSION_KEY];
        NSLog(@"第一次打開 App");
        
        return YES;
    }
    else if (![lastRunVersion isEqualToString:currentVersion])
    {
        [defaults setObject:currentVersion forKey:LAST_RUN_VERSION_KEY];
        NSLog(@"更新版本後第一次打開 App");
        
        return YES;
    }
    
    //記得一定要呼叫此函式才會永久儲存
    [defaults synchronize];
    
    return NO;
}

2012年8月16日 星期四

[iOS] 使用 UIActionSheet 時,取消按鈕感應範圍小記

在使用 UIActionSheet 時,發現取消按鈕的感應範圍整個偏上一點。
拜讀 google 大神後,從 連結 中找到了解答,原來是有使用 UITabBarController 的緣故,因此要改用 [UIApplication sharedApplication].keyWindow 來取代 self.view 即可。

[Mac] Firefox 使用內建字典外掛

在 safari 有內建功能可以快速打開字典查詢,沒想到 firefox 無此功能,網路上找到一個外掛使用後就可以啦!
就叫 Look Up in Dictionary

[Mac] Lion 下使用網路郵局之 java 設定

最近開通了網路郵局後,發現用 safari 打開網頁後都出現電腦尚未安裝 java 的怪現象。
我明明就有裝了說,結果發現原來要打開外掛才行,然後重開 safari 就大功告成。
來個 java 測試網頁

[iOS] 設定多國語系無法正常顯示小記

這陣子忙著寫第一個 iPhone 版本的 app,終於要上架了!(撒花)
雖然一開始鎖定繁中語系,不過 app 的主要市場在北美,
因此就要做個多國語系啦,在設定時,很多教學都提到直接使用 NSLocalizedString 函式即可。但我發現一直無法正常顯示,結果最近在一篇文章中提到,NSLocalizedString 函式預設是依照 Localizable.strings 檔案去設定,但若檔名有自行更改...就不會理你了。
因此,若自行更改檔名,就要改用 NSLocalizedStringFromTable(@”title”,@”檔名”, nil)  函式來解決,打完收工。