[英]Phone call from plist iOS
如何處理此代碼以使其從plist中提取電話號碼?
-(IBAction)callPhone:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:2135554321"]];
}
您應該將plist添加到您的項目中(如果它在捆綁包中)(例如,如果它是字典):
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"clubs" ofType:@"plist"];
telsDictionary = [NSDictionary dictionaryWithContentsOfFile:filePath];
例如,您的plist再次是NSDictionary
:
{“邁克”,“ 2135554321”,“丹尼爾”,“ 2135554322”,“桑德拉”,“ 2135554323”}
如果要致電Deniel:
-(IBAction)callPhone:(id)sender {
NSString* yourActualNumber = [NSString stringWithFormat:@"tel:%@",telsDictionary[@"Deniel"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:yourActualNumber]];
}
假設您已在項目中包含一個名為foo.plist
的文件,該文件的字符串屬性為telnum
,則以下代碼應該可以工作:
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"plist"];
NSDictionary *plistHash = [NSDictionary dictionaryWithContentsOfFile:filepath];
NSString *tel = [plistHash objectForKey:@"telnum"];
之后,您可以使用tel
的值調用openURL
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.