The third weekly report of IOS development training

The third weekly report of IOS development training

Conclusion:

The development of the project starts this week. At present, the team plans to complete the front-end development together. The part I am responsible for is the personal page. When it comes to loading personal information, it is necessary to obtain pictures from the album or camera, upload them as the head portrait, and make network requests and obtain resources. Therefore, this week's weekly report summarizes the contents of this part;

Summary of learning points

1. Call system camera and album

STEP 1: first, add permissions for the project to allow app access to cameras and albums,


STEP 2: create UIImagePickController,

@property (strong, nonatomic) UIImagePickerController *picker;

- (UIImagePickerController *)picker
{
    if (!_picker) {
        _picker = [[UIImagePickerController alloc]init];
    }
    return _picker;
}

STEP3: add agent

Two agents, UIImagePickerControllerDelegate and UINavigationControllerDelegate, need to be added and implemented in the agent:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//    Get pictures
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    self.image.image = image;
//    Get the picture and return
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//Function when the Cancel button is pressed
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
//    Return
    [picker dismissViewControllerAnimated:YES completion:nil];
}

STEP 4: access camera or album:

// Judge whether the camera is available
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
	isPicker = true;
}

// Open Album
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
isPicker = true;

// 
if (isPicker) {
	[self presentViewController:self.picker animated:YES completion:nil];
}

2. Send network request GET and POST

The network request API s of IOS include: NSURLConnection and NSURLSession;

Common network request frameworks are: AFNetworking;

(1)NSURLConnection

NSURL *url = [NSURL URLWithString:@"http://m.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// Send network request, asynchronous call, put into operation queue

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
   
    NSLog(@"Network response: response: %@",response);
    
    NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
};

(2)NSURLSession

NSURL *url = [NSURL URLWithString:@"http://m.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// Get session object
NSURLSession *session=[NSURLSession sharedSession];

//4. Create a task according to the session object (send request)

NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        //Response: response: response from the server
        //Data: binary data: data returned by the server. (that's what we want)
        //Error: link error information
        
        NSLog(@"Network response: response: %@",response);
  
        NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        
        });
    
    }
];

(3)AFNetworking

//GET request
-(void)demo1{
    NSString *urlString = @"http://www.liubaiqi.cn";
    
    AFHTTPSessionManager *manger =[AFHTTPSessionManager manager];
    
    [manger GET:urlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"Success");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

//POST request
-(void)demo2{
    NSString *urlString = @"http://192.168.1.101:8080";
    
    AFHTTPSessionManager *manger =[AFHTTPSessionManager manager];
    
    NSMutableDictionary *parameter= @{@"":@"",@"":@""};
    
    [manger POST:urlString parameters:parameter progress:^(NSProgress * _NonnulluploadProgress){
        
    }success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"Success");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

//DownLoad request
-(void)demo3{
    //1. Create NSURLSessionConfiguration
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    //2. Create manager object
    AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
    
    //3. set url
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/1.mp4"];
    
    //4. Create request object
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //5. Download task
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        //Print download progress
        NSLog(@"%lf",1.0*downloadProgress.completedUnitCount/downloadProgress.totalUnitCount);
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        //Set download path
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager]URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
        
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        //Download complete
        NSLog(@"File downloaded to : %@",filePath);
    }];
    //Start task
    [downloadTask resume];
}

follow-up task

Through this week's study, I have finished the upload and display of the avatar in my personal page, as well as the way to execute the network request. Next, I am responsible for the design of secondary pages, such as collection, browsing, likes, friends, etc., which will involve the study of UITableView;

Published 23 original articles, won praise 5, visited 4860
Private letter follow

Tags: network Session iOS encoding

Posted on Wed, 29 Jan 2020 07:48:31 -0800 by ryansmith44