如何在自家应用内直接唤起微信Share Extension

大家都知道,Extension是需要Host App(宿主应用)来唤起的。比如微信的分享扩展就可以在照片应用和Safari上唤起。 那我们如何在自己的应用内唤起微信呢?其实别人家14年都写了的文章了。http://indiestack.com/2014/11/share-extension-iterations/

UIActivity​View​Controller

http://nshipster.com/uiactivityviewcontroller/

我们得先来看看UIActivity​View​Controller,我们在Safari看到的分享面板就是这玩意儿咯。唤起share extension 都是通过UIActivityViewController。


image.png

SLComposeViewController

这个是微信Share Extension弹框的VC。 这个类composeViewControllerForServiceType的方法,通过名字直接拿到对应VC。微信分享组件的名字不方便透露,大家可以通过对UIActivityViewController回调方法的研究拿到。

NSString* serviceName = @"........";
SLComposeViewController* myController = [SLComposeViewController composeViewControllerForServiceType:serviceName];
[myController setInitialText:@"Hello World"];
[[self navigationController] presentViewController:myController animated:YES completion:nil];";
SLComposeViewController* myController = [SLComposeViewController composeViewControllerForServiceType:serviceName];
[myController setInitialText:@"Hello World"];
[[self navigationController] presentViewController:myController animated:YES completion:nil];

用下面两个方法,带上自己的图片或者url。

// Adds an image to the post. Returns NO if the additional image will not fit
// within the character space currently available, or if the sheet has already
// been presented to the user.
- (BOOL)addImage:(UIImage *)image;

// Adds a URL to the post. Returns NO if the additional URL will not fit
// within the character space currently available, or if the sheet has already
// been presented to the user.
- (BOOL)addURL:(NSURL *)url;

推荐阅读更多精彩内容