开发者

How do I open links in a WebView rather than Safari?

I have a text view, which has links to开发者_如何学Python 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)


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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜