Ghobs
Ghobs

Reputation: 859

website not loading in UIWebView

I've set my code up so that when a user taps a cell in the UITableView, it segues to WebViewController, and passes that cells "Item URL" property along the way. In the WebViewController class, I initialize a UIWebView, and have it load the respective URL. For some reason, it shows up blank and doesn't do any loading. How can I set my WebViewController to begin loading the webpage once it segues over?

WebViewController.h:

#import <UIKit/UIKit.h>
#import "MatchCenterViewController.h"

@interface WebViewController : UIViewController <UIWebViewDelegate>

@property (strong, nonatomic) NSURL *itemURL;
@property (strong, nonatomic) IBOutlet UIWebView *myWebView;

@end

WebViewController.m:

#import "WebViewController.h"

@interface WebViewController ()

@end

@implementation WebViewController

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


- (void)viewDidLoad
{
    [super viewDidLoad];


    _myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    _myWebView.delegate=self;
    [self.view addSubview:_myWebView];

    self.myWebView.delegate = self;  //set the delegate first before calling LoadRequest

    NSURL *url = [NSURL URLWithString:_itemURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [self.myWebView setScalesPageToFit:YES];
    [self.myWebView loadRequest:request];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

MatchCenterViewController.h:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "AsyncImageView.h"
#import "SearchViewController.h"
#import "WebViewController.h"

@interface MatchCenterViewController : UIViewController <UITableViewDataSource>

//irrelevant code hidden for conciseness

@property (strong, nonatomic) NSArray *matchCenterArray;
@property (strong, nonatomic) NSString *searchTerm;

@property (strong, nonatomic) NSURL *itemURL;

@end

MatchCenterViewController.m:

#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>

@interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *matchCenter;
@end

@implementation MatchCenterViewController


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


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
    self.matchCenter.frame = CGRectMake(0,50,320,self.view.frame.size.height-100);
    _matchCenter.dataSource = self;
    _matchCenter.delegate = self;
    [self.view addSubview:self.matchCenter];

    _matchCenterArray = [[NSArray alloc] init];
}

- (void)viewDidAppear:(BOOL)animated
{
    self.matchCenterArray = [[NSArray alloc] init];

    [PFCloud callFunctionInBackground:@"MatchCenter"
                       withParameters:@{
                                        @"test": @"Hi",
                                        }
                                block:^(NSArray *result, NSError *error) {

                                    if (!error) {
                                        _matchCenterArray = result;
                                        [_matchCenter reloadData];

                                        NSLog(@"Result: '%@'", result);
                                    }
                                }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _matchCenterArray.count;
}

//the part where i setup sections and the deleting of said sections

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 21.0f;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    //irrelevant code removed for conciseness

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Initialize cell
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        // if no cell could be dequeued create a new one
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    //irrelevant code removed for conciseness

    return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSURL *itemURL = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"Item URL"];

    [self performSegueWithIdentifier:@"WebViewSegue" sender:self];
}


- (void)deleteButtonPressed:(id)sender
{
    // links button
    UIButton *deleteButton = (UIButton *)sender;

    // Define the sections title
    NSString *sectionName = _searchTerm = [[[[_matchCenterArray  objectAtIndex:deleteButton.tag] objectForKey:@"Top 3"] objectAtIndex:3]objectForKey:@"Search Term"];

    // Run delete function with respective section header as parameter
    [PFCloud callFunctionInBackground:@"deleteFromMatchCenter"
                       withParameters:
                      @{@"searchTerm": sectionName,}
                                block:^(NSDictionary *result, NSError *error) {
                                   if (!error) {
                                       [PFCloud callFunctionInBackground:@"MatchCenter"
                                                          withParameters:@{
                                                                           @"test": @"Hi",
                                                                           }
                                                                   block:^(NSArray *result, NSError *error) {

                                                                       if (!error) {
                                                                           _matchCenterArray = result;
                                                                           [_matchCenter reloadData];

                                                                           NSLog(@"Result: '%@'", result);
                                                                       }
                                                                   }];

                                   }
                                }];
}

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

 #pragma mark - Navigation

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
     WebViewController *controller = (WebViewController *) segue.destinationViewController;
     controller.itemURL = self.itemURL;   
 }


@end

Upvotes: 0

Views: 218

Answers (3)

Jamal Zafar
Jamal Zafar

Reputation: 2189

This line in your code indicates that you have a WebView in your Xib file and you have an outlet attached to it:

@property (strong, nonatomic) IBOutlet UIWebView *myWebView;

So When you already have a webView defined in your Xib file, why are you creating another UIWebView and assigning it to the IBOutlet's webView? There is no point in allocating a new webView and adding it as subview. That is, you should discard these lines:

_myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

_myWebView.delegate=self;

[self.view addSubview:_myWebView];

The reason is that when you already have a WebView in Xib file then you don't need another instance of it. Simply do:

      self.myWebView.delegate = self;  

      NSURL *url = [NSURL URLWithString: itemURL];  //itemURL must be a NSString. If it is a NSURL, then you should skip this line and put itemURL in the next line instead of "url"

      NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

      [self.myWebView setScalesPageToFit:YES];         

      [self.myWebView loadRequest:request];

Upvotes: 1

TotoroTotoro
TotoroTotoro

Reputation: 17622

There's a bug in your code:

NSURL *url = [NSURL URLWithString:_itemURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

_itemURL is an NSURL object, not a NSString. You should just be able to do

NSURLRequest *request = [NSURLRequest requestWithURL:_itemURL];

provided _itemURL is set.

Upvotes: 1

userx
userx

Reputation: 1121

In the viewDidLoad method of your WebViewController, add this code:

-(void)viewDidLoad {

    [super viewDidLoad];

    UIWebView* webView = [UIWebView alloc] initWithFrame:self.view.frame];

    [self.view addSubView:webView];
NSURLRequest* request = [NSURLRequest requestWithURL:self.itemURL; cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];

    [webView loadRequest:request];

}

Upvotes: 0

Related Questions