2014-12-03 29 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