iOS開發——來改掉那些被禁用的方法吧(持續更新中)

來源:互聯網
上載者:User

iOS開發——來改掉那些被禁用的方法吧(持續更新中)

iOS平台在快速的發展,各種介面正在不斷的更新。隨著iOS9的發布,又有一批老方法不推薦使用了,你若調用這些方法,啟動並執行結果是沒有問題的,但是會出現警告“***is deprecated :first deprecated in iOS 9.0 - Use *******”.就像:

 

在實際項目開發中,我們要秉承一個信念就是:要把每一個警告當做錯誤來處理,並解決每一個警告。你想想,你運行一個項目,就算運行成功了,但是出現幾十個、甚至幾百個黃黃的警告,心情是不是很糟糕呢?我將在這篇部落格結合我的開發經驗,羅列出iOS9中不推薦使用的方法,並換成蘋果最新推薦的方式。本篇部落格將會持續更新。

1.【彈出提示對話方塊】

在iOS9之前我們使用AlertView來彈出對話方塊,現在推薦使用AlertController,對於這個變化,請參考我的另一篇部落格《iOS9使用提示框的正確實現方式》。

 

2.【stringByAddingPercentEncodingWithAllowedCharacters替換stringByAddingPercentEscapesUsingEncoding】

這個方法真的好長。。。我們使用這個方法來進行字串編碼方式的更改。最常用的地方就是進行Http網路請求的時候,發送的連結的參數中如果帶有中文,那麼首先就需要調用這個方法把編碼方式改為utf8,因為伺服器端一般都使用utf8編碼。兩者實現的功能一樣。

 

//以下方法已經不推薦使用;  //  NSString *urlStr = [@http://v.juhe.cn/weather/index?format=2&cityname=北京&key=88e194ce72b455563c3bed01d5f967c5stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

 //建議使用這個方法stringByAddingPercentEncodingWithAllowedCharacters,不推薦使用stringByAddingPercentEscapesUsingEncoding;  NSString *urlStr2 = [@http://v.juhe.cn/weather/index?format=2&cityname=北京&key=88e194ce72b455563c3bed01d5f967c5 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];


 

3.【NSURLSession替換NSURLConnection】

NSURLSession已經漸漸走上曆史舞台了。最近使用[NSURLConnection sendAsynchronousRequest]時已經警告為不推薦使用了,那我們就換成NSURLSession中的dataTaskWithRequest方法吧。

 

  //以下方法已經被禁用;    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    [NSURLConnection   sendAsynchronousRequest:urlRequest   queue:queue   completionHandler:^(NSURLResponse *response,                       NSData *data,                       NSError *error) {          if ([data length] >0  &&         error == nil){       NSString *html = [[NSString alloc] initWithData:data                                              encoding:NSUTF8StringEncoding];       resault=[html copy];              NSLog(@返回的伺服器資料 = %@, html);     }     else if ([data length] == 0 &&              error == nil){       NSLog(@Nothing was downloaded.);     }     else if (error != nil){       NSLog(@發生錯誤 = %@, error);     }        }];

  //推薦使用這種要求方法;  NSURLSession *session = [NSURLSession sharedSession];    __block  NSString *result = @;  NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {        if (!error) {      //沒有錯誤,返回正確;      result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];      NSLog(@返回正確:%@,result);          }else{      //出現錯誤;      NSLog(@錯誤資訊:%@,error);    }      }];      [dataTask resume];


 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.