-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRFTelephone.m
More file actions
148 lines (124 loc) · 3.24 KB
/
RFTelephone.m
File metadata and controls
148 lines (124 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// RFTelephone.m
// RF
//
// Created by gouzhehua on 14-6-26.
// Copyright (c) 2014年 GZH. All rights reserved.
//
#import "RFTelephone.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <MessageUI/MessageUI.h>
#import "ARCMacros.h"
#import "RFKit.h"
#import "RFUIKit.h"
@interface RFTelephone ()
<
MFMessageComposeViewControllerDelegate
>
{
}
@property (nonatomic, SAFE_ARC_STRONG) MFMessageComposeViewController *msgCtrl;
@end
@implementation RFTelephone
@synthesize msgCtrl = _msgCtrl;
+ (RFTelephone *)shared
{
static RFTelephone *s_instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
s_instance = [[RFTelephone alloc] init];
});
return s_instance;
}
- (NSString *)carrierName
{
NSString *ret = nil;
CTTelephonyNetworkInfo *cn = [[CTTelephonyNetworkInfo alloc] init];
if (cn.subscriberCellularProvider != nil)
{
NSString *tmp = cn.subscriberCellularProvider.carrierName;
if (![NSString isEmpty:tmp])
{
ret = [tmp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
}
}
SAFE_ARC_RELEASE(cn);
return ret;
}
- (NSString *)carrierCode
{
NSString *ret = nil;
CTTelephonyNetworkInfo *cn = [[CTTelephonyNetworkInfo alloc] init];
if (cn.subscriberCellularProvider != nil)
{
NSString *mobileCountryCode = cn.subscriberCellularProvider.mobileCountryCode;
NSString *mobileNetworkCode = cn.subscriberCellularProvider.mobileNetworkCode;
NSMutableString *buffer = [NSMutableString stringWithString:@""];
if (![NSString isEmpty:mobileCountryCode])
{
[buffer appendString:mobileCountryCode];
}
if (![NSString isEmpty:mobileNetworkCode])
{
[buffer appendString:mobileNetworkCode];
}
if (![NSString isEmpty:buffer])
{
ret = buffer;
}
}
SAFE_ARC_RELEASE(cn);
return ret;
}
- (BOOL)callWithPhone:(NSString *)aPhone
{
if (![NSString isEmpty:aPhone])
{
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@", aPhone]];
NSLog(@"Call, URL=%@", phoneNumberURL);
return [[UIApplication sharedApplication] openURL:phoneNumberURL];
}
return NO;
}
- (BOOL)sendSMSWithPhone:(NSString *)aPhone content:(NSString *)aContent
{
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass == nil || ![messageClass canSendText])
{
return NO;
}
if (_msgCtrl != nil)
{
[_msgCtrl dismissMe];
SAFE_ARC_AUTORELEASE(_msgCtrl);
_msgCtrl = nil;
}
self.msgCtrl = SAFE_ARC_AUTORELEASE([[MFMessageComposeViewController alloc] init]);
[_msgCtrl setMessageComposeDelegate:self];
[_msgCtrl setBody:aContent];
[_msgCtrl setRecipients:[NSArray arrayWithObject:aPhone]];
UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
if (window.rootViewController != nil)
{
[window.rootViewController presentViewController:_msgCtrl animated:YES];
}
return YES;
}
#pragma mark MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
case MessageComposeResultCancelled:
break;
case MessageComposeResultSent:
break;
case MessageComposeResultFailed:
break;
default:
break;
}
[controller dismissMe];
}
@end