我正在尝试让 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 RFC ,mailto:
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/