Phone call from plist iOS

随声附和 提交于 2019-12-14 02:33:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!