- 訪問聯絡人,並選擇資訊
- 撥號
- 傳送簡訊
- 傳送郵件
- 本地通知
介面檔案
NS_ASSUME_NONNULL_BEGIN
@interface LZSystemSDKManager : NSObject
+ (instancetype)manager;
/**
選擇聯絡人 先新增許可權
@param result 選擇的資訊
*/
- (void)lz_selecteContactResulet:(void(^)(CNContact *obj))result API_AVAILABLE(ios(9.0));
/**
傳送郵件
@param toRecipients 收件人群組
@param ccRecipients 抄送人群組
@param bccRecipients 密送人群組
@param theme 主題
@param emailContent 正文內容
@param isHTML 是否是HTML格式
@param result 傳送結果
*/
- (void)lz_postEmialWithToRecipients:(NSArray<NSString *> *)toRecipients
ccRecipients:(NSArray<NSString *> *)ccRecipients
bccRecipients:(NSArray<NSString *> *)bccRecipients
theme:(NSString *)theme
emailContent:(NSString *)emailContent
isHTML:(BOOL)isHTML
reslt:(void(^)(NSInteger))result;
/**
傳送簡訊
@param toRecipients 收件人群組
@param content 內容
@param result 傳送結果
*/
- (void)lz_sendMessageWithToRecipients:(NSArray<NSString *> *)toRecipients
content:(NSString *)content
reslt:(void(^)(NSInteger))result;
/**
撥號
@param phoneNum 號碼
*/
- (void)lz_dailPhone:(NSString *)phoneNum;
/**
開啟定位服務
@param localResult 定位獲取資訊
*/
- (void)lz_openLocationService:(void(^)(id info))localResult;
/**
IOS 10的通知 推送訊息 支援的音訊 圖片 <= 10M 視訊 <= 50M
@param body 訊息內容
@param promptTone 提示音
@param soundName 音訊
@param imageName 圖片
@param movieName 視訊
@param identifier 訊息標識
*/
-(void)lz_pushNotification_IOS_10_Title:(NSString *)title
subtitle:(NSString *)subtitle
body:(NSString *)body
promptTone:(NSString *)promptTone
soundName:(NSString *)soundName
imageName:(NSString *)imageName
movieName:(NSString *)movieName
timeInterval:(NSTimeInterval)TimeInterval
repeats:(BOOL)repeats
Identifier:(NSString *)identifier API_AVAILABLE(ios(10.0));
/**
IOS 10前的通知
@param timeInterval 延遲時間
@param repeatInterval 重複提醒次數
@param alertBody 內容
@param alertTitle 標題
@param alertAction 滑動文字
@param alertLaunchImage alertLaunchImage
@param soundName 聲音 為空則為系統聲音
@param userInfo 字典引數
*/
- (void)lz_pushNotifationWithTimeInterval:(NSTimeInterval)timeInterval
repeatInterval:(NSInteger)repeatInterval
alertBody:(NSString *)alertBody
alertTitle:(NSString *)alertTitle
alertAction:(NSString *)alertAction
alertLaunchImage:(NSString *)alertLaunchImage
soundName:(NSString *)soundName
userInfo:(NSDictionary *)userInfo;
@end
NS_ASSUME_NONNULL_END
複製程式碼
實現檔案
@interface LZSystemSDKManager () < CNContactPickerDelegate,
MFMailComposeViewControllerDelegate,
MFMessageComposeViewControllerDelegate,
CLLocationManagerDelegate>
@property (nonatomic, copy) void (^selectContactResult)(CNContact *obj) API_AVAILABLE(ios(9.0));
@property (nonatomic, copy) void (^postEmailResult)(NSInteger obj);
@property (nonatomic, copy) void (^sendMessageResult)(NSInteger obj);
@property (nonatomic, copy) void (^locakResult)(id info);
@end
@implementation LZSystemSDKManager
+ (instancetype)manager {
static dispatch_once_t onceToken;
static LZSystemSDKManager *manager;
dispatch_once(&onceToken, ^{
manager = [[LZSystemSDKManager alloc] init];
});
return manager;
}
- (void)lz_selecteContactResulet:(void(^)(CNContact *obj))result API_AVAILABLE(ios(9.0)) {
self.selectContactResult = result;
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusNotDetermined) {
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
[self showHUDTitle:@"未獲取訪問許可權" info:@"請前往設定中開啟訪問許可權"];
} else {
CNContactPickerViewController * picker = [CNContactPickerViewController new];
picker.delegate = self;
picker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];
[kRootViewController presentViewController: picker animated:YES completion:nil];
}
}];
}
if (status == CNAuthorizationStatusAuthorized) {
CNContactPickerViewController * picker = [CNContactPickerViewController new];
picker.delegate = self;
picker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];
[kRootViewController presentViewController: picker animated:YES completion:nil];
}
else{
[self showHUDTitle:@"未獲取訪問許可權" info:@"請前往設定中開啟訪問許可權"];
}
}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty API_AVAILABLE(ios(9.0)) {
CNContact *contact = contactProperty.contact;
self.selectContactResult(contact);
}
- (void)lz_postEmialWithToRecipients:(NSArray<NSString *> *)toRecipients
ccRecipients:(NSArray<NSString *> *)ccRecipients
bccRecipients:(NSArray<NSString *> *)bccRecipients
theme:(NSString *)theme
emailContent:(NSString *)emailContent
isHTML:(BOOL)isHTML
reslt:(nonnull void (^)(NSInteger))result {
self.postEmailResult = result;
MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
mailCompose.mailComposeDelegate = self;
[mailCompose setToRecipients:toRecipients];
[mailCompose setCcRecipients:ccRecipients];
[mailCompose setBccRecipients:bccRecipients];
[mailCompose setSubject:theme];
[mailCompose setMessageBody:emailContent isHTML:isHTML];
[kRootViewController presentViewController:mailCompose animated:YES completion:nil];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[controller dismissViewControllerAnimated:YES completion:nil];
self.postEmailResult(result);
}
- (void)lz_sendMessageWithToRecipients:(NSArray<NSString *> *)toRecipients content:(NSString *)content reslt:(nonnull void (^)(NSInteger))result {
self.sendMessageResult = result;
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
messageVC.recipients = toRecipients;
messageVC.body = content;
messageVC.messageComposeDelegate = self;
[kRootViewController presentViewController:messageVC animated:YES completion:nil];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
[controller dismissViewControllerAnimated:true completion:nil];
self.sendMessageResult(result);
}
- (void)lz_dailPhone:(NSString *)phoneNum {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]]];
}
- (void)lz_openLocationService:(void(^)(id info))localResult {
self.locakResult = localResult;
CLLocationManager *localManager = [[CLLocationManager alloc] init];
localManager.delegate = self;
localManager.desiredAccuracy = kCLLocationAccuracyBest;
localManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
[localManager requestWhenInUseAuthorization];
if ([CLLocationManager locationServicesEnabled] ||
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse ||
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) {
[localManager startUpdatingLocation];
} else {
[self showHUDTitle:@"未獲取定位許可權" info:@"請前往設定中開啟定位許可權"];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *currLocation = [locations lastObject];
CLLocation *infLocation = [[CLLocation alloc] initWithLatitude:currLocation.coordinate.latitude longitude:currLocation.coordinate.longitude];
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:infLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error !=nil || placemarks.count == 0) {
NSLog(@"%@",error);
self.locakResult(nil);
return ;
}
NSDictionary *infoDic = @{@"country":placemarks.firstObject.country,
@"administrativeArea":placemarks.firstObject.administrativeArea,
@"locality":placemarks.firstObject.locality,
@"subLocality":placemarks.firstObject.subLocality,
@"thoroughfare":placemarks.firstObject.thoroughfare,
@"name":placemarks.firstObject.name
};
self.locakResult(infoDic);
}];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
[self showHUDTitle:@"未獲取定位許可權" info:@"請前往設定中開啟定位許可權"];
}
- (void)showHUDTitle:(NSString *)title info:(NSString *)info {
UIAlertController *alterController = [UIAlertController alertControllerWithTitle:title message:info
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancal = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
}
}
}];
[alterController addAction:cancal];
[alterController addAction:sure];
[kRootViewController presentViewController:alterController animated:true completion:nil];
}
- (void)lz_pushNotifationWithTimeInterval:(NSTimeInterval)timeInterval
repeatInterval:(NSInteger)repeatInterval
alertBody:(NSString *)alertBody
alertTitle:(NSString *)alertTitle
alertAction:(NSString *)alertAction
alertLaunchImage:(NSString *)alertLaunchImage
soundName:(NSString *)soundName
userInfo:(NSDictionary *)userInfo {
// 定義本地通知物件
UILocalNotification *notification = [[UILocalNotification alloc] init];
// 設定呼叫時間
notification.timeZone = [NSTimeZone localTimeZone];
//通知觸發的時間,10s以後
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
// 通知重複次數
notification.repeatInterval = repeatInterval;
// 當前日曆,使用前最好設定時區等資訊以便能夠自動同步時間
notification.repeatCalendar = [NSCalendar currentCalendar];
//設定通知屬性
// 通知主體
notification.alertBody = alertBody;
if (@available(iOS 8.2, *)) {
notification.alertTitle = alertTitle;
} else {
// Fallback on earlier versions
}
// 應用程式圖示右上角顯示的訊息數
notification.applicationIconBadgeNumber += 1;
// 待機介面的滑動動作提示
notification.alertAction = alertAction;
// 通過點選通知開啟應用時的啟動圖片,這裡使用程式啟動圖片
notification.alertLaunchImage = alertLaunchImage;
// 收到通知時播放的聲音,預設訊息聲音
notification.soundName = soundName.length == 0 ? UILocalNotificationDefaultSoundName : soundName;
//設定使用者資訊
notification.userInfo = userInfo;
//呼叫通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
-(void)lz_pushNotification_IOS_10_Title:(NSString *)title
subtitle:(NSString *)subtitle
body:(NSString *)body
promptTone:(NSString *)promptTone
soundName:(NSString *)soundName
imageName:(NSString *)imageName
movieName:(NSString *)movieName
timeInterval:(NSTimeInterval)TimeInterval
repeats:(BOOL)repeats
Identifier:(NSString *)identifier API_AVAILABLE(ios(10.0)) {
//獲取通知中心用來啟用新建的通知
UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
content.title = title;
content.subtitle = subtitle;
content.body = body;
//通知的提示音
if ([promptTone containsString:@"."]) {
UNNotificationSound *sound = [UNNotificationSound soundNamed:promptTone];
content.sound = sound;
}
__block UNNotificationAttachment *imageAtt;
__block UNNotificationAttachment *movieAtt;
__block UNNotificationAttachment *soundAtt;
if ([imageName containsString:@"."]) {
[self addNotificationAttachmentContent:content attachmentName:imageName options:nil withCompletion:^(NSError *error, UNNotificationAttachment *notificationAtt) {
imageAtt = [notificationAtt copy];
}];
}
if ([soundName containsString:@"."]) {
[self addNotificationAttachmentContent:content attachmentName:soundName options:nil withCompletion:^(NSError *error, UNNotificationAttachment *notificationAtt) {
soundAtt = [notificationAtt copy];
}];
}
if ([movieName containsString:@"."]) {
// 在這裡擷取視訊的第10s為視訊的縮圖 :UNNotificationAttachmentOptionsThumbnailTimeKey
[self addNotificationAttachmentContent:content attachmentName:movieName options:@{@"UNNotificationAttachmentOptionsThumbnailTimeKey":@10} withCompletion:^(NSError *error, UNNotificationAttachment *notificationAtt) {
movieAtt = [notificationAtt copy];
}];
}
NSMutableArray * array = [NSMutableArray array];
if (imageAtt) {
[array addObject:imageAtt];
}
if (soundAtt) {
[array addObject:soundAtt];
}
if (movieAtt) {
[array addObject:movieAtt];
}
content.attachments = array;
//新增通知下拉動作按鈕
NSMutableArray * actionMutableArray = [NSMutableArray array];
UNNotificationAction * actionA = [UNNotificationAction actionWithIdentifier:@"identifierNeedUnlock" title:@"進入應用" options:UNNotificationActionOptionAuthenticationRequired];
UNNotificationAction * actionB = [UNNotificationAction actionWithIdentifier:@"identifierRed" title:@"忽略" options:UNNotificationActionOptionDestructive];
[actionMutableArray addObjectsFromArray:@[actionA,actionB]];
if (actionMutableArray.count > 1) {
UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"categoryNoOperationAction" actions:actionMutableArray intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
[center setNotificationCategories:[NSSet setWithObjects:category, nil]];
content.categoryIdentifier = @"categoryNoOperationAction";
}
// UNTimeIntervalNotificationTrigger 延時推送
// UNCalendarNotificationTrigger 定時推送
// UNLocationNotificationTrigger 位置變化推送
BOOL repeat = (TimeInterval > 60 && repeats) ? true : false;
UNTimeIntervalNotificationTrigger * tirgger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:TimeInterval repeats:repeat];
//建立通知請求
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:tirgger];
//將建立的通知請求新增到通知中心
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"%@本地推送 :( 報錯 %@",identifier, error);
}];
}
/**
增加通知附件
@param content 通知內容
@param attachmentName 附件名稱
@param options 相關選項
@param completion 結果回撥
*/
-(void)addNotificationAttachmentContent:(UNMutableNotificationContent *)content attachmentName:(NSString *)attachmentName options:(NSDictionary *)options withCompletion:(void(^)(NSError * error , UNNotificationAttachment * notificationAtt))completion API_AVAILABLE(ios(10.0)) {
NSArray * arr = [attachmentName componentsSeparatedByString:@"."];
NSError * error;
NSString * path = [[NSBundle mainBundle]pathForResource:arr[0] ofType:arr[1]];
UNNotificationAttachment * attachment = [UNNotificationAttachment attachmentWithIdentifier:[NSString stringWithFormat:@"notificationAtt_%@",arr[1]] URL:[NSURL fileURLWithPath:path] options:options error:&error];
if (error) {
NSLog(@"attachment error %@", error);
}
completion(error,attachment);
content.launchImageName = attachmentName;
}
@end
複製程式碼