1

我正在使用 Sencha Touch 和 PhoneGap。该代码适用于 iOS,它正在等待带有后缀的 url #phonegap-external ..

- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ( ([url fragment] != NULL) && ([[url fragment] rangeOfString:@"phonegap=external"].location != NSNotFound))
{
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
}

return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}

所以因为我没有用 Obj-C 写过任何代码,我需要你的帮助。有人可以编辑代码,这样它就可以打开没有后缀的 url。

编辑:如果用户在应用程序中打开 url,它会在 webview 中打开它,但有时我更喜欢在 Safari 中打开 url。因此,此代码检查 url 是否具有这样的后缀 - http://google.com#phonegap-external,然后在 Safari 中打开它。唯一让我烦恼的是 url 没有更改为http://google.com并且它打开给定 url http://google.com/#phonegap-external。有人可以解决这个问题。

4

1 回答 1

2

如果您确定 URL 中指示是内联还是外部打开的部分(即#phonegap-external字符串)始终是 URL 中的最后一个,那么您可以尝试通过编写如下内容来删除此后缀:

NSString *orig = [url absoluteString];
size_t frLen = [@"phonegap-external" length];
NSString *stripped = [orig substringToIndex:orig.length - frLen];
NSURL *newURL = [NSURL URLWithString:stripped];

[[UIApplication sharedApplication] openURL:newURL];
于 2012-09-16T20:11:36.950 回答