-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathACAppDelegate.m
More file actions
92 lines (71 loc) · 2.83 KB
/
ACAppDelegate.m
File metadata and controls
92 lines (71 loc) · 2.83 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
//
// ACAppDelegate.m
// iOS Simulator Notifications
//
// Created by Arnaud Coomans on 22/02/14.
// Copyright (c) 2014 acoomans. All rights reserved.
//
#import "ACAppDelegate.h"
#import "ACSimulatorRemoteNotificationsService.h"
static NSString * const ACAppDelegatePayloadUserDefaultsKey = @"ACAppDelegatePayloadUserDefaultsKey";
@interface ACAppDelegate ()<NSTextViewDelegate>
@property (strong,nonatomic) NSDictionary * payload;
@end
@implementation ACAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSDictionary *payload = [[NSUserDefaults standardUserDefaults] objectForKey:ACAppDelegatePayloadUserDefaultsKey];
if (![payload isKindOfClass:NSDictionary.class]) {
[self resetAction:self];
} else {
NSData *data = [NSJSONSerialization dataWithJSONObject:payload options:NSJSONWritingPrettyPrinted error:nil];
if (data) {
self.payloadTextView.string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
}
_payloadTextView.delegate = self;
_payloadTextView.richText = NO;
}
#pragma mark - NSTextDelegate
- (void)textDidChange:(NSNotification *)notification
{
[self updateUI];
}
- (void) updateUI
{
NSError *error;
NSData *data = [self.payloadTextView.string dataUsingEncoding:NSUTF8StringEncoding];
_payload = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!_payload || ![_payload isKindOfClass:NSDictionary.class]) {
if(error){
self.errorTextField.stringValue = [NSString stringWithFormat:@"Invalid JSON : %@", error.localizedDescription];
}else{
self.errorTextField.stringValue = @"Invalid JSON : Not a dictionnary";
}
self.errorTextField.hidden = NO;
}else{
self.errorTextField.hidden = YES;
}
}
#pragma mark - Actions
- (IBAction)resetAction:(id)sender {
NSDictionary *dict = @{
@"aps" : @{
@"alert" : @"You got your emails.",
@"badge" : @9,
@"sound" : @"bingbong.aiff"
},
@"acme1" : @"bar",
@"acme2" : @42
};
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
self.payloadTextView.string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self updateUI];
}
- (IBAction)sendAction:(id)sender
{
if(!self.errorTextField.isHidden) return;
[[ACSimulatorRemoteNotificationsService sharedService] send:_payload];
[[NSUserDefaults standardUserDefaults] setObject:_payload forKey:ACAppDelegatePayloadUserDefaultsKey];
}
@end