ios - UIWebView 忽略 mailto

标签 ios objective-c uiwebview mailto

我正在尝试让 mailto 链接与 UIWebView 一起工作。

到目前为止,我正在使用下面的委托(delegate)打开一系列内容(http://、file://等),它们都工作正常。然而,似乎 mailto 甚至没有到达那里,如果我对传递给它的每个 url 发出警报,所有其他人都会发出警报,但 mailto 链接没有任何内容。

Xcode 会抛出

WebKit discarded an uncaught exception in the webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: delegate: <NSRangeException> *** -[__NSArrayI objectAtIndex:]: index 4294967295 beyond bounds [0 .. 0]

代表

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

NSURL *url = request.URL;
NSArray *components = [url.absoluteString componentsSeparatedByString:@"/"];
NSString *folder = [components objectAtIndex:[components count]-2];

UIAlertView *display_url = [[UIAlertView alloc]initWithTitle:@"Warning" message:[NSString stringWithFormat:@"URL %@ ", request.URL.scheme] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

[display_url show];

[...]

关于为什么“mailto”链接会被忽略并抛出错误的任何想法?

最佳答案

这两行是原因:

NSArray *components = [url.absoluteString componentsSeparatedByString:@"/"];
NSString *folder = [components objectAtIndex:[components count]-2];

根据the RFCmailto: URL 中没有斜杠。所以 components 数组只包含 1 个对象(整个 URL)。然后你请求一个索引为 -1 的对象,它会抛出一个异常。 WebKit 似乎捕获异常(否则您的应用程序将崩溃)并显示您发布的消息。

一般来说,检查您的输入是个好主意(在这种情况下是 URL。您不能假设 URL 总是像“protocol://domain.com/something/something-else”。有这里有很多可能的情况(比如相对路径)。

至于解析mailto URLs,你可以在this answer中找到一个例子。 .

关于ios - UIWebView 忽略 mailto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26358065/

相关文章:

ios - 不同的纵向和横向布局

ios - xcode 5.1 - 体系结构 x86_64 (zbar) 的 undefined symbol

ios - 是否可以跟踪 UIView 框架的更改?

objective-c - CAShapeLayer 不断关闭路径,我希望路径打开

ios - 像 NSFetchedController 在 FMDB 的 Core Data 中使用的那样缓存结果

objective-c - 未调用 UIWebViewDelegate 函数

ios - 使用 UIWebView 在 iOS 应用程序中登录 Instagram 时出错

ios - UIProgressView 没有更新?

objective-c - Xcode 4 调试器代码完成

ios - 在 UIView Controller 和 UITableViewController 之间传递值