Open judaco opened 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
//
// 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
//
// 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