forked from facebook/facebook-ios-sdk
/
FBUtility.m
148 lines (125 loc) · 5.19 KB
/
FBUtility.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
/*
* Copyright 2012 Facebook
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "FBUtility.h"
#import "FBGraphObject.h"
#import "FBRequest.h"
#import "FBSBJSON.h"
#import "FBSession.h"
#include <sys/time.h>
@implementation FBUtility
// finishes the parsing job that NSURL starts
+ (NSDictionary*)dictionaryByParsingURLQueryPart:(NSString *)encodedString {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSArray *parts = [encodedString componentsSeparatedByString:@"&"];
for (NSString *part in parts) {
if ([part length] == 0) {
continue;
}
NSRange index = [part rangeOfString:@"="];
NSString *key;
NSString *value;
if (index.location == NSNotFound) {
key = part;
value = @"";
} else {
key = [part substringToIndex:index.location];
value = [part substringFromIndex:index.location + index.length];
}
if (key && value) {
[result setObject:[FBUtility stringByURLDecodingString:value]
forKey:[FBUtility stringByURLDecodingString:key]];
}
}
return result;
}
// the reverse of url encoding
+ (NSString*)stringByURLDecodingString:(NSString*)escapedString {
return [[escapedString stringByReplacingOccurrencesOfString:@"+" withString:@" "]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
+ (NSString*)stringByURLEncodingString:(NSString*)unescapedString {
NSString* result = (NSString *)CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault,
(CFStringRef)unescapedString,
NULL, // characters to leave unescaped
(CFStringRef)@":!*();@/&?#[]+$,='%’\"",
kCFStringEncodingUTF8);
[result autorelease];
return result;
}
+ (unsigned long)currentTimeInMilliseconds {
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000) + (time.tv_usec / 1000);
}
+ (NSTimeInterval)randomTimeInterval:(NSTimeInterval)minValue withMaxValue:(NSTimeInterval)maxValue {
return minValue + (maxValue - minValue) * (double)arc4random() / UINT32_MAX;
}
+ (id<FBGraphObject>)graphObjectInArray:(NSArray*)array withSameIDAs:(id<FBGraphObject>)item {
for (id<FBGraphObject> obj in array) {
if ([FBGraphObject isGraphObjectID:obj sameAs:item]) {
return obj;
}
}
return nil;
}
// The assumption here is that the view and the tableView share a common parent.
+ (void)centerView:(UIView*)view tableView:(UITableView*)tableView {
// We want to center the view in the table as much as possible, but we also want to center it
// within a cell so it is visually appealing.
CGRect bounds = tableView.bounds;
CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGFloat rowHeight = tableView.rowHeight;
int numRows = bounds.size.height / rowHeight;
int centerRow = numRows / 2;
center.y = rowHeight * centerRow + rowHeight / 2;
center = [view.superview convertPoint:center fromView:tableView];
view.center = center;
}
+ (NSString *)stringFBIDFromObject:(id)object {
if ([object isKindOfClass:[NSDictionary class]]) {
id val = [object objectForKey:@"id"];
if ([val isKindOfClass:[NSString class]]) {
return val;
}
}
return [object description];
}
+ (NSBundle *)facebookSDKBundle {
static dispatch_once_t fetchBundleOnce;
static NSBundle *bundle = nil;
dispatch_once(&fetchBundleOnce, ^{
NSString *path = [[NSBundle mainBundle] pathForResource:@"FacebookSDKResources"
ofType:@"bundle"];
bundle = [NSBundle bundleWithPath:path];
});
return bundle;
}
+ (NSString *)localizedStringForKey:(NSString *)key
withDefault:(NSString *)value {
return [self localizedStringForKey:key withDefault:value inBundle:FBUtility.facebookSDKBundle];
}
+ (NSString *)localizedStringForKey:(NSString *)key
withDefault:(NSString *)value
inBundle:(NSBundle *)bundle {
NSString *result = value;
if (bundle) {
result = [bundle localizedStringForKey:key
value:value
table:nil];
}
return result;
}
@end