Skip to content

Commit

Permalink
增加发微博功能
Browse files Browse the repository at this point in the history
  • Loading branch information
JimLiu committed Aug 12, 2013
1 parent 3992ef8 commit 8f6ba1e
Show file tree
Hide file tree
Showing 21 changed files with 349 additions and 13 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WeiboSDK

- 在你项目App的target设置, 找到 "Build Phases" 选项并打开 "Link Binary With Libraries":
- 点击 "+" 按钮,然后点击 "Add Other...", 浏览到WeiboSDK根目录的"build"目录,选择 "WeiboSDK.framework" 并添加到项目中
- 在你项目设置, 找到 "Build Settings" 选项,找到 "Other Linker Flags" 项,添加 `-ObjC`
- 在你项目设置, 找到 "Build Settings" 选项,找到 "Other Linker Flags" 项,添加值 `-ObjC`

#### 引用头文件

Expand Down Expand Up @@ -101,6 +101,34 @@ WeiboSDK
```
#### 发新微博
通过Weibo实例中的newStatus方法可以发表微博
不带附件发微博
```objective-c
[weibo newStatus:@"test weibo" pic:nil completed:^(Status *status, NSError *error) {
if (error) {
NSLog(@"failed to post:%@", error);
}
else {
NSLog(@"success: %lld.%@", status.statusId, status.text);
}
}];
```

带附件发微博
```objective-c
NSData *img = UIImagePNGRepresentation([UIImage imageNamed:@"Icon"]);
[weibo newStatus:@"test weibo with image" pic:img completed:^(Status *status, NSError *error) {
if (error) {
NSLog(@"failed to upload:%@", error);
}
else {
StatusImage *statusImage = [status.images objectAtIndex:0];
NSLog(@"success: %lld.%@.%@", status.statusId, status.text, statusImage.originalImageUrl);
}
}];
```
项目参考代码
----------
Expand Down
6 changes: 6 additions & 0 deletions build/WeiboSDK.framework/Versions/A/DeprecatedHeaders/Weibo.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum {
typedef void(^WeiboTimelineQueryCompletedBlock)(NSMutableArray *statuses, NSError *error);
typedef void(^WeiboUserQueryCompletedBlock)(User *user, NSError *error);
typedef void(^WeiboUserAuthenticationCompletedBlock)(WeiboAccount *account, NSError *error);
typedef void(^WeiboNewStatusCompletedBlock)(Status *status, NSError *error);


typedef enum {
Expand Down Expand Up @@ -76,5 +77,10 @@ typedef enum {
completed:(WeiboTimelineQueryCompletedBlock)completedBlock;


#pragma mark - Post

- (WeiboRequestOperation *)newStatus:(NSString *)status
pic:(NSData *)picData
completed:(WeiboNewStatusCompletedBlock)completedBlock;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
#define kWeiboAuthorizeURL @"https://api.weibo.com/oauth2/authorize"
#define kWeiboAccessTokenURL @"https://api.weibo.com/oauth2/access_token"
#define kWeiboAPIBaseUrl @"https://api.weibo.com/2/"
#define kWeiboUploadAPIBaseUrl @"https://upload.api.weibo.com/2/"



6 changes: 6 additions & 0 deletions build/WeiboSDK.framework/Versions/A/Headers/Weibo.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum {
typedef void(^WeiboTimelineQueryCompletedBlock)(NSMutableArray *statuses, NSError *error);
typedef void(^WeiboUserQueryCompletedBlock)(User *user, NSError *error);
typedef void(^WeiboUserAuthenticationCompletedBlock)(WeiboAccount *account, NSError *error);
typedef void(^WeiboNewStatusCompletedBlock)(Status *status, NSError *error);


typedef enum {
Expand Down Expand Up @@ -76,5 +77,10 @@ typedef enum {
completed:(WeiboTimelineQueryCompletedBlock)completedBlock;


#pragma mark - Post

- (WeiboRequestOperation *)newStatus:(NSString *)status
pic:(NSData *)picData
completed:(WeiboNewStatusCompletedBlock)completedBlock;

@end
2 changes: 2 additions & 0 deletions build/WeiboSDK.framework/Versions/A/Headers/WeiboConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
#define kWeiboAuthorizeURL @"https://api.weibo.com/oauth2/authorize"
#define kWeiboAccessTokenURL @"https://api.weibo.com/oauth2/access_token"
#define kWeiboAPIBaseUrl @"https://api.weibo.com/2/"
#define kWeiboUploadAPIBaseUrl @"https://upload.api.weibo.com/2/"



Binary file modified build/WeiboSDK.framework/Versions/A/WeiboSDK
Binary file not shown.
201 changes: 197 additions & 4 deletions samples/HelloWeiboSample/HelloWeiboSample.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion samples/HelloWeiboSample/HelloWeiboSample/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import <UIKit/UIKit.h>
#import <WeiboSDK/WeiboSDK.h>
#import "Weibo.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

Expand Down
26 changes: 26 additions & 0 deletions samples/HelloWeiboSample/HelloWeiboSample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
Weibo *weibo = [[Weibo alloc] initWithAppKey:@"3326691039" withAppSecret:@"75dd27596a081b28651d214e246c1b15"];
[Weibo setWeibo:weibo];
// Override point for customization after application launch.

if (weibo.isAuthenticated) {
/*
[weibo newStatus:@"test weibo" pic:nil completed:^(Status *status, NSError *error) {
if (error) {
NSLog(@"failed to post:%@", error);
}
else {
NSLog(@"success: %lld.%@", status.statusId, status.text);
}
}];
NSData *img = UIImagePNGRepresentation([UIImage imageNamed:@"Icon"]);
[weibo newStatus:@"test weibo with image" pic:img completed:^(Status *status, NSError *error) {
if (error) {
NSLog(@"failed to upload:%@", error);
}
else {
StatusImage *statusImage = [status.images objectAtIndex:0];
NSLog(@"success: %lld.%@.%@", status.statusId, status.text, statusImage.originalImageUrl);
}
}];
*/
}


return YES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
<string>${PRODUCT_NAME}</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIcons</key>
<dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>Icon.png</string>
<string>Icon@2x.png</string>
</array>
</dict>
</dict>
<key>CFBundleIdentifier</key>
<string>com.openlab.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion samples/HelloWeiboSample/HelloWeiboSample/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import <UIKit/UIKit.h>
#import <WeiboSDK/WeiboSDK.h>
#import "Weibo.h"

@interface ViewController : UITableViewController

Expand Down
Binary file added samples/HelloWeiboSample/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/HelloWeiboSample/Icon@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/WeiboSDK/Engine/WeiboRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ - (WeiboRequestOperation*)getFromPath:(NSString *)apiPath
- (WeiboRequestOperation*)postToPath:(NSString *)apiPath
params:(NSDictionary *)params
completed:(WeiboRequestCompletedBlock)completedBlock {
NSString * fullURL = [kWeiboAPIBaseUrl stringByAppendingString:apiPath];
NSString * fullURL = [apiPath hasSuffix:@"upload.json"] ? [kWeiboUploadAPIBaseUrl stringByAppendingString:apiPath] : [kWeiboAPIBaseUrl stringByAppendingString:apiPath];
return [self postToUrl:fullURL params:params completed:completedBlock];
}

Expand Down
3 changes: 2 additions & 1 deletion src/WeiboSDK/Engine/WeiboRequestOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
dispatch_async(self.queue, ^
{
NSError* error = nil;
//NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
id result = [self parseJsonResponse:self.responseData error:&error];

dispatch_async(dispatch_get_main_queue(), ^
Expand All @@ -236,7 +237,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

//NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
if (self.completedBlock)
{
self.completedBlock(nil, nil, error);
Expand Down
6 changes: 3 additions & 3 deletions src/WeiboSDK/Utilities/URLRequestHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ + (NSMutableURLRequest *)getRequestWithUrl:(NSString *)url

+ (NSMutableURLRequest *)postRequestWithUrl:(NSString *)url
params:(NSDictionary *)params {
NSString* urlString = [self serializeURL:url params:params];
NSURL* postURL = [NSURL URLWithString:url];

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlString] cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 30.f];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:postURL cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 30.f];
[request setHTTPMethod: @"POST"];
[request setValue: kUserAgent forHTTPHeaderField: @"User-Agent"];

Expand Down Expand Up @@ -159,7 +159,7 @@ + (NSMutableURLRequest *)postRequestWithUrl:(NSString *)url
requestData = [self buildURLEncodedPostBodyWithPostData:postData forRequest:request];
}
[request setValue: [NSString stringWithFormat: @"%d", [requestData length]] forHTTPHeaderField: @"Content-Length"];

[request setHTTPBody:requestData];
return request;
}

Expand Down
6 changes: 6 additions & 0 deletions src/WeiboSDK/Weibo.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum {
typedef void(^WeiboTimelineQueryCompletedBlock)(NSMutableArray *statuses, NSError *error);
typedef void(^WeiboUserQueryCompletedBlock)(User *user, NSError *error);
typedef void(^WeiboUserAuthenticationCompletedBlock)(WeiboAccount *account, NSError *error);
typedef void(^WeiboNewStatusCompletedBlock)(Status *status, NSError *error);


typedef enum {
Expand Down Expand Up @@ -76,5 +77,10 @@ typedef enum {
completed:(WeiboTimelineQueryCompletedBlock)completedBlock;


#pragma mark - Post

- (WeiboRequestOperation *)newStatus:(NSString *)status
pic:(NSData *)picData
completed:(WeiboNewStatusCompletedBlock)completedBlock;

@end
55 changes: 54 additions & 1 deletion src/WeiboSDK/Weibo.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ - (WeiboRequestOperation *)queryUserWithPath:(NSString *)path
completedBlock(user, nil);
}
@catch (NSException *exception) {
completedBlock(nil, [NSError errorWithDomain:WeiboErrorDomain code:kJsonParseUserErrorCode userInfo:@{NSLocalizedDescriptionKey: @"Failed to parse responsed object."}]);
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *msg = [NSString stringWithFormat:@"Failed to parse responsed object. responseString: %@", responseString];
completedBlock(nil, [NSError errorWithDomain:WeiboErrorDomain code:kJsonParseUserErrorCode userInfo:@{NSLocalizedDescriptionKey: msg}]);
}
@finally {

Expand Down Expand Up @@ -236,4 +238,55 @@ - (WeiboRequestOperation *)queryTimeline:(StatusTimeline)timeline
}


#pragma mark - Post


- (WeiboRequestOperation *)newStatus:(NSString *)status
pic:(NSData *)picData
completed:(WeiboNewStatusCompletedBlock)completedBlock {
NSString *path = picData.length ? @"statuses/upload.json" : @"statuses/update.json";
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:status forKey:@"status"];
if (picData.length) {
[params setObject:picData forKey:@"pic"];
}
WeiboRequestOperation *operation =
[[WeiboRequest shared] postToPath:path
params:params
completed:^(id result, NSData *data, NSError *error) {
if (error) {
completedBlock(nil, error);
}
else {
Status *status = nil;
if ([result isKindOfClass:[NSDictionary class]]) {
@try {
status = [[Status alloc] initWithJsonDictionary:result];
}
@catch (NSException *exception) {
NSLog(@"newStatus exception: %@", exception);
}
@finally {

}
}
if (status) {
completedBlock(status, nil);
}
else {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *msg = [NSString stringWithFormat:@"Failed to parse responsed object. responseString: %@", responseString];

completedBlock(nil, [NSError errorWithDomain:WeiboErrorDomain code:kJsonParseTimelineErrorCode userInfo:@{NSLocalizedDescriptionKey: msg}]);
}
}


}];
return operation;


}


@end
2 changes: 2 additions & 0 deletions src/WeiboSDK/WeiboConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
#define kWeiboAuthorizeURL @"https://api.weibo.com/oauth2/authorize"
#define kWeiboAccessTokenURL @"https://api.weibo.com/oauth2/access_token"
#define kWeiboAPIBaseUrl @"https://api.weibo.com/2/"
#define kWeiboUploadAPIBaseUrl @"https://upload.api.weibo.com/2/"



0 comments on commit 8f6ba1e

Please sign in to comment.