I found most iMessage app tutorial about iMessage extension development is in Swift, since I am a beginner at objective-c and have to implement an iMessage extension quickly I want to code with Objective-C I find MSStickerBrowserViewController is some how like UITableViewController cause it uses a datasrouce protocol right?
So I implement my iMessage extension app like this:
@interface MessagesViewController ()<MSStickerBrowserViewDataSource>
@property (strong, nonatomic) NSMutableArray *stickerList;
@end
I have already implemented the
stickerBrowserView:(MSStickerBrowserView *)stickerBrowserView stickerAtIndex:(NSInteger)index
and the
(NSInteger) numberOfStickersInStickerBrowserView:(MSStickerBrowserView *)stickerBrowserView
method and init the stickerList as a property of the viewController
and I think it will be fine to add a MSStickerViewController and made the view of it as the subview of the root view, then my work is done
MSStickerBrowserViewController *stickerBrowserViewController = [[MSStickerBrowserViewController alloc]initWithStickerSize:MSStickerSizeRegular];
stickerBrowserViewController.view.backgroundColor = [UIColor redColor];
[self.view addSubview:stickerBrowserViewController.view];
But after doing that the only thing I got is a red content(which indicts the view of the MSStickerBrowserViewController did have been added into the rootview)
But none of my images were shown as a sticker
I thought there maybe lack of some hooks of the dataresource to the MSStickerBrowserViewController, but I can not find a delegate or resource attribute to hook them.
I also notice that there is some override stuff to do that with swift code, but do I have to make a subclass of MSStrickerBrowserViewController and implement the protocol of MSStickerBrowserViewDataSource in the subclass to do that?
So how to make this work, I mean with Objective-C code?
The problem has to do with implementing the
MSStickerBrowserViewDataSourceprotocol inMessagesViewControllerbut then creating a separate instance ofMSStickerBrowserViewController. That's fine so far, but as you pointed out, you're not setting the data source onMSStickerBrowserViewControllerto your class implementing theMSStickerBrowserViewDataSourceprotocol:MessagesViewController.The
MSStickerBrowserViewControllerclass has a view of typeMSStickerBrowserViewand it's calledstickerBrowserView. The property you're looking for isdataSourceand described here. In the context of your code, that would be:stickerBrowserViewController.stickerBrowserView.dataSourceI could not say for certain why the Framework Engineers didn't decide call
stickerBrowserViewjustview, and why thestickerBrowserViewdata source isn't calleddelegatebut perhaps they wanted to draw a very clear distinction between the functionality ofstickerBrowserViewversus regularUIView.As you allude to in your question, an alternate approach would be to sub-class
MSStickerBrowserViewControllerand then implement the data-source methods in that sub-class. With the first approach, you may run into issues with having two classes that inherit fromUIViewControllerbeing on screen at the same time. It can be done, but it's probably not necessary in this case. For your particular circumstance, I would recommend going the second route and sub-classingMSStickerBrowserViewControllerinstead of using yourMessagesViewController.