uiwebview problem

Discussion in 'iOS Programming' started by sagarshivam, Sep 8, 2011.

  1. sagarshivam macrumors member

    Joined:
    May 24, 2011
    #1
    Dear All

    I am facing some problem with uiwebview. In my split view based application, on tapping cell of left panel (uitableview) , corresponding file (located on server) should be viewed on detail view (right pane of uisplitview)

    part of Maintable.m is:

    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        detailViewController.detailItem = [file_list_for_subproject objectAtIndex:indexPath.row];
    	
    }

    detailviewcontroller.h is :

    Code:
    
    @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate> {
        
        UIPopoverController *popoverController;
        UIToolbar *toolbar;
        UIWebView *file_view;
        id detailItem;
        UILabel *detailDescriptionLabel;
    	IBOutlet UIImageView *imgview;
    }
    
    @property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
    
    @property (nonatomic, retain) id detailItem;
    @property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;
    @property (nonatomic,retain) UIWebView *file_view;
    @property (nonatomic ,retain) UIImageView *imgview;
    @end
    

    and part of detailviewcontroller.m is:

    Code:
    - (void)setDetailItem:(id)newDetailItem {
        if (detailItem != newDetailItem) {
            [detailItem release];
            detailItem = [newDetailItem retain];
            NSString *fname1= [NSString stringWithFormat:@"%@",detailItem];
    	NSLog(@"file name passed is %@",fname1);
            //fname is getting displayed properly
            [self configureView];
    		
        }
    
        if (popoverController != nil) {
            [popoverController dismissPopoverAnimated:YES];
        }        
    }
    
    
    - (void)configureView {
      
        detailDescriptionLabel.text = [detailItem description];   
    	//CGRect frame= [[UIScreen mainScreen] applicationFrame];
    	//UIWebView *webview = [[UIWebView alloc] initWithFrame:frame];
    	//self.file_view.backgroundColor = [UIColor redColor];
    	NSURL *urpath = [NSURL URLWithString:@"http://172.22.79.169/~spp/Cref.pdf"];
    	NSURLRequest *req = [NSURLRequest requestWithURL:urpath];
    	[self.file_view loadRequest:req];
    	[self.view addSubview:file_view];
    	[self.file_view release];
    }
    
    
    But pdf is not getting displayed. Where am I making mistake?
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
    Where is file_view initialized?

    Have you used NSLog's (or breakpoints) to explore and understand your program flow?

    Good luck :)
     
  3. sagarshivam thread starter macrumors member

    Joined:
    May 24, 2011
    #3
    Left panel is 3 level deep and each level is uitabletype. So on tapping row of first table, second table is pushed and similarly on tapping cell of second table , third table is pushed (which contains a list of files located on local server, ex: http://172.22.79.169/~spp/Cref.pdf .These files are absolutely accessible)

    Till the time, third table is pushed, detail view shows activityindicator and it keeps on moving. It works fine.

    Now , on tapping third table's cell, webview should be loaded in detail view and corresponding tapped file (variable detailItem is every time assigned with the URL of file) should be loaded in uiwebview. And the problem is that uiwebview is not getting loaded and file is not viewed and finally activityindicator keeps on moving.

    I am sure that the problem is related to uiwebview and I am not using IB to make either of interface.

    Codes are :

    for last level table (I am not posting code for other tables as everything related to them are proper functioning.), code is:

    SubTable.h

    Code:
    #import <UIKit/UIKit.h>
    @class DetailViewController;
    @interface SubTable : UITableViewController {
    	NSArray *file_list_for_subproject;
    	DetailViewController *detailViewController;
    	
    }
    
    @property (nonatomic, retain) NSArray *file_list_for_subproject;
    @property (nonatomic,retain) DetailViewController *detailViewController;
    @end
    
    SubTable.m
    Code:
    #import "SubTable.h"
    #import "DetailViewController.h"
    #import "SecondLevelViewController.h"
    
    @implementation SubTable
    
    @synthesize file_list_for_subproject,detailViewController;
    - (void)viewDidLoad {
        [super viewDidLoad];
    	detailViewController = [[DetailViewController alloc] init];
    	self.clearsSelectionOnViewWillAppear = NO;
    	self.navigationItem.rightBarButtonItem = self.editButtonItem;
         }
    
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
    }
    
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     
        return [file_list_for_subproject count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"any-cell"];
    	UILabel *labelView = NULL;
    	if (cell == nil) {
    		
    		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"any-cell"] autorelease];
    		cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    		[[[cell subviews] objectAtIndex:0] setTag:111];
    		labelView = [[UILabel alloc] initWithFrame: CGRectMake(8, 0, 300, 44)]; 
    		[labelView setBackgroundColor:[UIColor clearColor]]; 
    		[labelView setTag:222];
    		[labelView setFont:[UIFont boldSystemFontOfSize:20]];
    		[cell addSubview:labelView];
    		[labelView release];
    		UIView *cellView = [cell viewWithTag:111];
    		if (row % 2)
    			[cellView setBackgroundColor:[UIColor whiteColor]]; 
    		else
    			[cellView setBackgroundColor:[UIColor colorWithRed:0.90f green:0.95f blue:1.0f alpha:1.0f]];
    		
    		[(UILabel *)[cell viewWithTag:222] setText:[file_list_for_subproject objectAtIndex:indexPath.row]]; 
    		
    	}	
    	return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        detailViewController.detailItem = [file_list_for_subproject objectAtIndex:indexPath.row];
    	
    }  
    /* variable detailitem in DetailViewController class is assigned file url every time any cell is tapped here. And I have checked using NSLog that detailItem is containing that url. Everything works fine till here 
    */
    
    DetailViewController.h is:
    Code:
    #import <UIKit/UIKit.h>
    
    @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate,UIWebViewDelegate> {
        
        UIPopoverController *popoverController;
        UIToolbar *toolbar;
        UIWebView *webview;
        id detailItem;
        UILabel *detailDescriptionLabel;
       UIActivityIndicatorView *uiact;
    	
    }
    
    @property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
    
    @property (nonatomic, retain) id detailItem;
    @property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;
    @property (nonatomic,retain) UIWebView *webview;
    @property (nonatomic ,retain) UIActivityIndicatorView *uiact;
    @end
    
    DetailViewController.m is:
    Code:
    #import "DetailViewController.h"
    
    @interface DetailViewController ()
    @property (nonatomic, retain) UIPopoverController *popoverController;
    @end
    
    @implementation DetailViewController
    
    @synthesize toolbar, popoverController, detailItem, detailDescriptionLabel,webview,uiact;
    
    - (void)setDetailItem:(id)newDetailItem {
        if (detailItem != newDetailItem) {
            [detailItem release];
            detailItem = [newDetailItem retain]; //detailItem contains url of file                   
                                                              // tapped in table
          [self performSelector:@selector(startWebViewLoad) withObject:nil afterDelay:0];
        }
    	
        if (popoverController != nil) {
            [popoverController dismissPopoverAnimated:YES];
        }        
    }
    
    - (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {
        
        barButtonItem.title = @"Root List";
        NSMutableArray *items = [[toolbar items] mutableCopy];
        [items insertObject:barButtonItem atIndex:0];
        [toolbar setItems:items animated:YES];
        [items release];
        self.popoverController = pc;
    }
    
    - (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
        
        NSMutableArray *items = [[toolbar items] mutableCopy];
        [items removeObjectAtIndex:0];
        [toolbar setItems:items animated:YES];
        [items release];
        self.popoverController = nil;
    }
    
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
    }
    
    - (void)startWebViewLoad {
    	NSLog(@"detail item is ------ %@", detailItem); // It is displaying
                                                                               //  correct file URL 
    	CGRect frame= [[UIScreen mainScreen] applicationFrame];
    	UIWebView *webview1 = [[UIWebView alloc] initWithFrame:frame];
    	webview1.backgroundColor = [UIColor redColor]; // I cant see red 
                                                                               //color in detail view
    	webview1.scalesPageToFit =YES;
    	self.webview =webview1;
    	NSString *file_path=detailItem;
    	NSURL *urlpath = [NSURL URLWithString:file_path];
    	NSURLRequest *reqobj = [NSURLRequest requestWithURL:urlpath];
    	[self.webview loadRequest:reqobj];
    	[self.view addSubview: self.webview];
    	[webview1 release];
        //there is no UIWebView getting loaded, I guess and activityindicator keeps on moving
    }
    
    
    - (void)viewDidLoad{
    	
    	UIActivityIndicatorView *progressWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(260,260,90,90)];
    	progressWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    	progressWheel.hidesWhenStopped = YES;
    	self.uiact = progressWheel;
    	[self.view addSubview: self.uiact];
    	[self.uiact startAnimating];
    	[progressWheel release]; 	
    	
    }
    
    // Activityindicator should start right from Root table view, and its working well as expected
    
    
    
    I want to know where am I making mistake? And why file is not getting diplayed in uiwebview in detailview. Files are completely accessible and they are well displayed when using in different project.
     
  4. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #4
    Add NSLog's to every function in both projects. Run both projects. Compare output.

    You can use this for logging:

    Code:
    #define HERE() NSLog(@"%s, %u: %s", __FILE__, __LINE__, __FUNCTION__)
    
     
  5. sagarshivam thread starter macrumors member

    Joined:
    May 24, 2011
    #5
    When I say different projects, I mean that when I am using only one level on left panel of split view, uiwebview is properly working on right pane.

    But When I am making it to 3 level, then uiwebview is not working.
     
  6. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #6
    All of this is very interesting, but does not change my advice :)
     
  7. sagarshivam thread starter macrumors member

    Joined:
    May 24, 2011
    #7
    There is proper NSLog result coming out and even putting nslog above and below uiwebview portion is displaying proper result. But still uiwebview is not loading the files.
     
  8. sagarshivam, Sep 12, 2011
    Last edited: Sep 12, 2011

    sagarshivam thread starter macrumors member

    Joined:
    May 24, 2011
    #8
    plz look at my DetailViewController.m posted below once again.
    I thought to make this issue solved and so I just made it in a separate project.
    I selected splitview based template in xcode and then I implemented navigation in left pane to implement two level deep uitable view. This is working well.
    Also in viewDidLoad method of DetailViewController.m , I initialized and activity indicator, so as soon as the application is launched, I can see root table in left pane and activity indicator in right pane.

    On navigating to second level in left pane, I have few cells and on tapping either cell, some hard coded url (intranet) is passed to var detailItem in detailviewcontroller and on every tapping "startwebviewload" method in detailviwecontroller is called where I am trying to load uiwebview with url obtained.
    I am using nslog in startwebviewload method to check if I am getting correct url or not and It is displaying correct url everytime on console. But uiwebview is not displaying the file.

    I tried giving wrong url also, then I am getting console message as "can not find data converter call back for uti public.data" on tapping that particular cell.

    So, I think the problem is with uiwebview only. Plz suggest.

    (In already generated DetailView.xib , I am adding one uiwebview and adding two rows is reference outlet: view===> file's owner and webview===> file's owner
    And on control dragging of file's owner on uiwebview , I get three outlets, detailItem, view and webview.)

    Kindly suggest

    DetailViewController.h is:
    Code:
    #import <UIKit/UIKit.h>
    
    @interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate,UIWebViewDelegate> {
        
        UIPopoverController *popoverController;
        UIToolbar *toolbar;
        
        id detailItem;
        UILabel *detailDescriptionLabel;
        UIWebView *webview;
        UIActivityIndicatorView *actview;
    }
    
    @property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
    
    @property (nonatomic, retain) id detailItem;
    @property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;
    @property (nonatomic,retain) IBOutlet UIWebView *webview;
    @property (nonatomic, retain) UIActivityIndicatorView *actview;
    @end
    
    DetailViewController.m is
    Code:
    #import "DetailViewController.h"
    #import "RootViewController.h"
    
    
    @interface DetailViewController ()
    @property (nonatomic, retain) UIPopoverController *popoverController;
    - (void)configureView;
    -(void)startWebViewLoad;
    @end
    
    
    
    @implementation DetailViewController
    
    @synthesize toolbar, popoverController, detailItem, detailDescriptionLabel,webview,actview;
    
    
    - (void)setDetailItem:(id)newDetailItem {
    	
        if (detailItem != newDetailItem) {
            [detailItem release];
            detailItem = [newDetailItem retain];
           // NSLog(@"set detail item  ****** %@", detailItem);
           
            [self configureView];
    	[self startWebViewLoad];
        }
    
        if (popoverController != nil) {
            [popoverController dismissPopoverAnimated:YES];
        }        
    }
    
    
    - (void)configureView {
        // Update the user interface for the detail item.
    	[actview stopAnimating];
        detailDescriptionLabel.text = [detailItem description];   
    }
    
    
    #pragma mark -
    #pragma mark Split view support
    
    - (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {
        
        barButtonItem.title = @"Root List";
        NSMutableArray *items = [[toolbar items] mutableCopy];
        [items insertObject:barButtonItem atIndex:0];
        [toolbar setItems:items animated:YES];
        [items release];
        self.popoverController = pc;
    }
    
    
    // Called when the view is shown again in the split view, invalidating the button and popover controller.
    - (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
        NSLog(@"in viewcontroller landscape mode");
        NSMutableArray *items = [[toolbar items] mutableCopy];
        [items removeObjectAtIndex:0];
        [toolbar setItems:items animated:YES];
        [items release];
        self.popoverController = nil;
    }
    
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
    }
    
    
    - (void)startWebViewLoad {
    	[actview stopAnimating];
    	NSLog(@"detail item is ------ %@", detailItem);
    	CGRect frame= [[UIScreen mainScreen] applicationFrame];
    	webview = [[[UIWebView alloc] initWithFrame:frame] autorelease];
    	webview.scalesPageToFit =YES;
    	webview.backgroundColor = [UIColor clearColor];
    	//NSString *file_path=detailItem;
    	NSURL *urlpath = [NSURL URLWithString:detailItem];
    	NSURLRequest *reqobj = [NSURLRequest requestWithURL:urlpath];
    	[webview loadRequest:reqobj];
    	[webview reload];
    	[self.view addSubview:webview];
    	NSLog(@"done");
    	//[self.view addSubview:webview];
    	//[webview reload];
    	
        
    }
    
    - (void)viewDidLoad{
    	UIActivityIndicatorView *progressWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(260,260,90,90)];
    	
    	progressWheel.activityIndicatorViewStyle =  UIActivityIndicatorViewStyleGray;
    	progressWheel.hidesWhenStopped = YES;
    	self.actview = progressWheel;
    	[self.view addSubview: self.actview];
    	[self.actview startAnimating];
           [progressWheel release];
    	[super viewDidLoad];
    		
    	
    }
    
    
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Have you tried having your UIWebView bring up a known URL, such as http://www.apple.com, and seeing what happens?
     
  10. Sykte macrumors regular

    Joined:
    Aug 26, 2010
  11. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #11
    See above.
     
  12. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #12

    Code:
    @property (nonatomic,retain) IBOutlet UIWebView *webview;
    
    His code disagrees, kinda.
     

Share This Page