Weixin (微信) is Tencent's mobile messaging product. It's highly popular in China, with hundreds of millions of users, as well as many international users who may know it by its English name WeChat. It includes both a chat feature, similar to WhatsApp, and a timeline feature similar to Path.
Tencent have a developer site in English, but the documentation is still quite sparse.

For a recent project, we needed to integrate sharing of images to Weixin.
The workflow for the user will be:
1. Create an image in the app
2. Tap a button to share to Weixin
3. The Weixin app will launch, and the user confirms they want to add the image to their "Moments".
4. The user taps a button in the Weixin app to return to our app.
STEP 1: Download and add the iOS SDK
1. Download the latest iOS SDK from the WeChat developer site. After unzipping you should have four files:

2. If you're building with the latest Xcode and targeting iPhone 5, you'll probably want to use the version of the library which supports armv7s, so copy WXApi.h, WXApiObject.h and libWeChatSDK_armv7_armv7s.a to your project folder. Rename libWeChatSDK_armv7_armv7s.a to libWeChatSDK.a.
3. Now in your XCode project go to File > Add Files and add the three files to your project.
STEP 2: Register for an app id
1. Register as a WeChat developer using the signup form. Once you've confirmed your email address, head to the My Apps page and tap "Register my apps"
2. Fill out the name of your app and other details. You can skip some optional fields like icon for now. Make sure you choose "Mobile app" as the app type, not Web app.
3. At the end of the process you will be given an app id which looks like wx123456789012
STEP 3: Integrate with the SDK
1. In your application:didFinishLaunchingWithOptions: method, add code to register your app. Be sure to replace wx123456789012 with the code you obtained earlier.
if (![WXApi registerApp:@"wx123456789012"]) {
NSLog(@"Failed to register with Weixin");
}
2. After the Weixin app is launched it needs a way to re-launch your app. It does this by trying to open a URL with your app id as the protocol, for example wx123456789012://. We need to ensure we can handle these URLs, so implement these two methods:
-
(BOOL)application:
(UIApplication *
)application handleOpenURL:
(NSURL *
)url
{return [WXApi handleOpenURL:url delegate:self];
}
-
(BOOL)application:
(UIApplication *
)application openURL:
(NSURL *
)url sourceApplication:
(NSString *
)sourceApplication annotation:
(id)annotation
{ return [WXApi handleOpenURL:url delegate:self];
}
3. We need to ensure that our app delegate implements WXApiDelegate, so first add WXApiDelegate to your AppDelegate.h file:
@interface AppDelegate : UIResponder<WXApiDelegate>
and then add empty implementations of these two callbacks in the .m:
- (void) onReq:(BaseReq*)req {
}
- (void) onResp:(BaseResp*)resp {
}
4. We also need to make a change in the Info.plist to ensure we can handle these kind of URLs. Go to your target, select the Info tab and add a new URL type as follows. The identifier can be "weixin" and the URL scheme should match your app id.

5. Now let's add a method to send an image.
- (void) sendImageContentToWeixin:(UIImage *)image {
//if the Weixin app is not installed, show an error
if (![WXApi isWXAppInstalled]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"The Weixin app is not installed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
return;
}
//create a message object
WXMediaMessage *message = [WXMediaMessage message];
//set the thumbnail image. This MUST be less than 32kb, or sendReq may return NO.
//we'll just use the full image resized to 100x100 pixels for now
[message setThumbImage:[image resizedImage:CGSizeMake(100,100) interpolationQuality:kCGInterpolationDefault]];
//create an image object and set the image data as a JPG representation of our UIImage
WXImageObject *ext = [WXImageObject object];
ext.imageData = UIImageJPEGRepresentation(image, 0.8);
message.mediaObject = ext;
//create a request
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
//this is a multimedia message, not a text message
req.bText = NO;
//set the message
req.message = message;
//set the "scene", WXSceneTimeline is for "moments". WXSceneSession allows the user to send a message to friends
req.scene = WXSceneTimeline;
//try to send the request
if (![WXApi sendReq:req]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
6. Add callback code. This will be called once control is returned to our app after returning from Weixin.
- (void) onResp:(BaseResp*)resp {
if([resp isKindOfClass:[SendMessageToWXResp class]]) {
NSLog(@"Response from Weixin was: %@",strMsg);
}
}
STEP 4: Test!
All being well, you should now be able to share an image from your code, using something like:
UIImage *test = [UIImage imageNamed:@"test.png"];
[self sendImageContentToWeixin:test];
After sharing the image, you should be returned to your app.
If you found this tutorial useful, or have any suggestions, do leave a comment!