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;
------------------------------------------------------------------------------------------------------------------
.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