1

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.

Community
  • 1
  • 1
ccagle8
  • 308
  • 1
  • 2
  • 12

2 Answers2

1

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.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
1

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.

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • Is there something I will need to add to my `Foo.h` file after changing the + to a -? I ask because I did that and now my app crashes. – ccagle8 Sep 19 '12 at 17:58
  • How are you calling openWebView? What error are you getting in the debugger? – vacawama Sep 19 '12 at 18:02
  • I am calling it from within another method inside that same class file. `-(void) startTheWebView { [...] }` The error is **target does not implement selector...** – ccagle8 Sep 19 '12 at 18:04
  • So you're calling it with `[self openWebView]` ? – vacawama Sep 19 '12 at 18:08
  • It sounds like you are not implementing a method in class Foo that is required by being a UIWebViewDelegate. – vacawama Sep 19 '12 at 18:10
  • `[self performSelectorInBackground:@selector(openWebView) withObject:nil]` I also tried it your way, and I also got the same type of error message in debugger. – ccagle8 Sep 19 '12 at 18:12
  • Which selector does the target not implement? – vacawama Sep 19 '12 at 18:15
  • -[NSThread initWithTarget:selector:object:]: target does not implement selector (*** +[startTheWebView]) – ccagle8 Sep 19 '12 at 18:23
  • Ok, so the answer to why `loadRequest` is not working is that I do not have my delegate set correctly? – ccagle8 Sep 19 '12 at 18:38
  • 1
    There may be other things wrong with your `loadRequest` as well. Why don't you implement the delegate methods `webViewDidStartLoad` and `webViewDidFinishLoad` in your delegate (ie Foo.m) and see if they get called. – vacawama Sep 19 '12 at 18:51