删除部分网址

时间:2012-09-16 19:56:27

标签: objective-c cordova

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

- (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。

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

1 个答案:

答案 0 :(得分:2)

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

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];