我如何操纵此代码让它从plist中提取电话号码?
-(IBAction)callPhone:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:2135554321"]];
}
答案 0 :(得分:2)
您应该将plist添加到项目中(如果它在您的包中)(例如,如果它是一个字典):
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"clubs" ofType:@"plist"];
telsDictionary = [NSDictionary dictionaryWithContentsOfFile:filePath];
例如,您的plist再次为NSDictionary
:
{“Mike”,“2135554321”“Deniel”,“2135554322”“Sandra”,“2135554323”}
如果你想打电话给Deniel:
-(IBAction)callPhone:(id)sender {
NSString* yourActualNumber = [NSString stringWithFormat:@"tel:%@",telsDictionary[@"Deniel"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:yourActualNumber]];
}
答案 1 :(得分:0)
假设您已将名为foo.plist
的文件包含在项目中,并使用名为telnum
的字符串属性,则以下内容应该有效 -
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"plist"];
NSDictionary *plistHash = [NSDictionary dictionaryWithContentsOfFile:filepath];
NSString *tel = [plistHash objectForKey:@"telnum"];
之后,您可以使用openURL
中的值调用tel
。