如何操纵此代码以使其从 plist 中提取电话号码?
-(IBAction)callPhone:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:2135554321"]];
}
如何操纵此代码以使其从 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];
例如,您的 plistNSDictionary
又是:
{“迈克”,“2135554321”“丹尼尔”,“2135554322”“桑德拉”,“2135554323”}
如果你想打电话给丹尼尔:
-(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"];
之后,您可以openURL
使用 中的值调用tel
。