来自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
:
{“Mike”,“2135554321”“Deniel”,“2135554322”“Sandra”,“2135554323”}
如果你想打电话给Deniel:
-(IBAction)callPhone:(id)sender { NSString* yourActualNumber = [NSString stringWithFormat:@"tel:%@",telsDictionary[@"Deniel"]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:yourActualNumber]]; }
假设你已经将一个名为foo.plist
的文件包含到你的项目中,并且有一个名为telnum
的string属性,
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"plist"]; NSDictionary *plistHash = [NSDictionary dictionaryWithContentsOfFile:filepath]; NSString *tel = [plistHash objectForKey:@"telnum"];
之后你可以用tel
的值调用openURL
。