-
Notifications
You must be signed in to change notification settings - Fork 1
/
TLTwitterClient.m
207 lines (178 loc) · 9.38 KB
/
TLTwitterClient.m
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// TLTwitterClient.m
// TLCommon
//
// Created by Joshua Bleecher Snyder on 11/4/09.
//
#import "TLTwitterClient.h"
#import "NSString_TLCommon.h"
#define kTweetieFamily @"tweetie"
#define kTwitterfonFamily @"twitterfon"
#define kTwitterfonProFamily @"twitterfonpro"
#define kTwitterrificFamily @"twitterrific"
#define kTwittelatorFamily @"twittelator"
#define kBirdfeedFamily @"birdfeed"
#define kTwinkleFamily @"twinkle"
#pragma mark -
@interface TLTwitterClient ()
+ (TLTwitterClient *)twitterClientWithFamily:(NSString *)family
familyPriority:(NSUInteger)priority
displayName:(NSString *)name
canOpenURLTestURL:(NSURL *)testURL
openWithTweetURLFormat:(NSString *)tweetFormat
openWithURLURLFormat:(NSString *)urlFormat;
- (void)openURLWithFormat:(NSString *)format string:(NSString *)aString;
@property(nonatomic, retain, readwrite) NSString *family;
@property(nonatomic, assign, readwrite) NSUInteger familyPriority;
@property(nonatomic, retain, readwrite) NSString *displayName;
@property(nonatomic, retain, readwrite) NSURL *canOpenURLTestURL;
@property(nonatomic, retain, readwrite) NSString *openWithTweetURLFormat;
@property(nonatomic, retain, readwrite) NSString *openWithURLURLFormat;
@end
#pragma mark -
@implementation TLTwitterClient
@synthesize family;
@synthesize familyPriority;
@synthesize displayName;
@synthesize canOpenURLTestURL;
@synthesize openWithTweetURLFormat;
@synthesize openWithURLURLFormat;
+ (TLTwitterClient *)twitterClientWithFamily:(NSString *)family
familyPriority:(NSUInteger)priority
displayName:(NSString *)name
canOpenURLTestURL:(NSURL *)testURL
openWithTweetURLFormat:(NSString *)tweetFormat
openWithURLURLFormat:(NSString *)urlFormat {
TLTwitterClient *client = [[[self alloc] init] autorelease];
client.family = family;
client.familyPriority = priority;
client.displayName = name;
client.canOpenURLTestURL = testURL;
client.openWithTweetURLFormat = tweetFormat;
client.openWithURLURLFormat = urlFormat;
return client;
}
+ (NSSet *)allTwitterClients {
NSSet *allTwitterClients = [NSSet setWithObjects:
// tweetie
[self twitterClientWithFamily:kTweetieFamily
familyPriority:1
displayName:@"Tweetie"
canOpenURLTestURL:[NSURL URLWithString:@"tweetie:///post?message=test"]
openWithTweetURLFormat:@"tweetie:///post?message=%@"
openWithURLURLFormat:nil],
// twitterfon
[self twitterClientWithFamily:kTwitterfonFamily
familyPriority:1
displayName:@"Twitterfon"
canOpenURLTestURL:[NSURL URLWithString:@"twitterfon:///message?test"]
openWithTweetURLFormat:@"twitterfon:///message?%@"
openWithURLURLFormat:@"twitterfon:///post?%@"],
// twitterfon pro
[self twitterClientWithFamily:kTwitterfonProFamily
familyPriority:1
displayName:@"Twitterfon Pro"
canOpenURLTestURL:[NSURL URLWithString:@"twitterfonpro:///message?test"]
openWithTweetURLFormat:@"twitterfonpro:///message?%@"
openWithURLURLFormat:@"twitterfonpro:///post?%@"],
// echofon
[self twitterClientWithFamily:kTwitterfonFamily
familyPriority:2
displayName:@"Echofon"
canOpenURLTestURL:[NSURL URLWithString:@"echofon:///message?test"]
openWithTweetURLFormat:@"echofon:///message?%@"
openWithURLURLFormat:@"echofon:///post?%@"],
// echofon pro
[self twitterClientWithFamily:kTwitterfonProFamily
familyPriority:2
displayName:@"Echofon Pro"
canOpenURLTestURL:[NSURL URLWithString:@"echofonpro:///message?test"]
openWithTweetURLFormat:@"echofonpro:///message?%@"
openWithURLURLFormat:@"echofonpro:///post?%@"],
// twitterrific
[self twitterClientWithFamily:kTwitterrificFamily
familyPriority:1
displayName:@"Twitterrific"
canOpenURLTestURL:[NSURL URLWithString:@"twitterrific:///post?message=test"]
openWithTweetURLFormat:@"twitterrific:///post?message=%@"
openWithURLURLFormat:nil],
// twitterrific
[self twitterClientWithFamily:kTwittelatorFamily
familyPriority:1
displayName:@"Twittelator"
canOpenURLTestURL:[NSURL URLWithString:@"twit:///post?message=test"]
openWithTweetURLFormat:@"twit:///post?message=%@"
openWithURLURLFormat:nil],
// birdfeed
[self twitterClientWithFamily:kBirdfeedFamily
familyPriority:1
displayName:@"Birdfeed"
canOpenURLTestURL:[NSURL URLWithString:@"x-birdfeed://post?text=test"]
openWithTweetURLFormat:@"x-birdfeed://post?text=%@"
openWithURLURLFormat:@"x-birdfeed://post?url=%@"],
// twinkle
[self twitterClientWithFamily:kTwinkleFamily
familyPriority:1
displayName:@"Twinkle"
canOpenURLTestURL:[NSURL URLWithString:@"twinkle://message=test"]
openWithTweetURLFormat:@"twinkle://message=%@"
openWithURLURLFormat:@"twinkle://url=%@"],
nil];
return allTwitterClients;
}
+ (NSSet *)availableTwitterClients {
NSSet *allTwitterClients = [self allTwitterClients];
NSMutableDictionary *preferredTwitterClientByFamily = [NSMutableDictionary dictionaryWithCapacity:[allTwitterClients count]];
UIApplication *application = [UIApplication sharedApplication];
// pick out the top priority client w/in each family that is launchable
for(TLTwitterClient *client in allTwitterClients) {
// check whether the client is launchable
if(![application canOpenURL:client.canOpenURLTestURL]) {
continue;
}
// the client is launchable -- add it iff it is better than the current competition for its family
TLTwitterClient *currentBestClientInFamily = [preferredTwitterClientByFamily objectForKey:client.family];
if(!currentBestClientInFamily || client.familyPriority > currentBestClientInFamily.familyPriority) {
[preferredTwitterClientByFamily setObject:client forKey:client.family];
}
}
NSArray *arrayOfClients = [preferredTwitterClientByFamily allValues];
NSSet *availableTwitterClients = [NSSet setWithArray:arrayOfClients];
return availableTwitterClients;
}
- (void)openURLWithFormat:(NSString *)format string:(NSString *)aString {
NSString *encodedString = [aString stringByURLEncodingAllCharacters];
NSString *URLToOpen = [NSString stringWithFormat:format, encodedString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLToOpen]];
}
- (void)openWithTweet:(NSString *)aTweet {
[self openURLWithFormat:self.openWithTweetURLFormat string:aTweet];
}
- (void)openWithURL:(NSURL *)aURLToTweet {
NSString *format = self.openWithURLURLFormat;
if(!format) {
format = self.openWithTweetURLFormat;
}
NSString *stringForURL = [aURLToTweet absoluteString];
[self openURLWithFormat:format string:stringForURL];
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@ %p: %@>",
NSStringFromClass([self class]),
self,
self.displayName];
}
- (void)dealloc {
[family release];
family = nil;
[displayName release];
displayName = nil;
[canOpenURLTestURL release];
canOpenURLTestURL = nil;
[openWithTweetURLFormat release];
openWithTweetURLFormat = nil;
[openWithURLURLFormat release];
openWithURLURLFormat = nil;
[super dealloc];
}
@end