| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| #import "AppDelegate.h" |
| #import "WXDemoViewController.h" |
| #import "UIViewController+WXDemoNaviBar.h" |
| #import "WXEventModule.h" |
| #import "WXImgLoaderDefaultImpl.h" |
| #import "DemoDefine.h" |
| #import "WXScannerVC.h" |
| #import "WXScannerHistoryVC.h" |
| #import "WXSyncTestModule.h" |
| #import "WXExtModule.h" |
| #import "UIView+UIThreadCheck.h" |
| #import <WeexSDK/WeexSDK.h> |
| #import <AVFoundation/AVFoundation.h> |
| #import <ATSDK/ATManager.h> |
| #import "WXConfigCenterProtocol.h" |
| #import "WXConfigCenterDefaultImpl.h" |
| #import "WXNavigationHandlerImpl.h" |
| //#import "WXAnalyzerCenter.h" |
| #import "WXApmGeneratorImpl.h" |
| #import "WXWebSocketDefaultImpl.h" |
| #import <WeexPluginLoader/WeexPluginLoader.h> |
| |
| #ifdef DEBUG |
| #import "DebugAnalyzer.h" |
| #endif |
| |
| @interface AppDelegate () |
| @end |
| |
| @implementation AppDelegate |
| |
| #pragma mark |
| #pragma mark application |
| |
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| { |
| self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; |
| self.window.backgroundColor = [UIColor whiteColor]; |
| |
| [self initWeexSDK]; |
| |
| self.window.rootViewController = [[WXRootViewController alloc] initWithRootViewController:[self demoController]]; |
| [self.window makeKeyAndVisible]; |
| |
| [self startSplashScreen]; |
| |
| #if DEBUG |
| // check if there are any UI changes on main thread. |
| [UIView wx_checkUIThread]; |
| #endif |
| |
| return YES; |
| } |
| |
| - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window |
| { |
| if (_allowRotation) { |
| return UIInterfaceOrientationMaskAllButUpsideDown; |
| } |
| else { |
| return UIInterfaceOrientationMaskPortrait; |
| } |
| } |
| |
| -(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler |
| { |
| if ([shortcutItem.type isEqualToString:QRSCAN]) { |
| WXScannerVC * scanViewController = [[WXScannerVC alloc] init]; |
| [(WXRootViewController*)self.window.rootViewController pushViewController:scanViewController animated:YES]; |
| } |
| if ([shortcutItem.type isEqualToString:QRSCAN_HISTORY]) { |
| WXScannerHistoryVC *scannerHistoryVC = [WXScannerHistoryVC new]; |
| [(WXRootViewController*)self.window.rootViewController pushViewController:scannerHistoryVC animated:YES]; |
| } |
| } |
| |
| - (void)applicationDidEnterBackground:(UIApplication *)application |
| { |
| |
| #ifdef UITEST |
| #if !TARGET_IPHONE_SIMULATOR |
| NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
| NSString *documentsDirectory = [paths objectAtIndex:0]; |
| setenv("GCOV_PREFIX", [documentsDirectory cStringUsingEncoding:NSUTF8StringEncoding], 1); |
| setenv("GCOV_PREFIX_STRIP", "6", 1); |
| #endif |
| extern void __gcov_flush(void); |
| __gcov_flush(); |
| #endif |
| } |
| |
| - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url |
| { |
| NSString *newUrlStr = url.absoluteString; |
| if([url.scheme isEqualToString:@"wxpage"]) { |
| newUrlStr = [newUrlStr stringByReplacingOccurrencesOfString:@"wxpage://" withString:@"http://"]; |
| } |
| UIViewController * viewController = [self demoController]; |
| ((WXDemoViewController*)viewController).url = [NSURL URLWithString:newUrlStr]; |
| [(WXRootViewController*)self.window.rootViewController pushViewController:viewController animated:YES]; |
| return YES; |
| } |
| |
| #pragma mark weex |
| - (void)initWeexSDK |
| { |
| [WXAppConfiguration setAppGroup:@"AliApp"]; |
| [WXAppConfiguration setAppName:@"WeexDemo"]; |
| [WXAppConfiguration setExternalUserAgent:@"ExternalUA"]; |
| |
| [WXSDKEngine initSDKEnvironment]; |
| |
| [WXSDKEngine registerHandler:[WXImgLoaderDefaultImpl new] withProtocol:@protocol(WXImgLoaderProtocol)]; |
| [WXSDKEngine registerHandler:[WXEventModule new] withProtocol:@protocol(WXEventModuleProtocol)]; |
| [WXSDKEngine registerHandler:[WXConfigCenterDefaultImpl new] withProtocol:@protocol(WXConfigCenterProtocol)]; |
| [WXSDKEngine registerHandler:[WXNavigationHandlerImpl new] withProtocol:@protocol(WXNavigationProtocol)]; |
| [WXSDKEngine registerHandler:[WXApmGeneratorImpl new] withProtocol:@protocol(WXApmGeneratorProtocol)]; |
| [WXSDKEngine registerHandler:[WXWebSocketDefaultImpl new] withProtocol:@protocol(WXWebSocketHandler)]; |
| |
| [WXSDKEngine registerComponent:@"select" withClass:NSClassFromString(@"WXSelectComponent")]; |
| [WXSDKEngine registerModule:@"event" withClass:[WXEventModule class]]; |
| [WXSDKEngine registerModule:@"syncTest" withClass:[WXSyncTestModule class]]; |
| [WXSDKEngine registerModule:@"titleBar" withClass:NSClassFromString(@"WXTitleBarModule")]; |
| [WXSDKEngine registerExtendCallNative:@"test" withClass:NSClassFromString(@"WXExtendCallNativeTest")]; |
| [WXSDKEngine registerModule:@"ext" withClass:[WXExtModule class]]; |
| [WPRegister registerPlugins]; |
| #ifdef DEBUG |
| [WXAnalyzerCenter addWxAnalyzer:[DebugAnalyzer new]]; |
| #endif |
| |
| #if !(TARGET_IPHONE_SIMULATOR) |
| [self checkUpdate]; |
| #endif |
| |
| #ifdef DEBUG |
| [self atAddPlugin]; |
| [WXDebugTool setDebug:YES]; |
| [WXLog setLogLevel:WXLogLevelLog]; |
| |
| #ifndef UITEST |
| [[ATManager shareInstance] show]; |
| #endif |
| #else |
| [WXDebugTool setDebug:NO]; |
| [WXLog setLogLevel:WXLogLevelError]; |
| #endif |
| } |
| |
| - (UIViewController *)demoController |
| { |
| UIViewController *demo = [[WXDemoViewController alloc] init]; |
| ((WXDemoViewController *)demo).url = [NSURL URLWithString:BUNDLE_URL]; |
| return demo; |
| } |
| |
| #pragma mark |
| #pragma mark animation when startup |
| |
| - (void)startSplashScreen |
| { |
| UIView* splashView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
| splashView.backgroundColor = WEEX_COLOR; |
| |
| UIImageView *iconImageView = [UIImageView new]; |
| UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weex-icon" ofType:@"png"]]; |
| if ([icon respondsToSelector:@selector(imageWithRenderingMode:)]) { |
| iconImageView.image = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; |
| iconImageView.tintColor = [UIColor whiteColor]; |
| } else { |
| iconImageView.image = icon; |
| } |
| iconImageView.frame = CGRectMake(0, 0, 320, 320); |
| iconImageView.contentMode = UIViewContentModeScaleAspectFit; |
| iconImageView.center = splashView.center; |
| [splashView addSubview:iconImageView]; |
| |
| [self.window addSubview:splashView]; |
| |
| float animationDuration = 1.4; |
| CGFloat shrinkDuration = animationDuration * 0.3; |
| CGFloat growDuration = animationDuration * 0.7; |
| |
| if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { |
| [UIView animateWithDuration:shrinkDuration delay:1.0 usingSpringWithDamping:0.7f initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{ |
| CGAffineTransform scaleTransform = CGAffineTransformMakeScale(0.75, 0.75); |
| iconImageView.transform = scaleTransform; |
| } completion:^(BOOL finished) { |
| [UIView animateWithDuration:growDuration animations:^{ |
| CGAffineTransform scaleTransform = CGAffineTransformMakeScale(20, 20); |
| iconImageView.transform = scaleTransform; |
| splashView.alpha = 0; |
| } completion:^(BOOL finished) { |
| [splashView removeFromSuperview]; |
| }]; |
| }]; |
| } else { |
| [UIView animateWithDuration:shrinkDuration delay:1.0 options:0 animations:^{ |
| CGAffineTransform scaleTransform = CGAffineTransformMakeScale(0.75, 0.75); |
| iconImageView.transform = scaleTransform; |
| } completion:^(BOOL finished) { |
| [UIView animateWithDuration:growDuration animations:^{ |
| CGAffineTransform scaleTransform = CGAffineTransformMakeScale(20, 20); |
| iconImageView.transform = scaleTransform; |
| splashView.alpha = 0; |
| } completion:^(BOOL finished) { |
| [splashView removeFromSuperview]; |
| }]; |
| }]; |
| } |
| } |
| |
| #pragma mark |
| |
| - (void)atAddPlugin { |
| #if DEBUG |
| [[ATManager shareInstance] addPluginWithId:@"weex" andName:@"weex" andIconName:@"../weex" andEntry:@"" andArgs:@[@""]]; |
| [[ATManager shareInstance] addSubPluginWithParentId:@"weex" andSubId:@"logger" andName:@"logger" andIconName:@"log" andEntry:@"WXATLoggerPlugin" andArgs:@[@""]]; |
| // [[ATManager shareInstance] addSubPluginWithParentId:@"weex" andSubId:@"viewHierarchy" andName:@"hierarchy" andIconName:@"log" andEntry:@"WXATViewHierarchyPlugin" andArgs:@[@""]]; |
| [[ATManager shareInstance] addSubPluginWithParentId:@"weex" andSubId:@"test2" andName:@"test" andIconName:@"at_arr_refresh" andEntry:@"" andArgs:@[]]; |
| [[ATManager shareInstance] addSubPluginWithParentId:@"weex" andSubId:@"test3" andName:@"test" andIconName:@"at_arr_refresh" andEntry:@"" andArgs:@[]]; |
| #endif |
| } |
| |
| - (void)checkUpdate { |
| __weak typeof(self) weakSelf = self; |
| dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; |
| NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"]; |
| NSString *URL = @"http://itunes.apple.com/lookup?id=1130862662"; |
| NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; |
| [request setURL:[NSURL URLWithString:URL]]; |
| [request setHTTPMethod:@"POST"]; |
| |
| NSHTTPURLResponse *urlResponse = nil; |
| NSError *error = nil; |
| NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; |
| NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding]; |
| |
| NSDictionary *dic = [WXUtility objectFromJSON:results]; |
| NSArray *infoArray = [dic objectForKey:@"results"]; |
| |
| if ([infoArray count]) { |
| NSDictionary *releaseInfo = [infoArray objectAtIndex:0]; |
| weakSelf.latestVer = [releaseInfo objectForKey:@"version"]; |
| if ([weakSelf.latestVer floatValue] > [currentVersion floatValue]) { |
| if (![[NSUserDefaults standardUserDefaults] boolForKey: weakSelf.latestVer]) { |
| [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:weakSelf.latestVer]; |
| dispatch_async(dispatch_get_main_queue(), ^{ |
| UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New Version" message:@"Will update to a new version" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"update", nil]; |
| [alert show]; |
| }); |
| } |
| } |
| } |
| }); |
| } |
| |
| - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { |
| switch (buttonIndex) { |
| case 0: |
| [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:self.latestVer]; |
| break; |
| case 1: |
| [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/weex-playground/id1130862662?mt=8"]]; |
| default: |
| break; |
| } |
| [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES]; |
| } |
| |
| @end |