2014年1月28日 星期二

用程式取得plist中的值

//在plist中有二種顯示法,一種是給開發者看的,另一種是程式真正的key
//在這裡我們只要用程式真正的key去取值,就可以找到想要的資訊。

NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

NSString *name = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];

note.多筆時出來的值

layer設圓角、陰影畫黑框

sampleView.layer.cornerRadius = 10.5; // 圓角的弧度
sampleView.layer.masksToBounds = YES; //只留圓角內的背景,不加的話背景會超過圓角。
//它的意思是layer的效果只在laye的範圍內才有效,意思是如果你加了它,你設定的陰影效果就永遠不會出現。


//陰影畫黑框
self.closeButton.image = UIImage(named: PhotoNode.CLOSE_BUTTON_BG)
self.closeButton.layer.shadowRadius = 1.0
self.closeButton.layer.shadowOpacity = 1

self.closeButton.layer.shadowOffset = CGSizeZero

2014年1月27日 星期一

OSX好用小技巧

//新增位於資料夾位置的終端機視窗
//把第一項勾起來,即可在滑鼠右鍵->服務->新增位於資料夾位置的終端機視窗


//在Finder中顯示隱藏檔案
顯示隱藏檔案:defaults write com.apple.finder AppleShowAllFiles TRUE;\killall Finder
不顯示隱藏檔案:defaults write com.apple.finder AppleShowAllFiles FALSE;\killall Finder

2014年1月24日 星期五

iOS7背景存活時間

//沒設定背景執行
直接掛起

//被服務喚醒(藍牙,音樂,GPS等等)
十秒鐘後被掛起

2014年1月19日 星期日

JSON string 轉 nsdictionary 或 nsarray

-(id)_JSONParser:(NSString*)JSONString{
    
    NSData* jsonData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError* error = nil;
    NSArray* jsonArray = nil;
    NSDictionary* jsonDictionary = nil;
    if (jsonData) {
        id _jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData                                                 options:NSJSONReadingMutableContainers error:&error];
        if ([_jsonObject isKindOfClass:[NSDictionary class]]) {
            jsonDictionary = (NSDictionary*)_jsonObject;
            NSLog(@"%@",jsonDictionary);
            return jsonDictionary;
        }else if ([_jsonObject isKindOfClass:[NSMutableArray class]]) {
            jsonArray = (NSArray*)_jsonObject;
            NSLog(@"%@",jsonArray);
            return jsonArray;
        }
    }
    return nil;

}

2014年1月16日 星期四

NSLog的格式

  • NSLog:雙引號所括起來的為格式化字串,@符號表示為 Objective-C物件型態:其他用法類似 C 語言 printf 函式
  • \n : 換行
  • \t  : TAB功能
  • %-ni : 整數欄位寬,預設向右靠齊,負號表示向左靠齊,n 為印出位數
  • %-nc : 字元欄位寬,預設向右靠齊,負號表示向左靠齊,n為印出位數
  • %-m.nf : 浮點數欄位寬,預設向右靠齊,負號表示向左靠齊,m 為包含小數點前包含小數點的總位數,n 為小數點後的位數
  • %i : int
  • %f : float
  • %d : double
  • %c : char

微博取用戶資料

先Oauth2認證後,取得tokenID,然後依照/users/show的參數文件填入uid跟tokenID
http://api.weibo.com/2/users/show.json?uid=1904178193&access_token=2.00zBrJ2EjQ8zzC15a429a8ff2pybUD&source=2045436852

PS,重點是要加上appkey,但這邊很GY害我搞很久的點是,它用source代表appkey,降子取名誰知道啊!翻桌

//20140121
PS,突然發現他的網址改成https了,現在用http開頭是不會通的

當你要跟微博互動時,分為下面二個部份
一、還沒拿到Token時,要利用微博App去跟微博要Token

#pragma mark- WeiboSDKDelegate
//微博App來跟我們寫的App要資料,會觸發這裡,你可以透過[WeiboSDK sendResponse:]回傳資料回去
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request

//這裡是用來接,微博App收到我們的請求的所發出的callback
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
#pragma mark- WBHttpRequestDelegate


二、當拿到Token以後,就不用透過微博App,而可以直接HttpRequest跟微博互動

//當你用這個方法去呼叫時[WBHttpRequest requestWithAccessToken:...],就會call back回下面的Delegate
#pragma mark- WBHttpRequestDelegate
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

- (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result

2014年1月10日 星期五

iOS7 navigation bar會透過去的問題

//解決iOS 7以上版面會跑掉的問題
+(void) setViewControllerTranslucent:(UIViewController *)viewController{
    
    if( [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f )
    {
        viewController.edgesForExtendedLayout = UIRectEdgeNone;
        if (viewController.navigationController) {
            viewController.navigationController.navigationBar.translucent = NO;
        }
        if (viewController.tabBarController) {
            viewController.tabBarController.tabBar.translucent = NO;
        }
    }
}

判斷GPS訊號強度

/*
* @ 取得指定 Location 的 GPS 訊號強度
* - Judge the Gps singal strength of limit location.
*/
-(KRGpsSingalStrength)singalStrengthWithLocation:(CLLocation *)_location
{
    //水平精度
    //CLLocationAccuracy horizontal = newLocation.horizontalAccuracy;
    //垂直精度
    //CLLocationAccuracy vertical = newLocation.verticalAccuracy;
   
    CLLocationAccuracy _horizontalAccuracy = _location.horizontalAccuracy;
    KRGpsSingalStrength _singalStrength = KRGpsSingalStrengthNone;
    if( (_horizontalAccuracy > 0.0f) && (_horizontalAccuracy <= 10.0f) )
    {
        _singalStrength = KRGpsSingalStrengthPerfect;
    }
    else if( (_horizontalAccuracy > 10.0f) && (_horizontalAccuracy <= 30.0f) )
    {
        _singalStrength = KRGpsSingalStrengthStrong;
    }
    else if( (_horizontalAccuracy > 30.0f) && (_horizontalAccuracy <= 60.0f) )
    {
        _singalStrength = KRGpsSingalStrengthHigh;
    }
    else if( (_horizontalAccuracy > 60.0f ) && (_horizontalAccuracy <= 100.0f) )
    {
        _singalStrength = KRGpsSingalStrengthMiddle;
    }
    else if( (_horizontalAccuracy > 100.0f) && (_horizontalAccuracy <= 200.0f))
    {
        _singalStrength = KRGpsSingalStrengthLow;
    }
    return _singalStrength;
}

2014年1月9日 星期四

iOS 7 的UITableView上面會空一行空白的問題

//在你的UIViewController中加入這行,關掉該屬性。
if([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]){
      [self setAutomaticallyAdjustsScrollViewInsets:NO];
}


2014年1月8日 星期三

直接顯示URL上的圖片

//設定圖片的url位址
                NSURL *url = [NSURL URLWithString:[content objectForKey:@"url"]];
                UIImage *urlImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];

                cell.imageView.image = urlImage;

2014年1月7日 星期二

透過Xib初始化要記得實作initWithCoder

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initWithParameter];
    }
    return self;

}

- (void)initWithParameter{
    //初始化參數
}

客製化UITableView

//在要用到的TableViewController中實作這裡
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //為某個種類的Cell給一個特定的名字作為區分
    static NSString *CellIdentifier = @"Cell";
    //如果TableView裡還有Cell的話,把cell叫出來使用
    StoresOnePlaceCell *cell = (StoresOnePlaceCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //如果叫不出來cell,就new一個新的cell
    if (cell == nil)
    {
        cell = [[StoresOnePlaceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell...
    
    [cell setCustomViewWithStoreInfo:[self.stores objectAtIndex:indexPath.row]];
    
    return cell;
}


//在你客製化的Cell中實作這二個方法,在其中引入你要客製化的View
#import "StoresOnePlaceCell.h"
#import "MapStoreView.h"

static NSInteger _kMapStoreViewTag = 7001;

@implementation StoresOnePlaceCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setCustomViewWithStoreInfo:(NSDictionary *)storeInfo
{
    MapStoreView *storeView = (MapStoreView *)[self.contentView viewWithTag:_kMapStoreViewTag];
    BOOL _isMakeNew         = NO;
    if( !storeView )
    {
        storeView  = [MapStoreView new];
        [storeView setTag:_kMapStoreViewTag];
        _isMakeNew = YES;
    }
    storeView.people = [[storeInfo objectForKey:@"instore_account_count"] integerValue];
    storeView.name = [storeInfo objectForKey:@"name"];
    storeView.items = [[storeInfo objectForKey:@"total_items"] integerValue];
    storeView.hotness = [[storeInfo objectForKey:@"hotness"] integerValue];
    storeView.followed = [[storeInfo objectForKey:@"total_followed"] integerValue];
    storeView.distance = [[storeInfo objectForKey:@"distance"] floatValue];
    [storeView make];
    [storeView setUserInteractionEnabled:NO];
    if( _isMakeNew )
    {
        [self.contentView addSubview:storeView];
    }
}

-(CGFloat)getCustomViewHeight
{
    MapStoreView *storeView = (MapStoreView *)[self.contentView viewWithTag:_kMapStoreViewTag];
    return storeView.frame.size.height;
}


@end

2014年1月3日 星期五

//TODO:超方便

在code加入//TODO: ...就會在method列表中用明顯的粗黑體顯示出來

你可以在共同開發時,告訴對方要做什麼

也可以在自己開發時,先將method架構寫好後先塞//TODO:(必須加上說明),之後再寫要實作的部份

注意,這個不能寫到Block中,不然不會有作用

2014年1月2日 星期四

NSDate的操作

時間與文字

//NSString => NSDate


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2014-01-02 14:16:30"];
NSLog(@"%@", date);
[dateFormatter release];


//NSDate => NSString


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
[dateFormatter release];



//時間的加減

NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
NSDate * anHourAfter = [now dateByAddingTimeInterval:60*60];

//只取某個時間單位

NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit fromDate:record.sleepDate];

index = comps.hour - startComps.hour;


//取得時間差
NSTimeInterVal duration = [now timeIntervalSinceDate:lastTime];

//時間的比較
if ([date compare:previousDate] == NSOrderedAscending) {
    //TODO:date 比 previousDate 來的小
}
if ([date compare:previousDate] == NSOrderedSame) {
    //TODO:date 跟 previousDate 是相同時間
}
if ([date compare:previousDate] == NSOrderedDescending) {
    //TODO:date 比 previousDate 來的大
}

//Sample codes
    date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"YYYY-MM-dd";
    formatter.timeZone = [NSTimeZone defaultTimeZone];
    NSMutableString *dateStr = [NSMutableString stringWithString:[formatter stringFromDate:date]];
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:se.wakeUpDate];
    [dateStr appendFormat:@" %02d:%02d:00", comps.hour, comps.minute];

    formatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";