views:

264

answers:

2

I have a text view, which has links to web pages, like Wikipedia. Usually when I click on the links it closes my app and opens up Safari.

How do I control the hyperlink, so it loads the web page in the UIWebView instead of Safari?

Putting a button over the link isn't what I want, because it is a scrollable text view.

Is there a Void I can call, because I also need to start showing the web view when the link is clicked.

Thanks. Any answers would be appreciated.

(I am a 12 year old learning to program for the iPhone. See Spaceulator or visit timh.me.uk for my first app)

+1  A: 

AFAIK this is not possible with a UITextView.

But you could display the text in a UIWebView instead (after converting it to HTML), so that you are in a UIWebView from the beginning.

oefe
+2  A: 

You can register your app as a URL handler for some URL scheme - for example "nttp". Add the next lines in your Info.plist file:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>com.test.nttp</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>nttp</string>
        </array>
    </dict>
</array>

When you write the content for your text view use the "nttp" URL scheme instead of the "http" ("nttp://www.stackoverflow.com/" instead of "http://www.stackoverflow.com/").

Implement the application:handleOpenURL: method in your app delegate:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

NSString *aUrl = [url absoluteString];

// Use the URL

return YES; }

For example, you can replace the "nttp" by "http" and initiate some web view with it...

Hope that this solution is good enough...

Michael Kessler
Thanks Michael. The link nttp://www.stackoverflow.com (or any other url with nttp) now shows blue, and acts like a hyperlink, but when it's clicked on, it doesn't actually do anything. I've tried it in the app delegate and the view controller (where all my code is), but it's not being called. Here is the code I put in:- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { NSString *aUrl = [url absoluteString]; //Load the request in the UIWebView. [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:aUrl]]]; // Use the URL return YES; }
philh