【问题标题】:UIWebView ignoring mailtoUIWebView 忽略 mailto
【发布时间】:2014-12-09 02:02:19
【问题描述】:

我正在尝试让 mailto 链接与 UIWebView 一起使用。

到目前为止,我正在使用下面的委托打开一系列内容(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”链接会被忽略并抛出错误的任何想法?

【问题讨论】:

    标签: ios objective-c uiwebview mailto


    【解决方案1】:

    这两行是原因:

    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的URL,你可以在this answer找到一个例子。

    【讨论】:

    • 啊!就是这样,事实证明“mailto://test@test.com”有效(添加两个斜杠),但您的答案修复了“解决方法”。感谢您的帮助!
    猜你喜欢
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    相关资源
    最近更新 更多