0

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;
}
  • just write first line viewDidAppear(true) inside viewDidLoad() or viewWillAppear() method. – Chirag Patel Aug 11 '16 at 09:46
  • What do you want? Update a uilable text from server response asynchronously? – Sofeda Aug 11 '16 at 09:55
  • @SMi - Yeah, I just haven't found a tutorial or information from searching that explained asynchronously loading information in a way that I could understand, or if I could it was for a very different function. – Matthew Evans Aug 11 '16 at 09:59
  • what is the type of your information? Json or what? – Sofeda Aug 11 '16 at 10:01

2 Answers2

0

Call [super viewDidAppear:animated]; If you only want to show the url string in lable

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

    NSURL *urlTermOutlookTitle = [NSURL URLWithString:@"URL that info is coming from here"];
    NSString *TitleLabel = [NSString stringWithContentsOfURL:urlTermOutlookTitle encoding:NSStringEncodingConversionAllowLossy error:nil];
     dispatch_async(dispatch_get_main_queue(), ^{
           TermOutlookTitleLabel.text = TitleLabel;
        }); 

}

You can always call the delegate methods directly from any other method too.

[self viewDidAppear:YES]

If you have to purse data from server

NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"URL that info is coming from here"]];
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    // optionally update the UI to say 'done'
    if (!error) {
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData: requestHandler options: NSJSONReadingMutableContainers error: &e];
        // update the UI here (and only here to the extent it depends on the json)
        dispatch_async(dispatch_get_main_queue(), ^{
           TermOutlookTitleLabel.text = TitleLabel;
        });  
    } else {
        // update the UI to indicate error
    }
}];
Sofeda
  • 1,361
  • 1
  • 13
  • 23
  • Sweet. That got the view to load, and not just show the placeholder text in the label, though, it didn't load the information from the URL. – Matthew Evans Aug 11 '16 at 09:56
0

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.

Community
  • 1
  • 1
Boris Y.
  • 4,387
  • 2
  • 32
  • 50