Developing Mobile
Application
Ios : session #8
By : Amr Elghadban
AMR
ELGHADB
AN
ABOUT ME
———————————
SOFTWARE
ENGINEER
FCI - HELWAN 2010
ITI INTAKE 32
WORKED BEFORE IN
- Tawasol IT
- Etisalat Masr
- NTG Clarity
Agenda
CLEAN CODE GUIDE
Making Network Requests
UIImagePickerController
CLEAN CODE - Structure
1.Does the code conform to any pertinent coding
standards?
2. Is the code well-structured, consistent in style, and
consistently formatted?
3.Are there any uncalled or unneeded procedures or
any unreachable code?
4.Are there any blocks of repeated code that could be
condensed into a single procedure?
5.Is storage use efficient?
CLEAN CODE - Documentation
1. Is the code clearly and adequately documented with
an easy-to-maintain commenting style?
2. Are all comments consistent with the code?
CLEAN CODE - Variables
1.Are all variables properly defined with meaningful,
consistent, and clear names?
2.Do all assigned variables have proper type
consistency or casting?
3.Are there any redundant or unused variables?
CLEAN CODE - Loops and Branches
1. Are all loops, branches, and logic constructs
complete, correct, and properly nested?
2.Are the most common cases tested first in IF-
-ELSEIF chains?
3.Are all cases covered in an IF- -ELSEIF or CASE
block, including ELSE or DEFAULT clauses?
4.Does every case statement have a default?
network requests ConT..
// step 1
NSString *dataUrl = @"YOUR_DATA_URL";
NSURL *url = [NSURL URLWithString:dataUrl];
// step 2
NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse
*response, NSError *error) {
// step 4: Handle response here
}];
// step 3
[downloadTask resume];
Image Download
https://upload.wikimedia.org/wikipedia/commons/7/7f/Williams_River-27527.jpg
//1
NSURL *url = [NSURL URLWithString:
@"http://upload.wikimedia.org/wikipedia/commons/7/7f/Williams_River-27527.jpg"];
// 2
NSURLSessionDownloadTask *downloadPhotoTask = [[NSURLSession sharedSession]
downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
// 3
UIImage *downloadedImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:location]];
}];
// 4
[downloadPhotoTask resume];
UIImagePickerController
Open up ViewController.m
We’ll implement a UIButton method to make it open the photo
gallery.
To do so, we create a new instance of
UIImagePickerController and set it’s sourceType to either
UIImagePickerControllerSourceTypeSavedPhotosAlbum or
UIImagePickerControllerSourceTypePhotoLibrary or
UIImagePickerControllerSourceTypeCamera “later topic / need
real drive to be tested”
The difference is that the PhotosAlbum will take you directly to
the album for the pictures taken with the camera (the Camera
Roll) while PhotoLibrary will take you to the albums list (of which
Camera Roll is one of the albums if the device has a camera).
- (IBAction)tappedUseCamera:(id)sender {
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.sourceType =
UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentViewController:picker
animated:YES completion:nil];
}
If you try to run the app right now in the simulator, it’s
going to crash when you tap the button.
The reason this happens is that the camera isn’t
available on the simulator.
We can prevent the crash by checking to see if the
camera is an available source type before using it by
calling the UIImagePickerController’s
isSourceTypeAvailable method like this:
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSour
ceTypeCamera])
we need to add the UIImagePickerControllerDelegate’s
didFinishPickingMediaWithInfo method:
-(void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
[self.imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}
The first thing we’ll do is pull the
UIImagePickerControllerOriginImage property out of
the NSDictionary that is sent into the method.
This property is of the type UIImage which is what
we can then use to set our imageView’s image
property using setImage.
Lastly, we have to call
dismissViewControllerAnimated on the view
controller. If we don’t do this, the preview won’t go
away and our app won’t return (even though it
returned on it’s own when we didn’t implement the
method).
THANKS
â–¸ Skype : amr_elghadban
â–¸ Email : amr.elghadban@gmail.com
â–¸ Phone : (+20) 1098558500
â–¸ Fb/amr.elghadban
â–¸ Linkedin/amr_elghadban
â–¸ ios_course facebook group : https://www.facebook.com/groups/1161387897317786/
WISH YOU WONDERFUL DAY

08 objective-c session 8

  • 1.
    Developing Mobile Application Ios :session #8 By : Amr Elghadban
  • 2.
  • 3.
    Agenda CLEAN CODE GUIDE MakingNetwork Requests UIImagePickerController
  • 4.
    CLEAN CODE -Structure 1.Does the code conform to any pertinent coding standards? 2. Is the code well-structured, consistent in style, and consistently formatted? 3.Are there any uncalled or unneeded procedures or any unreachable code? 4.Are there any blocks of repeated code that could be condensed into a single procedure? 5.Is storage use efficient?
  • 5.
    CLEAN CODE -Documentation 1. Is the code clearly and adequately documented with an easy-to-maintain commenting style? 2. Are all comments consistent with the code?
  • 6.
    CLEAN CODE -Variables 1.Are all variables properly defined with meaningful, consistent, and clear names? 2.Do all assigned variables have proper type consistency or casting? 3.Are there any redundant or unused variables?
  • 7.
    CLEAN CODE -Loops and Branches 1. Are all loops, branches, and logic constructs complete, correct, and properly nested? 2.Are the most common cases tested first in IF- -ELSEIF chains? 3.Are all cases covered in an IF- -ELSEIF or CASE block, including ELSE or DEFAULT clauses? 4.Does every case statement have a default?
  • 8.
    network requests ConT.. //step 1 NSString *dataUrl = @"YOUR_DATA_URL"; NSURL *url = [NSURL URLWithString:dataUrl]; // step 2 NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // step 4: Handle response here }]; // step 3 [downloadTask resume];
  • 9.
    Image Download https://upload.wikimedia.org/wikipedia/commons/7/7f/Williams_River-27527.jpg //1 NSURL *url= [NSURL URLWithString: @"http://upload.wikimedia.org/wikipedia/commons/7/7f/Williams_River-27527.jpg"]; // 2 NSURLSessionDownloadTask *downloadPhotoTask = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { // 3 UIImage *downloadedImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:location]]; }]; // 4 [downloadPhotoTask resume];
  • 10.
    UIImagePickerController Open up ViewController.m We’llimplement a UIButton method to make it open the photo gallery. To do so, we create a new instance of UIImagePickerController and set it’s sourceType to either UIImagePickerControllerSourceTypeSavedPhotosAlbum or UIImagePickerControllerSourceTypePhotoLibrary or UIImagePickerControllerSourceTypeCamera “later topic / need real drive to be tested” The difference is that the PhotosAlbum will take you directly to the album for the pictures taken with the camera (the Camera Roll) while PhotoLibrary will take you to the albums list (of which Camera Roll is one of the albums if the device has a camera).
  • 11.
    - (IBAction)tappedUseCamera:(id)sender { UIImagePickerController*picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.delegate = self; [self presentViewController:picker animated:YES completion:nil]; }
  • 12.
    If you tryto run the app right now in the simulator, it’s going to crash when you tap the button. The reason this happens is that the camera isn’t available on the simulator. We can prevent the crash by checking to see if the camera is an available source type before using it by calling the UIImagePickerController’s isSourceTypeAvailable method like this: if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSour ceTypeCamera])
  • 13.
    we need toadd the UIImagePickerControllerDelegate’s didFinishPickingMediaWithInfo method: -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; [self.imageView setImage:image]; [self dismissViewControllerAnimated:YES completion:nil]; }
  • 14.
    The first thingwe’ll do is pull the UIImagePickerControllerOriginImage property out of the NSDictionary that is sent into the method. This property is of the type UIImage which is what we can then use to set our imageView’s image property using setImage. Lastly, we have to call dismissViewControllerAnimated on the view controller. If we don’t do this, the preview won’t go away and our app won’t return (even though it returned on it’s own when we didn’t implement the method).
  • 15.
    THANKS â–¸ Skype :amr_elghadban â–¸ Email : amr.elghadban@gmail.com â–¸ Phone : (+20) 1098558500 â–¸ Fb/amr.elghadban â–¸ Linkedin/amr_elghadban â–¸ ios_course facebook group : https://www.facebook.com/groups/1161387897317786/ WISH YOU WONDERFUL DAY