2014-12-03 81 views
1

我有应用程序使用电话卡进行自动电话呼叫。因此,我需要解析电话号码给nsurl来打电话。无法使用两个#号或更多解析NSString到NSURL

UIApplication *myApp = [UIApplication sharedApplication]; 
NSURL *telURL = [NSURL URLWithString:@"tel://18005333333,,,1#,,,421#,,,959538988"]; 
[myApp openURL:telURL]; 

我注意到,如果有超过1#,我不能解析到NSURL。它总是返回给我零。如果我需要超过1#,我可以知道如何解析NSURL吗?

回答

1

如果可以需要的iOS 7.0或更高版本,那么我建议你使用NSURLComponents

NSURLComponents* components = [[NSURLComponents alloc] init]; 
components.scheme = @"tel"; 
components.host = @"18005333333,,,1#,,,421#,,,959538788"; 
NSURL* telURL = components.URL; 
[myApp openURL:telURL]; 

否则,您可能需要使用CFURLCreateStringByAddingPercentEscapes()强行%的-难逃“#”字符。在整个URL而不是组件上使用这个函数并不是真的正确,但是如果你只打算在这些URL上使用它,而没有其他的组件,那么这个URL就可以工作。

+0

它是完美的。谢谢肯·托马斯 – 2014-12-06 15:48:09

0

在您的情况下,您需要使用NSMutableURLRequest而不是openURL,并分别向其中添加参数。

编辑: 试试这个

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:@"tel://18005333333"]; 
    [request setValue:@"1#" forHTTPHeaderField:@",,,"]; 

    [[UIApplication sharedApplication] openURL:request.URL]; 
+0

我可以知道如何使用? – 2014-12-03 14:16:29

+0

它只打电话给“18005333333”,并没有要求休息。 – 2014-12-03 14:47:11

0

首先在手机串号删除#如果真的没关系做出以下

NSString *[email protected]"18005333333,,,1#,,,421#,,,959538788"; 

NSString *phonenumberstring = [phoneString stringByReplacingOccurrencesOfString:@"#" withString:@""]; 

NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",[phonenumberstring stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]]; 
    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) { 
     [[UIApplication sharedApplication] openURL:phoneUrl]; 
    } else { 
     UIAlertView *noAccess=[[UIAlertView alloc] initWithTitle:@"Title" message:@"Your device doesn't support this feature." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [noAccess show]; 
    } 

电话call.like(OR )

NSURL *telURL = [NSURL URLWithString:@"tel:18005333333,,,1#,,,421#,,,959538788"]; 
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]]; 

希望它有助于哟ü...

+0

我需要#因为我正在为预付费电话卡申请。 – 2014-12-03 14:49:24