objective-c 拨打电话(NSString扩展类)

NSString扩展类的建立方法:url

第一步:建立一个NSString+Phone 继承NSObject的类spa

第二步:在建立的.h文件中@interface  NSString_Phone : NSObject.net

 改成NSString(Phone);
3d

第三步:在建立的.m文件中orm

@implementation NSString_Phone 改成NSString(Phone)继承


这样就建立成功!!以此类推:NSString都能扩展其余类一样能够。get

---------------------------------------------------------------string

.h文件it

#import <Foundation/Foundation.h>io

#import <UIKit/UIKit.h>

@interface NSString(Phone)

-(void)call_Controller:(UIViewController * )controller;

@end

------------------------------------------------------------------------------------------------------------------

.m文件

#import "NSString+Phone.h"


@implementation NSString(Phone)

-(void)call_Controller:(UIViewController * )controller{

    //拨打电话的两种格式

    //telprompt

    //tel

    NSString * urlstr=[NSString stringWithFormat:@"tel://%@",self];

    NSURL * url=[NSURL URLWithString:urlstr];

    

    UIAlertController * alertController=[UIAlertController alertControllerWithTitle:@"提示" message:@"是否拨打电话"preferredStyle:UIAlertControllerStyleAlert];

    

    [alertController addAction:[UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        if ([[UIApplication sharedApplication] canOpenURL:url]) {

            [[UIApplication sharedApplication] openURL:url];

        }

    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];

    [controller presentViewController:alertController animated:YES completion:nil];

}

@end

相关文章
相关标签/搜索