AP102 2016/5/26 早上

AP102 2016/5/26 早上

LOGIN

//Parse JSON to objects
        NSMutableDictionary * datas = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

//        NSJSONReadingMutableContainers = (1UL << 0),000000001=1
//        NSJSONReadingMutableLeaves = (1UL << 1),    000000010=2
//        NSJSONReadingAllowFragments = (1UL << 2)    000000100=4
        NSString *message = nil;

        NSMutableDictionary * userInfo = datas[@"user_info"];
        NSNumber *karma = userInfo[@"karma"];

        if(datas[@"error_text"]!=nil){
            message = datas[@"erroe_text"];

            }else{
                message=@"Login successfully";
            }


GCD用法

//分兩條路
dispatch_async
//做完才執行
dispatch_sync


跳出karma

 dispatch_async(dispatch_get_main_queue(), ^{
            NSString * karmaString = [NSString stringWithFormat:@"Karma:%f",[karma doubleValue]];

            UIAlertController*alert=[UIAlertController alertControllerWithTitle:karmaString message:message preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction*ok=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

            [alert addAction:ok];
            [self presentViewController:alert animated:true completion:nil];


 NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession * session = [NSURLSession sessionWithConfiguration:config];

 ->[_loadingIndicatorView stopAnimating];

    NSURLSessionDataTask * task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        if(error){
            NSLog(@"Error:%@",error);
            return;
        }
 dispatch_async(dispatch_get_main_queue(), ^{

            -->[_loadingIndicatorView stopAnimating];

            NSString * karmaString = [NSString stringWithFormat:@"Karma:%f",[karma doubleValue]];

如果失敗也要加入停止讀取的UI


        if(error){
            NSLog(@"Error:%@",error);
            dispatch_async(dispatch_get_main_queue(), ^{
               -> [_loadingIndicatorView stopAnimating];
            });
            return;
        }

分享

- (IBAction)publishBtnPressed:(id)sender {
      NSString *content = _comment.text;

        NSString * urlString = [NSString stringWithFormat:@"http://www.plurk.com/API/Timeline/plurkAdd?api_key=%@&content=%@&qualifier=shares&lang=tr_ch",PLURK_API_KEY,content];

        NSURL * url = [NSURL URLWithString:urlString];

 message=@"Publish successfully";


網址不能有空格
處理空格

SString *content = _comment.text;

        NSString *encodedContent =[content stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

        NSLog(@"%@ ==> %@",content,encodedContent);


        NSString * urlString = [NSString stringWithFormat:@"http://www.plurk.com/API/Timeline/plurkAdd?api_key=%@&content=%@&qualifier=shares&lang=tr_ch",PLURK_API_KEY,encodedContent];

Comments

Popular posts from this blog

MEGA 暫存檔案刪除

XAMPP 使用多PORT來執行不同網頁

IOS GCD多執行緒