Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How do I load a url from a plist?

I've created a plist that simply lists names of websites and the url. I've tried to link it to a uiwebview with no luck (blank screen, logs say it is null). What I would ideally like to do is load the url in safari.

I'm thinking it should go on the segue and then the view did load but other than that not much clue really. The code I have is;

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([[segue identifier] isEqualToString:@"LinkWebSegue"]) {
        _myTableView = [segue destinationViewController];
        _myTableView = [NSString stringWithString:[[tableData objectAtIndex:0]objectForKey:@"url"]];


and:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *urlToLoad = [NSURL URLWithString:self.url];
    [self.webView loadRequest:[NSURLRequest requestWithURL:urlToLoad]];

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

Thanks in advance :o)

4 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

I'm not sure what the above code is supposed to do. Maybe you can share the entire code?

Other than that this line of code looks funky:

_myTableView = [segue destinationViewController];
_myTableView = [NSString stringWithString:[[tableData objectAtIndex:0]objectForKey:@"url"]];

The first line of code is setting the destinationViewController to _myTableView but then in the second line of code you are reassigning _myTableView to a url string?

yeah, stackoverflow said it worked, but not for me, I am getting a nil return. Will load up all the code. Changed slightly as I have been experimenting. Log results are: 2013-02-06 09:28:42.009 SugarFreePlus[5574:c07] url = (null)

LinksTVC.h

//
//  LinksTVC.h
//  SugarFreePlus
//
//  Created by Christina Cherry on 4/02/13.
//  Copyright (c) 2013 Christina Cherry. All rights reserved.
//

@class LinkWebVC;
#import <UIKit/UIKit.h>

@interface LinksTVC : UITableViewController<UITableViewDelegate, UITableViewDataSource>
{

    IBOutlet UITableView *myTableView;
    NSArray *tableData;
}
@property (nonatomic, retain)IBOutlet UITableView *myTableView;

@end

LinksTVC.m

//
//  LinksTVC.m
//  SugarFreePlus
//
//  Created by Christina Cherry on 4/02/13.
//  Copyright (c) 2013 Christina Cherry. All rights reserved.
//

#import "LinksTVC.h"
#import "LinkWebVC.h"

@interface LinksTVC ()

@end

@implementation LinksTVC

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSString *path = [[NSBundle mainBundle] pathForResource:@"websites" ofType:@"plist"];

    tableData = [[NSArray alloc] initWithContentsOfFile:path];


}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableData count];
}


-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"linkstablecell"];

if( cell == nil )
    {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"linkstablecell"];
        }

//Set Data For each Cell
cell.textLabel.text = [[tableData objectAtIndex:indexPath.row]objectForKey:@"name"];
cell.detailTextLabel.text = [[tableData objectAtIndex:indexPath.row]objectForKey:@"url"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

  //  NSLog(@"Source Controller = %@", [segue sourceViewController]);
  //  NSLog(@"Destination Controller = %@", [segue destinationViewController]);
  //  NSLog(@"Segue Identifier = %@", [segue identifier]);

    if ([[segue identifier] isEqualToString:@"LinkWebSegue"])
    {
        LinkWebVC *viewController = [segue destinationViewController];

     viewController = [NSString stringWithString:[[tableData objectAtIndex:0]objectForKey:@"url"]];

    }

    }






#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end



LinksWebVC.h


//
//  LinkWebVC.h
//  SugarFreePlus
//
//  Created by Christina Cherry on 4/02/13.
//  Copyright (c) 2013 Christina Cherry. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LinkWebVC : UIViewController

@property (nonatomic, copy) NSString *url;
@property (nonatomic, strong) UIWebView *webView;

@end

LinksWebVC.m

//
//  LinkWebVC.m
//  SugarFreePlus
//
//  Created by Christina Cherry on 4/02/13.
//  Copyright (c) 2013 Christina Cherry. All rights reserved.
//

#import "LinkWebVC.h"

@interface LinkWebVC ()

@end

@implementation LinkWebVC


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    NSURL *urlToLoad = [NSURL URLWithString:self.url];
    [self.webView loadRequest:[NSURLRequest requestWithURL:urlToLoad]];
NSLog(@"url = %@",urlToLoad);


    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

@Christina

This following line in your prepareForSegue is problematic:

viewController = [NSString stringWithString:[[tableData objectAtIndex:0]objectForKey:@"url"]];

You should be setting the url property of the viewController:

viewController.url = [NSString stringWithString:[[tableData objectAtIndex:0]objectForKey:@"url"]];

Ohhhh - as simple as that? omg thanks HEAPS Amit!!

Edit - plus just realised I forgot to create the IBOutlet for web view - d'oh!

Working beautifully now - bit more information and I will have my third app ready for the app store!