-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMActionConfirmationView.m
153 lines (115 loc) · 4.72 KB
/
MMActionConfirmationView.m
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// MMActionConfirmationView.m
//
// Created by Michael Mavris on 13/06/16.
// Copyright © 2016 Miksoft. All rights reserved.
//
#import "MMActionConfirmationView.h"
#import "Enum.h"
@interface MMActionConfirmationView()
@property (nonatomic, assign) id <MMActionConfirmationViewDelegate> delegate;
@property (nonatomic, strong) UIWindow *activityWindow;
@property(nonatomic,assign)NSInteger mode;
@property (nonatomic,strong)UIAlertController *alertController;
@property (nonatomic,strong)NSString *value;
@property (nonatomic,strong)NSString *okTitle;
@property (nonatomic,strong)NSString *cancelTitle;
@property (nonatomic,strong)NSString *title;
@end
@implementation MMActionConfirmationView{
UIViewController *blankViewController;
}
-(instancetype)initWithConfirmationMode:(NSInteger)mode Value:(NSString*)stringValue andDelegate:(id<MMActionConfirmationViewDelegate>)del{
self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
if (self) {
self.mode=mode;
self.value=stringValue;
[self setupWindowAndController];
self.delegate=del;
}
return self;
}
-(void)setupWindowAndController{
//We create a new UIViewController and add our view. This UIViewController will be transparent
blankViewController = [[UIViewController alloc] init];
[[blankViewController view] setBackgroundColor:[UIColor clearColor]];
//We create a new UIWindow and add our UIViewController. This UIWindow will be transparent
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setRootViewController:blankViewController];
[window setBackgroundColor:[UIColor clearColor]];
[window setWindowLevel:UIWindowLevelAlert];
[blankViewController.view addSubview:self];
[self setActivityWindow:window];
}
-(void)updateConstraints {
[super updateConstraints];
}
-(UIAlertController*)alertController{
if(_alertController==nil){
NSURL *url;
//The string for the Cancel button
self.cancelTitle = @"Cancel";
switch (self.mode){
case telephoneConfirmation:
self.title=self.value;
self.okTitle=@"Call";
url= [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",self.value]];
break;
case websiteConfirmation:
self.title=self.value;
self.okTitle=@"Visit";
url= [NSURL URLWithString:self.value];
break;
case facebookConfirmation:
self.title=@"Open Facebook";
self.okTitle=@"Open";
url= [NSURL URLWithString:self.value];
break;
case emailConfirmation:
self.title=self.value;
self.okTitle=@"Email";
url= [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@",self.value]];
break;
default:
break;
}
//Initializign the OK button
UIAlertAction *okButton = [UIAlertAction actionWithTitle:self.okTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
else {
//The delegate that informs the controller that the request failed
[self.delegate MMActionConfirmationCantOpenURLDelegateForMode:self.mode];
}
//Hiding the UIViewController after showing the confirmation
[self hide];
}];
//Initializign the Cancel button
UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:self.cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
//Hiding the UIViewController after showing the confirmation
[self hide];
}];
//Initializing the controller
_alertController = [UIAlertController alertControllerWithTitle:self.title message:nil preferredStyle:UIAlertControllerStyleAlert];
if (okButton) {
[_alertController addAction:okButton];
}
if (cancelButton) {
[_alertController addAction:cancelButton];
}
}
return _alertController;
}
- (void)show{
//If blackViewController is initialized
if(blankViewController){
[self.activityWindow makeKeyAndVisible];
[blankViewController presentViewController:self.alertController animated:YES completion:^{}];
}
}
-(void)hide{
[[self activityWindow] setHidden:YES];
[self setActivityWindow:nil];
}
@end