0

I've read

Invoke method in objective c code from HTML code using UIWebView

They do

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

Here, a problem of my code is that I do not have

UIWebView's delegate's shouldStartLoadWithRequest method, since I simply implement UIWebView directly in AppDelegate like below:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{       
    UIWebView *view = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    NSString* path = [[NSBundle mainBundle] pathForResource:@"main" ofType:@"html" inDirectory:@"www"];
    [view loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
    view.scalesPageToFit = NO;
    UIViewController *controller = [[UIViewController alloc] init];
    controller.view = view;

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = controller;
    [self.window makeKeyAndVisible];

    return YES;    
}

I like this way because of the simpler structure of code and files.

So my question is, is it possible to implement call-native-from-webView in this style?

or, do I need to have an independent UIWebView Controller/Delegate?

If possible, can you show me the example how to do that?

I have not done much for objectiveC codes and MVC models, so appreciated if you show me the way. Thanks.

Community
  • 1
  • 1
  • This code belongs in a `UIViewController` subclass--not your app delegate... – nhgrif Mar 21 '14 at 21:53
  • Yes, I know. and here, my question is, is it possible without having UIViewController. –  Mar 21 '14 at 21:54
  • Can it? Yes. Should it? Sure, it depends. Should it be done in the App Delegate? Absolutely not under any circumstance. Moreover, why aren't you using storyboards? – nhgrif Mar 21 '14 at 21:58
  • There are plenty of reasons not to use storyboards. There aren't any _good_ reasons for using a generic UIViewController in this manner in the app delegate though. – powerj1984 Mar 21 '14 at 22:14
  • There are some good reasons not to use storyboards. There aren't very many good reasons for starting new projects and not using storyboards. Still though, there are reasons. The default should be to use a storyboard though, and only not use a storyboard if you have a good reason not to do so, and therefore, when people aren't using storyboards, I always ask why not. Most people I see don't have a good reason. – nhgrif Mar 21 '14 at 22:16

1 Answers1

1

The webView:shouldStartLoadWithRequest:navigationType: method will be called on whatever object is set as the webview's delegate.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Ok, I 've got a feeling it is possible with my code style according to your answer. Could you be more specific? Thanks. Small modification of my code would be appreciated. –  Mar 21 '14 at 21:58
  • `webview.delegate = theObjectYouWantToDelegateYourWebViewWhichShouldAbsolutelyNotBeYourAppDelegate_youdDoWellToWorkYourWayThroughACoupleTutorials;` where the variable can be any object that conforms to the `UIWebViewDelegate` protocol. – nhgrif Mar 21 '14 at 21:59
  • Sorry, I think you fragment your answer. I must say if I am mature enough to guess the structure, I would not ask this sort of question. –  Mar 21 '14 at 22:05
  • I think I'd be more opening to holding your hand through the entire process if you were more open to doing it the right way. You should be using storyboards unless you have a very good reason for not, and you shouldn't be putting this particular code in your App Delegate for any reason at all. – nhgrif Mar 21 '14 at 22:07
  • Ok, the reason I avoid to use `storyboards` is I think it's a new feature of Xcode and I need to depends on the GUI design pane of Xcode. So far I'm not that convinced that it's the best way to implement a single webView in the app. If I'm convinced it's the only way to do that, I would do that. Thanks. –  Mar 21 '14 at 22:13
  • It's not really considered a new Xcode feature anymore... but you still don't have a reason for implementing this code in your app delegate instead of in another class. – nhgrif Mar 21 '14 at 22:15