2012-07-18 68 views
0
NSString *phoneStr = @"tel:"; 
phoneStr = [phoneStr stringByAppendingFormat:@"%@" , self.ContactPhone]; 
NSURL * phoneURL = [NSURL URLWithString:phoneStr]; 
[[UIApplication sharedApplication] openURL:phoneURL]; 

此代碼不工作,不開手機的應用程序,當我打印phoneURL的值總是空無法打開手機應用程序編程

這是爲什麼會發生什麼?

THX

+0

ContactPhone是NSString的類型? – samfisher 2012-07-18 12:35:46

+0

是的,它是NSString類型 – 2012-07-18 12:36:19

+0

首先不需要輸入NSURL。其次,phoneStr有沒有什麼價值? – Nitish 2012-07-18 12:37:06

回答

1

你錯過了//電話:

NSString *phoneStr = @"tel://"; 
phoneStr = [phoneStr stringByAppendingFormat:@"%@" , self.ContactPhone]; 
NSURL * phoneURL = [NSURL URLWithString:phoneStr]; 
[[UIApplication sharedApplication] openURL:phoneURL]; 
+0

不工作,仍然phoneUrl值爲空 – 2012-07-18 12:44:43

+0

如果我用靜態字符串替換ContactPhone的值,它可以正常工作,即如此 phoneStr = [phoneStr stringByAppendingFormat:@「55555」]; 我確信contactPhone值不爲空 – 2012-07-18 12:46:38

+0

做一件事。而不是self.ContactPhone只使用ContactPhone。你確定ContactPhone是NSString嗎? – Nitish 2012-07-18 12:48:07

0

首先,檢查self.ContactPhone不是空的,並使用@ 「telprompt://」在結束通話後返回或重新啓動您的應用。

NSString *phoneStr = @"telprompt://"; 
if([self.ContactPhone length]>0) 
{ 
phoneStr = [phoneStr stringByAppendingFormat:@"%@" , self.ContactPhone]; 
NSURL * phoneURL = [NSURL URLWithString:phoneStr]; 
[[UIApplication sharedApplication] openURL:phoneURL]; 
} 
3

你需要躲避手機串:

[NSURL URLWithString:[phoneStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] 
相關問題