iOS9.0以后那些被不推薦使用(deprecated)方法之:sendAsynchronousRequest was deprecated in iOS 9、UIAlertView was deprecated


iOS9.0以后那些被不推薦使用(deprecated)方法之:sendAsynchronousRequest 和 UIAlertView 

一、UIAlertview

  在Xcode7 ,iOS9.0的SDK中,已經明確提示不再推薦使用UIAlertView,而只能使用UIAlertController;

  點擊一個按鈕,然后彈出提示框的示例代碼如下:

#import "ViewController.h" 
 
@interface ViewController () 
 
@property(strong,nonatomic) UIButton *button; 
 
@end 
 
@implementation ViewController 
 
- (void)viewDidLoad { 
[super viewDidLoad]; 
 
self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, 20)]; 
[self.button setTitle:@"跳轉" forState:UIControlStateNormal]; 
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[self.view addSubview:self.button]; 
 
[self.button addTarget:self action:@selector(clickMe:) forControlEvents:UIControlEventTouchUpInside]; 
 
} 
 
-(void)clickMe:(id)sender{ 
 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"按鈕被點擊了" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil nil]; 
[alert show]; 
 
} 
 
@end  

   但是會有警告:“‘UIAlertView’ is deprecated:first deprecated in iOS 9.0 - UIAlertView is deprecated.。。。表明UIAlertView已經iOS9中被棄用(不推薦)使用。推薦使用UIAlertController。

  為解決這個warning,使用UIAlertController來解決這個問題。代碼如下:

#import "ViewController.h" 
 
@interface ViewController () 
 
@property(strong,nonatomic) UIButton *button; 
 
@end 
 
@implementation ViewController 
 
- (void)viewDidLoad { 
[super viewDidLoad]; 
 
self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, 20)]; 
[self.button setTitle:@"跳轉" forState:UIControlStateNormal]; 
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[self.view addSubview:self.button]; 
 
[self.button addTarget:self action:@selector(clickMe:) forControlEvents:UIControlEventTouchUpInside]; 
 
} 
 
-(void)clickMe:(id)sender{ 
 
//初始化提示框; 
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"按鈕被點擊了" preferredStyle: UIAlertControllerStyleAlert]; 
 
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 
//點擊按鈕的響應事件; 
}]]; 
 
//彈出提示框; 
[self presentViewController:alert animated:true completion:nil]; 
 
 
} 
 
 
 
@end 

 

  通過運行發下,程序運行后的效果相同。 其中preferredStyle這個參數還有另一個選擇:UIAlertControllerStyleActionSheet。選擇這個枚舉類型后,實現效果:提示框會從底部彈出。

  -》對比:通過查看代碼還可以發現,在提示框中的按鈕響應不再需要delegate委托來實現了。直接使用addAction就可以在一個block中實現按鈕點擊,非常方便。

 

二、NSURLSession替換NSURLConnection

 最近使用[NSURLConnection sendAsynchronousRequest]時已經警告為不推薦使用了,蘋果官方推薦使用NSURLSession中的dataTaskWithRequest方法。

 用NSURLConnection實現的示例代碼如下:

  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方法實現如下:

//推薦使用這種請求方法;
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];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM