本文按三部分来写,
第一部分总结下该项目用到的关于 appdelegate 的 方法和涉及的一些功能。
第二部分主要是用于介绍下,没有涉及的方法和功能。
第三部分,扩展一下,用到的和没用到的功能一个部分补充。
第一部分:
先看一下总共用到了appdelegate里的哪些方法
1.程序载入后
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//设置状态栏颜色UIStatusBarStyleDefault = 0黑色文字,浅色背景时使UIStatusBarStyleLightContent = 1白色文字,深色背景时使用
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContentanimated:NO];
//设置配置IQKeyboard管理者
[selfsetupIQKeyboardManager];
//设置配置分享软件开发包Software Development Kit
[selfsetupShareSDK];
//设置jpush推送
[selfsetupJPush:launchOptions];
//请求服务器时间,和本地进行矫正
[selfreqSeverTime];
//设置根视图
[selfsetupWindowRootViewController];
//设置崩溃日志记录bugly software dvelopment kit.
[BuglystartWithAppId:@"XXXXXXX"];
returnYES;
}
2.applicationDidBecomeActive 应用程序挂起、复原与终止. 英语字面意思是应用程序变得活跃时, 这个方法,主要是为了需求,给服务器一次记录,每次都请求一次接口
- (void)applicationDidBecomeActive:(UIApplication*)application {
[[APIClientsharedManager]netReqNotify:^(NSDictionary*responseDic) {
NSLog(@"激活唤醒:%@",responseDic);
}failure:^(NSError*error) {
NSLog(@"激活唤醒:error %@",error);
}];
}
}
3.关于通知,推送
//Tells the delegate that the app successfully registered with Apple Push Notification service (APNs).
/ /告诉委托应用成功注册苹果推送通知服务(apn)。
- (void)application:(UIApplication*)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
/// Required -注册DeviceToken
[JPUSHServiceregisterDeviceToken:deviceToken];
}
//Called when your app has received a remote notification.
/ /时调用应用程序远程通知已收到。
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
// Required,For systems with less than or equal to iOS6
[JPUSHServicehandleRemoteNotification:userInfo];
}
接收远程通知
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {
NSIntegerbadge = [UIApplicationsharedApplication].applicationIconBadgeNumber;
if(badge >0) {
[[UIApplicationsharedApplication]setApplicationIconBadgeNumber:badge-1];
[JPUSHServicesetBadge:badge-1];
}
// IOS 7 Support Required
[JPUSHServicehandleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
//4.关于finish 里的 setup 方法
这个管理文本框键盘 第三方代码,很不错,我这里的版本有点旧
- (void)setupIQKeyboardManager
{
IQKeyboardManager*manager = [IQKeyboardManagersharedManager];
manager.enable=YES;
manager.shouldResignOnTouchOutside=YES;
manager.shouldToolbarUsesTextFieldTintColor=YES;
manager.enableAutoToolbar=NO;
manager.toolbarManageBehaviour=IQAutoToolbarBySubviews;
}
这里的22 23 24 是分享平台的缩写,具体看文档。
- (void)setupShareSDK
{
[ShareSDKregisterApp:@"XX"
activePlatforms:@[
//@(SSDKPlatformTypeSinaWeibo),
@(22),
@(23),
@(24),
@(6)
]
onImport:^(SSDKPlatformTypeplatformType)
{
switch(platformType)
{
caseSSDKPlatformTypeWechat:
[ShareSDKConnectorconnectWeChat:[WXApiclass]];
break;
caseSSDKPlatformTypeQQ:
[ShareSDKConnectorconnectQQ:[QQApiInterfaceclass]tencentOAuthClass:[TencentOAuthclass]];
break;
default:
break;
}
}
onConfiguration:^(SSDKPlatformTypeplatformType,NSMutableDictionary*appInfo)
{
switch(platformType)
{
caseSSDKPlatformTypeWechat:
[appInfoSSDKSetupWeChatByAppId:@"XXXX"
appSecret:@"XXX"];
break;
//appid转十六进制然后设置url
caseSSDKPlatformTypeQQ:
[appInfoSSDKSetupQQByAppId:@"XX"
appKey:@"XX"
authType:SSDKAuthTypeBoth];
break;
default:
break;
}
}];
}
- (void)setupJPush:(NSDictionary*)launchOptions
{
//推送
NSString*advertisingId = [[[ASIdentifierManagersharedManager]advertisingIdentifier]UUIDString];
if([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0) {
//可以添加自定义categories
[JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge|
UIUserNotificationTypeSound|
UIUserNotificationTypeAlert)
categories:nil];
}else{
//categories必须为nil
[JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|
UIRemoteNotificationTypeSound|
UIRemoteNotificationTypeAlert)
categories:nil];
}
//如不需要使用IDFA,advertisingIdentifier可为nil
[JPUSHServicesetupWithOption:launchOptionsappKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:advertisingId];
//消掉icon右上角badge通知数字
[UIApplicationsharedApplication].applicationIconBadgeNumber=0;
}
//这部分值得说一下,就是这里要获取 服务器的时间 和自己手机的时间 去做一个差值的计算。 拿 服务器减本地时间获得这个差值,然后有了这个差值以后,以后每次发送请求的时候,就拿这个差值去计算
//差值和本地时间相加
NSTimeIntervalretest =[zzDataManagerinstance].commonModel.cahzhiTime+ LocalTime;
然后把这个时间发给服务器,服务器去看这个时间的,如果这个时间相差太远,就请求失效。
主要的目的是为了 防止用户修改手机的时间,去做一些不好的操作!
- (void)reqSeverTime
{
[[APIClientsharedManager]netWorkGetSysTime:^(NSDictionary*response) {
NSTimeIntervalseverTime =[response[@"sysTime"]longValue];
//本地时间
NSTimeIntervalLocalTime = ((long)[[NSDatedate]timeIntervalSince1970] *1000);
//服务器时间减去本地时间
[zzDataManagerinstance].commonModel.cahzhiTime=severTime - LocalTime;
NSLog(@"responseTime:%@ commonModel.severTime %f",response,[zzDataManagerinstance].commonModel.severTime);
}failure:^(NSError*error) {
NSLog(@"error commonModel.severTime %@",error);
}];
}
- (void)setupWindowRootViewController
{
if([[NSUserDefaultsstandardUserDefaults]objectForKey:@"XXX"])
{
LYCTabBarController*lycTabbarController = [[LYCTabBarControlleralloc]init];
self.window.rootViewController= lycTabbarController;
}else{
//LYCLoginViewController *loginVC = [[LYCLoginViewController alloc] init];
LYCLoginViewController*loginVC = [[LYCLoginViewControlleralloc]init];
UINavigationController*loginNavVC = [[UINavigationControlleralloc]initWithRootViewController:loginVC];
self.window.rootViewController= loginNavVC;
}
}