来自plist iOS的电话

时间:2013-09-29 12:58:37

标签: ios objective-c plist

我如何操纵此代码让它从plist中提取电话号码?

-(IBAction)callPhone:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:2135554321"]];
}

2 个答案:

答案 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