问题
How can I manipulate this code to have it pull the phone number from a plist?
-(IBAction)callPhone:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:2135554321"]];
}
回答1:
You should add your plist into your project (if it s in your bundle) (for example if its a dictionary):
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"clubs" ofType:@"plist"];
telsDictionary = [NSDictionary dictionaryWithContentsOfFile:filePath];
for example, your plist is a NSDictionary
again:
{"Mike","2135554321" "Deniel","2135554322" "Sandra","2135554323"}
If you want to call Deniel:
-(IBAction)callPhone:(id)sender {
NSString* yourActualNumber = [NSString stringWithFormat:@"tel:%@",telsDictionary[@"Deniel"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:yourActualNumber]];
}
回答2:
Assuming you have included a file named foo.plist
into your project, with a string property named telnum
, the following should work-
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"plist"];
NSDictionary *plistHash = [NSDictionary dictionaryWithContentsOfFile:filepath];
NSString *tel = [plistHash objectForKey:@"telnum"];
afterwards you can call openURL
with the value in tel
.
来源:https://stackoverflow.com/questions/19078308/phone-call-from-plist-ios