Stop UIWebView from downloading images

Discussion in 'iOS Programming' started by brendan09, Sep 30, 2009.

  1. brendan09 macrumors newbie

    Joined:
    Nov 10, 2008
    #1
    I have searched hours and hours for a solution to prevent UIWebView from downloading images when it renders a page. The webview is hidden, so the user can't see it anyway. This webview queries a server and receives an html page as a result. I have it render the html so that i can inject and run a piece of javascript into the page so that i can extract some information from it. Everything in my code works perfectly with the exception that the code to run the javascript doesn't run until webViewDidFinishLoad is called. This isn't called until after ALL images have been downloaded... this can be excessively slow on the cellular network. The number of images depend upon the query. I don't have access to the code on the server, so i can't stop it from returning images. Any solution or suggestion is appreciated. Thanks!!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You might want to consider this approach: instead of loading the HTML into a UIWebView, load it into an NSString object, via the initWithContentsOfURL: method, and then mess with it from there.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    There is a callback to the webview delegate shouldStartLoadWithRequest:. I haven't used it for this purpose but I think you can return NO for all images if you want and I think it will do what you want.
     
  4. brendan09 thread starter macrumors newbie

    Joined:
    Nov 10, 2008
    #4
    @dejo
    -It seems that i should be able to do what i need with this; however, whenever it hits this line:
    Code:
     NSString * webContents = [NSString initWithContentsOfURL:URL];
    It crashes. The URL is fine, it loads great in a webView.

    @PhoneyDeveloper
    -I've explored shouldStartLoadWithRequest, it doesn't seem to work for this purpose. It only seems to run this whenever a new page is starting to load. It won't let me check for individual elements that are loading within the page.

    -Not sure why it keeps crashing on that line. I've tried several variations from several sources on the web and none of them seem to work, they all crash.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    It crashes because a) you didn't see the compile warning/error first, and b) initWithContentsOfURL is an instance method, not a class method. If you don't know the difference between those, I'd suggest reading up on Objective-C.
     

Share This Page