judaco / iPhone

0 stars 0 forks source link

iPhone - Class 14 #10

Open judaco opened 7 years ago

judaco commented 7 years ago
//
//  ViewController.m
//  NSURLSession
//
//  Created by hackeru on 28/05/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <NSURLSessionDelegate, NSURLSessionDataDelegate>

@end

@implementation ViewController
{
    NSMutableData * mutableData;
    NSURLSession * session;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //Define a configuration
    NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    configuration.timeoutIntervalForRequest = 15.0; // after 15 seconds if the server is not responding disconnect the request
    session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

    NSURL * url = [NSURL URLWithString:@"http://www.uefa.com"];
    //bring data
    /*NSURLSessionDataTask * task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //NSData is an array of bytes, these are the bytes of the response
        if (error == nil) {
            NSLog(@"data length: %li", data.length);
            NSString * dataAsString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

            //NSLog(@"%i", [[NSThread currentThread] isMainThread]);
            //runs on global queue

        }else{
            NSLog(@"un grande casino %@", [error description]);
        }
        [session finishTasksAndInvalidate];//close the connection. like connection.close
    }];*/

    //this is POST
    //NSURLSessionDownloadTask * task = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable //location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    //}];
    mutableData = [[NSMutableData alloc] init];
    NSURLSessionDataTask * task = [session dataTaskWithURL:url];
    [task resume];//starting the task itself
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
    NSLog(@"did receive data");
    [mutableData appendData:data];
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
    if (error == nil){
        NSString * dataAsString = [[NSString alloc] initWithData:mutableData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", dataAsString);
        NSLog(@"%i", [NSThread isMainThread]);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
judaco commented 7 years ago
//
//  ViewController.m
//  Http Post Request
//
//  Created by hackeru on 04/06/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <NSURLSessionDelegate, NSURLSessionDataDelegate>

@end

@implementation ViewController
    {
        NSURLSession * session;
    }

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    configuration.timeoutIntervalForRequest = 15.0;
    session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSString * stringToSendToServer = @"ciao server! come stai oggi?";
    NSData * dataToUpload = [stringToSendToServer dataUsingEncoding:NSUTF8StringEncoding];
    NSURL * url = [NSURL URLWithString:@"http://localhost:8080/MainServlet"];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    NSURLSessionDataTask * task = [session uploadTaskWithRequest:request fromData:dataToUpload];
    [task resume];
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
    NSLog(@"did receive data");
    NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", string);
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
        if (error == nil) {
            NSLog(@"did complete");
        }
    [session finishTasksAndInvalidate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
judaco commented 7 years ago
//
//  ViewController.m
//  JSON Serialization
//
//  Created by hackeru on 04/06/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSDictionary<NSString*, NSString*> * dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", [NSArray arrayWithObjects:@"Juda", @"Shalev", @"Regev", @"Eliran", nil], @"key2", nil];
//    NSString * s = (NSString*)dictionary[@"key1"];
//    NSLog(@"value of s%@", s);
//    NSArray * array = (NSArray*)dictionary[@"key2"];
//    NSLog(@"first element %@", array[0]);
//}

    NSDictionary * person = [[NSDictionary alloc] initWithObjectsAndKeys:@"Juda", @"First Name", @"Cossa", @"Last Name", [NSNumber numberWithInt:30], @"Age", [NSArray arrayWithObjects:@"Shalev", @"Regev", nil], @"Friends", nil];

    NSError * error = nil;

    NSData * data = [NSJSONSerialization dataWithJSONObject:person options:NSJSONWritingPrettyPrinted error:&error];
    if (error == nil) {
        NSString * dataAsString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@", dataAsString);
    }

     id jsonObject = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    if (error == nil) {
        NSDictionary<NSString*, NSObject*> * dictionary2 = (NSDictionary*) jsonObject;
        NSLog(@"%@", dictionary2[@"First Name"]);
    }

}

- (void) didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
judaco commented 7 years ago
//
//  ViewController.m
//  Recording Audio
//
//  Created by hackeru on 04/06/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController () <AVAudioPlayerDelegate, AVAudioRecorderDelegate>

@end

@implementation ViewController
    {
        AVAudioPlayer * player;
        AVAudioRecorder * recorder;
    }

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)startRecordingAudio{
    NSError * error;
    NSURL * recordingPath = [self audioRecordingPath];
    recorder = [[AVAudioRecorder alloc] initWithURL:recordingPath settings:[self audioRecordingSettings] error:&error];
    if (error == nil) {
        recorder.delegate = self;
        if ([recorder prepareToRecord] && [recorder record]) {
            //now recording
        }
    }
}

-(NSDictionary*)audioRecordingSettings{
    return [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey, [NSNumber numberWithInt:16000], AVSampleRateKey, [NSNumber numberWithInt:1], AVNumberOfChannelsKey, AVAudioQualityLow, AVEncoderAudioQualityKey, nil];
}
-(NSURL*)audioRecordingPath{
    NSError * error;
    NSURL * url = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&error];
    return [url URLByAppendingPathComponent:@"recording.m4a"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end