Matthew Evans Matthew Evans - 3 months ago 21
iOS Question

How to make viewDidAppear load after view appears in iOS?

I am trying to get a label to load text from a URL so it can be updated from a server without updating the actual app.

Originally I used the viewDidLoad method, however this loads the view too slow. I read to use the viewDidAppear method, however, it is loading in the same way. I tried to find forums that had detail on how to make it work, though couldn't find what I needed.

I also read about loading asynchronously, though I am new to coding, so I didn't really know what I was reading!

If someone could let me know how to work this out in this circumstance, that'd be great.

Thanks.

- (void)viewDidLoad {
[super viewDidLoad];

// Do any additional setup after loading the view.
}

-(void) viewDidAppear:(BOOL)animated {
NSURL *urlTermOutlookTitle = [NSURL URLWithString:@"URL that info is coming from here"];
NSString *TitleLabel = [NSString stringWithContentsOfURL:urlTermOutlookTitle encoding:NSStringEncodingConversionAllowLossy error:nil];
TermOutlookTitleLabel.text = TitleLabel;
}

Answer

Don't ever call network requests and anything that's not instant on main thread because it will freeze your app. Have a look at this answer and use it for network request instead of stringWithContentsOfURL.

It is probably slow because network request takes time. Try to download your text before you open your view controller. If this is your initial view controller, do it in App Delegate.

Also when calling viewDidAppear you must call super.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // Your code...
}

viewDidLoad will run only once on your app launch, viewDidAppear is called every time your view appear on the screen.