1. 程式人生 > >ios學習筆記之-點選一個按鈕彈出撥打電話提示框

ios學習筆記之-點選一個按鈕彈出撥打電話提示框

按鈕的程式碼就不寫了。直接寫主要程式碼。

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

plist里加上述程式碼就是意思申請打電話許可權

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

NSString *str2 = [[UIDevicecurrentDevice] systemVersion];

if

([str2 compare:@"10.2"options:NSNumericSearch] == NSOrderedDescending || [str2 compare:@"10.2"options:NSNumericSearch] == NSOrderedSame)

    {

NSLog(@">=10.2");

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

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

UIApplication * app = [UIApplicationsharedApplication];

if ([app canOpenURL:[NSURLURLWithString:PhoneStr]]) {

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

            }

        }

    }else {

NSMutableString* str1 = [[NSMutableStringalloc]initWithString:phoneStr];// 存在堆區,可變字串

NSString * str = [NSStringstringWithFormat:@"是否撥打電話\n%@",str1];

UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:str message: nilpreferredStyle:UIAlertControllerStyleAlert];

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

NSLog(@"點選了呼叫按鈕10.2");

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

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

UIApplication * app = [UIApplicationsharedApplication];

if ([app canOpenURL:[NSURLURLWithString:PhoneStr]]) {

                    [app UIApplication:[NSURLURLWithString:PhoneStr]];

                }

            }

        }]];

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

NSLog(@"點選了取消按鈕");

        }]];

        [selfpresentViewController:alert animated:YEScompletion:nil];

    }

}

一定要區分一下ios系統版本,因為api是不一樣的,因為版本不一樣,方法是不一樣的。10.2那個裡面的方法自己可以彈出一個彈出框,另外一個需要用UIAlertController來製作彈出框。

這裡推薦一個

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