Using MSStickerBrowserViewController with objective c

290 Views Asked by At

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?

1

There are 1 best solutions below

0
On

The problem has to do with implementing the MSStickerBrowserViewDataSource protocol in MessagesViewController but then creating a separate instance of MSStickerBrowserViewController. That's fine so far, but as you pointed out, you're not setting the data source on MSStickerBrowserViewController to your class implementing the MSStickerBrowserViewDataSource protocol: MessagesViewController.

The MSStickerBrowserViewController class has a view of type MSStickerBrowserView and it's called stickerBrowserView. The property you're looking for is dataSource and described here. In the context of your code, that would be: stickerBrowserViewController.stickerBrowserView.dataSource

I could not say for certain why the Framework Engineers didn't decide call stickerBrowserView just view, and why the stickerBrowserView data source isn't called delegate but perhaps they wanted to draw a very clear distinction between the functionality of stickerBrowserView versus regular UIView.

As you allude to in your question, an alternate approach would be to sub-class MSStickerBrowserViewController and 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 from UIViewController being 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-classing MSStickerBrowserViewController instead of using your MessagesViewController.

@interface MessagesViewController ()<MSStickerBrowserViewDataSource>
@property (strong, nonatomic) NSMutableArray *stickerList;
@end

MSStickerBrowserViewController *stickerBrowserViewController = [[MSStickerBrowserViewController alloc]initWithStickerSize:MSStickerSizeRegular];
stickerBrowserViewController.view.backgroundColor = [UIColor redColor];

// Missing Line
stickerBrowserViewController.stickerBrowserView.dataSource = self;

[self.view addSubview:stickerBrowserViewController.view];