• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

当网络连接不佳时,iOS 应用程序在启动时会挂起一段时间

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:48:08 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个大问题,我正在地铁中测试我的应用程序,当我在一个 super 糟糕的覆盖站中时,我的应用程序在启动应用程序的第一个屏幕上卡住了一段时间(如 30 秒或 1 分钟) cero,然后一切都开始像魅力一样工作。

我有 2 个函数负责在我的 WS 中查找信息(响应是 JSON),并且我已将此 NSURLRequest 的时间设置为 2.5 秒,所以如果服务器有点慢我为什么要查找存储在 iPhone 中而不是服务器中的 JSON 文件。

如果我在飞行模式下测试我的应用程序,我的代码就像一个魅力,一切都从设备中读取,但如果我的覆盖率非常低或者我模拟了它,应用程序卡住,然后像在飞机上一样工作模式。

如果我在 Xcode 中添加断点,一切都会像往常一样执行(我的意思是快速),最后调用的函数是 viewWillAppear:.

有没有人有类似的问题?或者有人知道会发生什么吗?

任何想法将不胜感激。

提前致谢。

这是我用来获取 JSON 的函数:

-(NSData *)getMainMenuJsonData{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([[defaults objectForKey"OnLine"] boolValue] ) {
        NSString *urlAsString = [NSString stringWithFormat"%@%@/%@/%@",[configs valueForKey"wsURL"], [configs valueForKey"clientToken"], [configs valueForKey"appToken"], [CommonsUtils getCommonUtil].getAppLanguage];
        NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlAsString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:2.5];
        NSURLResponse * response = nil;
        NSError * error = nil;
        NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

        plistPath = [[NSBundle mainBundle] pathForResource"Config" ofType"plist"];
        configs = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
        NSUInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;

        if (statusCode == 200 && error == Nil) {
            if (error != Nil) {
                return Nil;
            } else {
                return data;
            }
        }
        else {
            return Nil;
        }
    }
    else {
        return Nil;
    }
}

-(NSData *)getSpecificJsonDataNSString *)itemId{
    NSString *urlAsString = [NSString stringWithFormat"%@%@/%@/%@/%@",[configs valueForKey"wsURL"], [configs valueForKey"clientToken"], [configs valueForKey:@"appToken"], [CommonsUtils getCommonUtil].getAppLanguage, itemId];
    NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlAsString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:2.5];
    NSURLResponse * response = nil;
    NSError * error = nil;
    NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

    plistPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];
    configs = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSUInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;

    if (statusCode == 200 && error == Nil) {
        if (error != Nil) {
            return Nil;
        } else {
            return data;
        }
    }
    else {
        return Nil;
    }
}

这是我主屏幕的所有代码,一旦加载并调用 viewWillAppear:,如果我触摸屏幕上的一个按钮,执行 btnAction: 函数需要一段时间(如 30 秒或 1 分钟) :

//
//  vcMainScreen.m
//  SmartHotel
//
//  Created by GoSmart on 08/07/13.
//  Copyright (c) 2013 GoSmart. All rights reserved.
//

#import "vcMainScreen.h"
#import "Util.h"
#import "Reachability.h"
#import "CommonsUtils.h"

@interface vcMainScreen ()
@property (nonatomic) Reachability *reachabilityInfo;
@end

@implementation vcMainScreen {
    NSDictionary *dValue;
    NSData *jsonData;
    NSDictionary *dConfiguration, *configs, *languages;
    Util *util;
    BOOL ok;
    NSString *plistPath, *language;
}

@synthesize tabController;

- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    _reachabilityInfo = [Reachability reachabilityWithHostname:@"www.google.com"];
    [_reachabilityInfo startNotifier];

    [super viewDidLoad];
    util = [[Util alloc] crearUtil];
    [[self navigationController] setNavigationBarHidden:YES animated:NO];

    NetworkStatus internetStatus = [_reachabilityInfo currentReachabilityStatus];
    NSData *mainData = Nil;
    if (internetStatus != NotReachable)
    mainData = [util getMainMenuJsonData];

    if (mainData != Nil) {
        jsonData = mainData;
    }
    else {
        plistPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];
        configs = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
        languages = [configs valueForKey:@"Languages"];

        for (NSString * appLanguage in [NSLocale preferredLanguages])
        {
            language = [[languages valueForKey:appLanguage] objectAtIndex:0];
            if ([language isEqual:[NSNull null]]) {
                language = [[languages valueForKey:@"default"] objectAtIndex:0];
            }
            else{
                break;
            }
        }

        NSString *jsonPath = [NSString stringWithFormat:@"%@/%@.json",[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0],language];
        jsonData = [NSData dataWithContentsOfFile:jsonPath];
    }

    dConfiguration =[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
    [self createTabBar];
    NSInteger count = 0;
    for (UIView* subView in self.view.subviews)
    {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *bCustom = (UIButton *)subView;
            [bCustom addTarget:self action:@selector(btnAction forControlEvents:UIControlEventTouchUpInside];
            count ++;
        }
    }
    dValue = [[dConfiguration objectForKey:@"BackgroundImage"] objectAtIndex:0];
    _ivBackGround.image = [UIImage imageNamed:[util getBackgroundImageName:[dValue objectForKey:@"Image"] andRetrina:[dValue objectForKey:@"ImageRetina"]]];
    self.navigationItem.title = @"Home";
}

- (void)viewWillAppearBOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];

    // Tracking view for analytics
    id tracker = [[GAI sharedInstance] defaultTracker];
    [tracker set:kGAIScreenName value:self.navigationItem.title];
    [tracker send:[[GAIDictionaryBuilder createAppView] build]];
}

- (void)viewWillDisappearBOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillDisappear:animated];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskPortrait;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)createTabBar
{   
    tabController = [self.storyboard instantiateViewControllerWithIdentifier:@"cCustomTabController"];
    dValue = [dConfiguration objectForKey:@"Buttons"];
    NSMutableArray  *aControllers = [[NSMutableArray alloc] init];
    int i = 0;
    for (NSString* sProperty in dValue) {
        NSString* d = @"Details";
        NetworkStatus internetStatus = [_reachabilityInfo currentReachabilityStatus];
        NSData *itemData = Nil;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        if (internetStatus != NotReachable && [[defaults objectForKey:@"OnLine"] boolValue])
            itemData = [util getSpecificJsonData:[sProperty valueForKeyPath:@"Item"]];
        if(itemData != nil){
            UIStoryboard *aStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
            UIViewController *vcCustom = [aStoryboard instantiateViewControllerWithIdentifier:[util getControllerName:[sProperty valueForKeyPath:@"ViewController"]]];
            [vcCustom setValue:itemData forKey:@"JsonData"];
            [vcCustom setValue:[sProperty valueForKeyPath:@"Item"] forKey:@"Item"];
            [vcCustom setValue:d forKey:@"Details"];
            [util saveJSON:itemData withName:[NSString stringWithFormat:@"%@%@",[sProperty valueForKeyPath:@"Item"],[CommonsUtils getCommonUtil].getAppLanguage]];
            [[vcCustom navigationController] setNavigationBarHidden:NO animated:NO];
            vcCustom.navigationItem.leftBarButtonItem = Nil;
            vcCustom.navigationItem.hidesBackButton = YES;
            UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
            UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
            UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:[sProperty valueForKeyPath:@"Title"] image:imageBtn selectedImage:imageBtnPress];
            UIImage * iSelected = imageBtnPress;
            iSelected = [iSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [tab setSelectedImage:iSelected];
            tab.tag = i;
            if([[sProperty valueForKeyPath:@"Title"] isEqualToString:@"Notificaciones"])
                tab.badgeValue=[sProperty valueForKeyPath:@"Badge"];
            [vcCustom setTabBarItem:tab];
            [vcCustom setTitle:[sProperty valueForKeyPath:@"Title"]];
            UINavigationController *navigationController = [[cCustomNavigationController alloc] initWithRootViewController:vcCustom];
            navigationController.navigationBar.tintColor = [UIColor colorWithRed:36.0/255.0 green:134.0/255.0 blue:232.0/255.0 alpha:1];
            [aControllers insertObject:navigationController atIndex:i];
            i++;
        }
        else
        {
            UIStoryboard *aStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
            UIViewController *vcCustom = [aStoryboard instantiateViewControllerWithIdentifier:[util getControllerName:[sProperty valueForKeyPath:@"ViewController"]]];
            NSNumber *val = [NSNumber numberWithInteger:i];
            NSString *nextJson = [sProperty valueForKeyPath:@"JsonConfigFile"];
            [vcCustom setValue:[sProperty valueForKeyPath:@"Item"] forKey:@"Item"];
            [vcCustom setValue:nextJson forKey:@"JsonConfigFile"];
            [vcCustom setValue:val forKey:@"MenuButtonSelectedTag"];
            [[vcCustom navigationController] setNavigationBarHidden:NO animated:NO];
            vcCustom.navigationItem.leftBarButtonItem = Nil;
            vcCustom.navigationItem.hidesBackButton = YES;
            UIImage *imageBtn = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]]];
            UIImage *imageBtnPress = [UIImage imageNamed:[util getImageName:[sProperty valueForKeyPath:@"ImageHeighlighted"] andRetrina:[sProperty valueForKeyPath:@"ImageRetinaHeighlighted"]]];
            UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:[sProperty valueForKeyPath:@"Title"] image:imageBtn selectedImage:imageBtnPress];
            UIImage * iSelected = imageBtnPress;
            iSelected = [iSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [tab setSelectedImage:iSelected];
            tab.tag = i;
            if([[sProperty valueForKeyPath:@"Title"] isEqualToString:@"Notificaciones"])
                tab.badgeValue=[sProperty valueForKeyPath:@"Badge"];
            [vcCustom setTabBarItem:tab];
            [vcCustom setTitle:[sProperty valueForKeyPath:@"Title"]];
            UINavigationController *navigationController = [[cCustomNavigationController alloc] initWithRootViewController:vcCustom];
            navigationController.navigationBar.tintColor = [UIColor colorWithRed:36.0/255.0 green:134.0/255.0 blue:232.0/255.0 alpha:1];
            [aControllers insertObject:navigationController atIndex:i];
            i++;
        }


    }
    tabController.delegate = self;
    tabController.viewControllers = aControllers;
    tabController.tabBar.tintColor = [UIColor blackColor];
}

-(BOOL)tabBarControllerUITabBarController *)tabBarController shouldSelectViewControllerUIViewController *)viewController{
    return YES;
}

- (IBAction)btnActionid)sender {
    UIButton *bCustom = (UIButton *)sender;

    tabController.selectedIndex = bCustom.tag;

    id<GAITracker> tracker= [[GAI sharedInstance] defaultTracker];

    [tracker send:[[GAIDictionaryBuilder createEventWithCategory: @"ui_button"
                                                          action: @"button_press"
                                                           label: [NSString stringWithFormat:@"%@-%@", self.title, [[tabController.viewControllers objectAtIndex:bCustom.tag] title]]
                                                           value: nil] build]];

    [self.navigationController pushViewController:tabController animated:YES];
}

@end

这是我 iPhone 中的设置:

Network settings



Best Answer-推荐答案


对此没有正确答案,由于@Guillaume Algis 和 this我能够找到并解决我的问题。

一如既往地感谢您的帮助。

关于当网络连接不佳时,iOS 应用程序在启动时会挂起一段时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28067080/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap