Is there anyway to create a link via a webview that opens a part of your application?

StackOverflow https://stackoverflow.com/questions/11297862

  •  18-06-2021
  •  | 
  •  

سؤال

For example: The facebook app is basically just a webview using html5, so how does it link to things like the default message editor when you try to write something on someone's wall? I know part of the app is probably using Three20. So are they just linking to that part of the app using their app url? (fb://profile/122605446 for example).

هل كانت مفيدة؟

المحلول

Yes you can do this using the UIWebView delegate method webView:shouldStartLoadWithRequest:navigationType: method. You will need to use a custom URL scheme to trigger your custom code, otherwise you let it pass through as normal. so in your example fb is the custom scheme. The use the rest of the URL to navigate to the profile screen for user 122605446.

Here is some sample code to help:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url = request.URL;
    NSString *scheme = [url scheme];
    if ([scheme isEqualToString:@"fb"]) {
        //Do something with the rest of the url and take any action you like.
    }
    else {
        return YES;    
    }
}

نصائح أخرى

Yes you could do that. Look at the UIWebViewDelegate Protocol Reference, specifically webView:shouldStartLoadWithRequest:navigationType:

The NSURLRequest will let you know when a link is clicked. You could have specific args in the link & depending on the args you could custom handle what views to open in your iOS app. or you would simply return NO on this method if you want to prevent the request from loading.

The UIWebViewDelegate has a method called webView:shouldStartLoadingRequest:navigationType. You can use this method to intercept certain links, button clicks, etc. You can create your own link that way, too:

<a href="myApp://dosomething">Click!</a> The second argument of the delegate method now has a NSURLRequest for which the URL is "myApp://dosomething".

You could simply register a number of url types (or just one, fb://) in your app.

Then you parse the url and open different parts of the application based on the URL you received.

See http://mobiledevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html for a good overview and tutorial.

(It does not really matter that the views in the app are written in HTML5 in this case.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top