Blame view

ios-examples-13.4.9/Ex_File/FileExample/EMFileViewController.m 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
//  EMFileViewController.m
//  FileExample
//
//  Created by Dexter Weiss on 10/11/13.
//  Copyright (c) 2013 Emmoco. All rights reserved.
//

#import "EMFileViewController.h"
#import "EMFramework.h"

@interface EMFileViewController () <UITextViewDelegate> {
    IBOutlet UITextView *_readTextFile;
    IBOutlet UITextField *_numberOfBytesField;
}

@end

@implementation EMFileViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_readTextFile setText:nil];
}

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

#pragma mark - Interface Actions

-(IBAction)readButtonPressed:(id)sender {
    [[EMConnectionManager sharedManager] readResource:@"fileData" onSuccess:^(id readValue) {
        NSLog(@"%@", readValue);
        [_readTextFile setText:[readValue description]];
        NSString *bytesString = [readValue length] == 1 ? @"byte" : @"bytes";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:[NSString stringWithFormat:@"Successfully read %d %@", [readValue length], bytesString] delegate:nil cancelButtonTitle:@"Sweet!" otherButtonTitles:nil];
        [alert show];
    } onFail:^(NSError *error) {

    }];
}

-(IBAction)sliderValueChanged:(UISlider *)sender {
    [_numberOfBytesField setText:[NSString stringWithFormat:@"%0.0f", [sender value]]];
}

-(IBAction)writeButtonPressed:(id)sender {
    NSInteger numberOfBytes = [[_numberOfBytesField text] integerValue];

    [[EMConnectionManager sharedManager] writeValue:[NSNumber numberWithInt:numberOfBytes] toResource:@"fileSize" onSuccess:^{

        UInt8 *integerArray = malloc(numberOfBytes);
        for (NSInteger i = 0; i < numberOfBytes; ++i) {
            integerArray[i] = i;
        }

        NSData *data = [NSData dataWithBytes:integerArray length:numberOfBytes];
        NSString *bytesString = numberOfBytes == 1 ? @"byte" : @"bytes";

        [[EMConnectionManager sharedManager] writeValue:data toResource:@"fileData" onSuccess:^{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:[NSString stringWithFormat:@"Successfully wrote %d %@", numberOfBytes, bytesString] delegate:nil cancelButtonTitle:@"Sweet!" otherButtonTitles:nil];
            [alert show];
        } onFail:^(NSError *error) {

        }];

    } onFail:^(NSError *error) {

    }];
}

#pragma mark - Text View Delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end