Question

So I've had to subclass NSURLProtocol because I am using a UIWebView and want to intercept ajax calls which works fine as per this example. I have to get the data from the request from + (BOOL)canInitWithRequest:(NSURLRequest *)request which causes a problem, I now need to call instance methods to do things with the data, but I can't because it's a class method.

How could I call an instance method based on the contents of what is in the request?

No correct solution

OTHER TIPS

You cannot call instance methods from a class method without having an instance first. This is true for all object-oriented systems, AFAIK. After all, the instance methods (usually) use data that the class method cannot have. Imagine an object with a property name, and you now have one thousand objects with different names. How should the class method know which name to use?

So you need to find a way to somehow query instances of your class based on your request, or find a way to get at the information in a way that does not involve instance variables (if a method doesn't use instance variable you can turn it into a class method or even C function). There are many different ways to solve the "query problem", which to chose is highly dependent on your design and even taste.

canInitWithRequest: shouldn't do any work whatsoever, it should just respond with "YES" or "NO". "startLoading" is where you do the work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top