ios学习笔记之-点击一个按钮弹出拨打电话提示框

来源:互联网 发布:抠图算法 编辑:程序博客网 时间:2024/06/10 19:55

按钮的代码就不写了。直接写主要代码。

<key>LSApplicationQueriesSchemes</key>
<array>
<string>tel</string>
<string>telprompt</string>
</array>

plist里加上述代码就是意思申请打电话权限

-(void)callPhoneStr:(NSString*)phoneStr  {

    NSString *str2 = [[UIDevicecurrentDevice] systemVersion];

    

    if ([str2compare:@"10.2"options:NSNumericSearch] ==NSOrderedDescending || [str2compare:@"10.2"options:NSNumericSearch] ==NSOrderedSame)

    {

        NSLog(@">=10.2");

        NSString* PhoneStr = [NSStringstringWithFormat:@"tel://%@",phoneStr];

        if ([PhoneStrhasPrefix:@"sms:"] || [PhoneStrhasPrefix:@"tel:"]) {

            UIApplication * app = [UIApplicationsharedApplication];

            if ([appcanOpenURL:[NSURLURLWithString:PhoneStr]]) {

                [app openURL:[NSURLURLWithString:PhoneStr ]options:@{}completionHandler:nil                                                                                ];

              

            }

        }

    }else {

        NSMutableString* str1 = [[NSMutableStringalloc]initWithString:phoneStr];//存在堆区,可变字符串

     

        NSString * str = [NSStringstringWithFormat:@"是否拨打电话\n%@",str1];

        UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:strmessage: nilpreferredStyle:UIAlertControllerStyleAlert];

      

        [alert addAction:[UIAlertActionactionWithTitle:@"呼叫"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *action) {

            NSLog(@"点击了呼叫按钮10.2");

            NSString* PhoneStr = [NSStringstringWithFormat:@"tel://%@",phoneStr];

            if ([PhoneStrhasPrefix:@"sms:"] || [PhoneStrhasPrefix:@"tel:"]) {

                UIApplication * app = [UIApplicationsharedApplication];

                if ([appcanOpenURL:[NSURLURLWithString:PhoneStr]]) {

                    [app UIApplication:[NSURLURLWithString:PhoneStr]];

                }

            }

        }]];

        [alert addAction:[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {

            NSLog(@"点击了取消按钮");

        }]];

        [selfpresentViewController:alertanimated:YEScompletion:nil];

    }

    

}

一定要区分一下ios系统版本,因为api是不一样的,因为版本不一样,方法是不一样的。10.2那个里面的方法自己可以弹出一个弹出框,另外一个需要用UIAlertController来制作弹出框。

这里推荐一个

http://www.cocoachina.com/ios/20161021/17824.html


0 0
原创粉丝点击