【问题标题】:How to catch a URL and render it in-app with UIWebView?如何捕获 URL 并使用 UIWebView 在应用程序中呈现它?
【发布时间】:2012-01-09 12:03:24
【问题描述】:

在我的 iOS 应用程序中,我有一个呈现为 NSString 的 URL。现在,如果用户点击 URL,它将在 Safari 中打开网页。有没有办法抓住水龙头并在 UIWebView 中打开它?我已经设置了 UIWebView 控制器,只需要捕获 URL 点击。谢谢!

这是现在呈现 URL 的代码。

[detailInfoList addObject: [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithInteger:kCarrierInfo], @"type", 
                            [NSString stringWithFormat:@"http://www.ups.com/track/%@/%@", userPackage.carrier, userPackage.trackingNumber], 
                            @"link", nil]];

【问题讨论】:

  • detailInfoList 是什么类型的对象? URL 是如何/在哪里呈现的?

标签: ios uiwebview


【解决方案1】:

编辑:您的 URL 不会被捕获并自动传递给 UIWebView。你必须设置一些程序/方法来捕捉它。否则,它将被移交给 Safari,例如UITextView 中的链接。


您必须将 URL 传递给 UIWebView 的实例。无论你是把它子类化为一个新的 ViewController 还是你当前的 viewController 中的一个对象,你都可以通过调用loadRequest 方法来设置它的 URL:

// if it is an object
[self.webView setLoadRequest:[URLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"]]];

// or if it is subclassed
self.loadRequest = ... // as mentioned above

您也可以使用我个人更喜欢的requestWithURL:(NSURL *)theURL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval),因为您可以使用更多选项对其进行自定义。

以下是有用的阅读链接: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURLRequest

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2011-05-17
    • 1970-01-01
    • 2012-05-11
    • 2018-02-01
    相关资源
    最近更新 更多