Most blogging systems support the newMediaObject call. It simply uploads an file to a designated location on the server. You get a URL to the object back, which you can embed in your post. First things, first. Lets reopen the XCODE project and modify the MetaWeblog class.
#import <Cocoa/Cocoa.h>
#import “Post.hâ€
@interface MetaWeblog : NSObject {
NSString *url;
NSString *username;
NSString *password;
NSString *blogId;
}
@property(readwrite,assign) NSString *url;
@property(readwrite,assign) NSString *username;
@property(readwrite,assign) NSString *password;
@property(readwrite,assign) NSString *blogId;
- (id) initWithArgs: (NSString*)theUrl
andUsername: (NSString*) theUsername
andPassword: (NSString*) thePassword
andBlogId: (NSString*) theBlogId;
- (NSString*)newPost: (Post*) post;
- (NSString*)newMediaObject: (NSString*)name
andType: (NSString*) type
andBits:(NSData*) bits;
@end
The implementation of newMediaObject is very similar to that of newPost. First, we setup the parameters, create and invoke the XMLRPC method and check results. Big deal. The beauty of CoreServices is that it takes care of everything. Absolutely sweet.
#import “MetaWeblog.hâ€
@implementation MetaWeblog
@synthesize url;
@synthesize username;
@synthesize password;
@synthesize blogId;
// other methods of the MetaWeblog class.
- (NSString*)newMediaObject: (NSString*)name andType: (NSString*) type andBits:(NSData*) bits
{
WSMethodInvocationRef rpcCall;
NSURL *rpcURL = [NSURL URLWithString: url];
NSString *methodName = @“metaWeblog.newMediaObjectâ€;
NSArray *postKey = [NSArray arrayWithObjects: @“nameâ€, @“typeâ€, @“bitsâ€, nil];
NSArray *postObject = [NSArray arrayWithObjects: name, type, bits, nil];
NSDictionary *post = [NSDictionary dictionaryWithObjects:postObject forKeys:postKey];
NSArray *keys = [NSArray arrayWithObjects: @“blog_ID†, @“usernameâ€, @“passwordâ€, @“postâ€, nil];
NSMutableArray *objects = [NSArray arrayWithObjects: blogId, username, password, post, nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSArray *order = [NSArray arrayWithObjects: @“blog_IDâ€, @“usernameâ€, @“passwordâ€, @“postâ€, nil];
rpcCall = WSMethodInvocationCreate ((CFURLRef) rpcURL, (CFStringRef) methodName, kWSXMLRPCProtocol);
WSMethodInvocationSetParameters (rpcCall, (CFDictionaryRef) params, (CFArrayRef) order);
NSDictionary *result = (NSDictionary *) (WSMethodInvocationInvoke (rpcCall));
NSLog(@“[metaWeblog.newMediaObject] result = %@“, result);
NSDictionary *values = [result objectForKey:(NSString*)kWSMethodInvocationResult ];
return (NSString*) [values objectForKey: @“urlâ€];
}
@end
So lets use it.
#import <Cocoa/Cocoa.h>
#import “MetaWeblog.hâ€
int main(int argc, char *argv[])
{
NSString* url = @“https://example.com/index.php?ACT=52&id=82â€;
NSString* username = @“Klaasâ€;
NSString* password = @“Vakie!“;
NSString* blogId = @“13â€;
// allocate resources
MetaWeblog* blog = [[MetaWeblog alloc] initWithArgs: url
andUsername:username
andPassword:password
andBlogId:blogId];
Post* post = [[Post alloc] init];
NSData* data = [NSData dataWithContentsOfFile: @“/Users/Bloudraak/Pictures/TantSannie.jpgâ€];
NSString* mediaUrl = [blog newMediaObject: @“TantSannie.jpg†andType: @“image/jpg†andBits:data];
// create post
post.title = "My first post";
post.summary = [NSString stringWithFormat:"<img src="%@" />", mediaUrl];post.description = @“Hello Tant Sannie; life is lekker hereâ€; [blog newPost:post]; // free resources [post release]; [blog release]; [data release]; return 0; }
The code should be self explanatory. First, we load the contents of the image, then upload it using newMediaObject. The result is a url which we convert into an IMG tag and add to the summary of the blog. You can now do all sorts of styling, if you wish, to ensure the image is correctly displayed. This gives me the minimum functionality to get started with my Aperture Plug-In. That said, expect this to be expanded in the near future.
.
Comments