ccagle8
ccagle8

Reputation: 308

UIWebView will not loadRequest inside a separate class file

I have a UIWebView that is successfully being created dynamically from an included class file (I think... there are no errors being spit out). My function that creates this webview in the Foo.m class file is:

+(void) openWebView {
  dispatch_async(dispatch_get_main_queue(), ^{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];
    [webView setDelegate:self];
    [webView setHidden:YES];
    NSURL *url = [NSURL URLWithString:address];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [webView loadRequest:urlRequest];
    NSLog(@"Successful web open: ", url); 
  });
}

I do get a warning error message on the setDelegate line (it doesn't crash the app though) that reads:

Incompatible pointer types sending 'const Class' to parameter of type 'id:<UIWebViewDelegate>'

The problem is that while the NSLog does log the correct URL it is trying to open, the page actually never gets called (I can tell this because I have a PHP counter on that page that increments each time it is opened).

So my questions are:

  1. Am I missing something in the .h file? I've added nothing there in respect to this new WebView
  2. I feel like I am missing the object that self references to. It was also made mention here in my precursor question to this one.
  3. How can I check the response of the loadRequest

Sorry everyone, as you probably can figure out from my questions, Obj-C is a little new to me, but I am really struggling here and would appreciate the help! Thanks.

Upvotes: 1

Views: 1036

Answers (2)

vacawama
vacawama

Reputation: 154533

Your openWebView method is a class method because of the + at the beginning. For this reason, you don't have an object. self in this case returns the class (hence the warning). Perhaps you want openWebView to be an instance method, so change the + to - and then self will point to your instantiated object.

To check if the loadRequest worked, you implement the UIWebViewDelegate delegate methods webViewDidStartLoad and webViewDidFinishLoad in your delegate (ie Foo.m) and see if they get called.

Upvotes: 1

DrummerB
DrummerB

Reputation: 40211

You are implementing a class method (indicated by the + sign). self in a class method refers to the class itself, not an instance. The delegate of a UIWebView (or anything really) has to be an object though.

Upvotes: 1

Related Questions