-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppDelegate.m
261 lines (223 loc) · 11.6 KB
/
AppDelegate.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// AppDelegate.m
// party
// 1111
// Created by guo on 13-1-13.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
#import "AppDelegate.h"
#import "LogInViewController.h"
#define alert_tag_push 10
@interface AppDelegate()
-(void)alertNotice:(NSString *)title withMSG:(NSString *)msg cancleButtonTitle:(NSString *)cancleTitle otherButtonTitle:(NSString *)otherTitle;
@end
@implementation AppDelegate
@synthesize window = _window;
@synthesize fcontroller=_fcontroller;
@synthesize scontroller=_scontroller;
@synthesize tcontroller=_tcontroller;
@synthesize focontroller=_focontroller;
- (void)dealloc
{
[_fcontroller release];
[_scontroller release];
[_tcontroller release];
[_focontroller release];
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
//或者UIStatusBarStyleDefault
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
[hostReach startNotifier];
NSMutableArray *controllers=[NSMutableArray array];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
//firstController
self.fcontroller=[[firstViewController alloc]initWithNibName:@"firstViewController" bundle:nil];
UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:self.fcontroller];
[nav1.navigationBar setBackgroundImage:[UIImage imageNamed:@"[email protected]"] forBarMetrics:UIBarMetricsDefault];
[controllers addObject:nav1];
[nav1 release];
//secondController
self.scontroller=[[secondViewController alloc]initWithNibName:@"secondViewController" bundle:nil];
UINavigationController *nav2=[[UINavigationController alloc]initWithRootViewController:self.scontroller];
[nav2.navigationBar setBackgroundImage:[UIImage imageNamed:@"[email protected]"] forBarMetrics:UIBarMetricsDefault];
[controllers addObject:nav2];
[nav2 release];
//thirdController
self.tcontroller=[[thirdViewController alloc]initWithNibName:@"thirdViewController" bundle:nil];
UINavigationController *nav3=[[UINavigationController alloc]initWithRootViewController:self.tcontroller];
[nav3.navigationBar setBackgroundImage:[UIImage imageNamed:@"[email protected]"] forBarMetrics:UIBarMetricsDefault];
[controllers addObject:nav3];
[nav3 release];
//forthController
self.focontroller=[[forthViewController alloc]initWithNibName:@"forthViewController" bundle:nil];
UINavigationController *nav4=[[UINavigationController alloc]initWithRootViewController:self.focontroller];
[nav4.navigationBar setBackgroundImage:[UIImage imageNamed:@"[email protected]"] forBarMetrics:UIBarMetricsDefault];
[controllers addObject:nav4];
[nav4 release];
tbarController=[[UITabBarController alloc]init];
tbarController.delegate=self;
tbarController.viewControllers=controllers;
tbarController.customizableViewControllers=controllers;
[[tbarController tabBar] setSelectedImageTintColor:[UIColor whiteColor]];
[[tbarController tabBar] setBackgroundColor:[UIColor whiteColor]];
[[tbarController tabBar] setBackgroundImage:[UIImage imageNamed:@"[email protected]"]];
[[tbarController tabBar] selectionIndicatorImage];
[self.window addSubview:tbarController.view];
[[tbarController tabBar] setSelectionIndicatorImage:[UIImage imageNamed:@"[email protected]"]];
[tbarController setSelectedIndex:1];
UIImage *selectedImage0 = [UIImage imageNamed:@"[email protected]"];
UIImage *unselectedImage0 = [UIImage imageNamed:@"[email protected]"];
UITabBar *tabBar0 = tbarController.tabBar;
UITabBarItem *item0 = [tabBar0.items objectAtIndex:0];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
UIImage *selectedImage1 = [UIImage imageNamed:@"[email protected]"];
UIImage *unselectedImage1 = [UIImage imageNamed:@"[email protected]"];
UITabBar *tabBar1 = tbarController.tabBar;
UITabBarItem *item1 = [tabBar1.items objectAtIndex:1];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
UIImage *selectedImage2 = [UIImage imageNamed:@"[email protected]"];
UIImage *unselectedImage2 = [UIImage imageNamed:@"[email protected]"];
UITabBar *tabBar2 = tbarController.tabBar;
UITabBarItem *item2 = [tabBar2.items objectAtIndex:2];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
UIImage *selectedImage3 = [UIImage imageNamed:@"[email protected]"];
UIImage *unselectedImage3 = [UIImage imageNamed:@"[email protected]"];
UITabBar *tabBar3 = tbarController.tabBar;
UITabBarItem *item3 = [tabBar3.items objectAtIndex:3];
[item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir=[path objectAtIndex:0];
NSString *imagePath=[docDir stringByAppendingPathComponent:@"Guo.txt"];
NSMutableArray *stringmutable=[NSMutableArray arrayWithContentsOfFile:imagePath];
NSLog(@"ffffffffffffffffffffff%@",stringmutable);
if (stringmutable==nil)
{
LogInViewController *login=[[LogInViewController alloc]init];
[self.window addSubview:login.view];
}
[self.window makeKeyAndVisible];
/** 注册推送通知功能, */
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
//判断程序是不是由推送服务完成的
if (launchOptions) {
NSDictionary* pushNotificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushNotificationKey) {
[self alertNotice:@"" withMSG:@"这是通过推送窗口启动的程序,你可以在这里处理推送内容" cancleButtonTitle:@"知道了" otherButtonTitle:nil];
}
}
return YES;
}
- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];
if (status == NotReachable) {
UIAlertView *soundAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[soundAlert show];
[soundAlert release];
}
}
-(void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
{
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//点击提示框的打开
application.applicationIconBadgeNumber = 0;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
//当程序还在后天运行
application.applicationIconBadgeNumber = 0;
}
#pragma mark - 实现远程推送需要实现的监听接口
/** 接收从苹果服务器返回的唯一的设备token,该token需要发送回推送服务器*/
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString* token = [NSString stringWithFormat:@"%@",deviceToken];
NSLog(@"apns -> 生成的devToken:%@", token);
// [self alertNotice:@"" withMSG:[NSString stringWithFormat:@"从苹果推送服务器返回的设备标识:%@",deviceToken] cancleButtonTitle:@"Ok" otherButtonTitle:@""];
DeviceSender* sender = [[DeviceSender alloc]initWithDelegate:self ];
[sender sendDeviceToPushServer:token ];
}
/** 接收注册推送通知功能时出现的错误,并做相关处理*/
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"apns -> 注册推送功能时发生错误, 错误信息:\n %@", err);
// [self alertNotice:@"注册推送功能时发生错误" withMSG:[err localizedDescription] cancleButtonTitle:@"Ok" otherButtonTitle:@""];
}
//程序处于启动状态,或者在后台运行时,会接收到推送消息,解析处理
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"\napns -> didReceiveRemoteNotification,Receive Data:\n%@", userInfo);
//把icon上的标记数字设置为0,
application.applicationIconBadgeNumber = 0;
if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"**推送消息**"
message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
delegate:self
cancelButtonTitle:@"关闭"
otherButtonTitles:@"处理推送内容",nil];
alert.tag = alert_tag_push;
[alert show];
}
}
#pragma mark - 处理推送服务器push过来的数据
-(void) pushAlertButtonClicked:(NSInteger)buttonIndex
{
NSLog(@"响应推送对话框");
if (buttonIndex == 0) {
NSLog(@"--->点了第一个按钮");
} else {
NSLog(@"--->点了第二个按钮");
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (alertView.tag) {
case alert_tag_push:
{
[self pushAlertButtonClicked:buttonIndex];
}
break;
default:
break;
}
}
-(void)alertNotice:(NSString *)title withMSG:(NSString *)msg cancleButtonTitle:(NSString *)cancleTitle otherButtonTitle:(NSString *)otherTitle
{
UIAlertView *alert;
if(!otherTitle || [otherTitle isEqualToString:@""])
alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:nil,nil];
else
alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:otherTitle,nil];
[alert show];
[alert release];
}
#pragma mark - 实现代理接口:DeviceSenderDelegate
- (void)didSendDeviceFailed:(DeviceSender *)sender withError:(NSError *)error
{
NSLog(@"apns -> 发送设备标识到服务器失败:%@", error);
// [self alertNotice:@"错误" withMSG:@"发送设备标识到服务器失败" cancleButtonTitle:@"确定" otherButtonTitle:nil ];
[sender release ];
}
- (void)didSendDeviceSuccess:(DeviceSender *)sender
{
NSLog(@"apns -> 设备标识已发送到服务器");
// [self alertNotice:@"" withMSG:@"设备标识已发送到服务器" cancleButtonTitle:@"确定" otherButtonTitle:nil ];
[sender release];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
//进入后台时要进行的处理
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
//进入前台时要进行的处理
}
@end