關(guān)于具體用法,這幾篇博客已經(jīng)做了大致總結(jié):
http://www.reibang.com/p/be79b8729bf8
http://www.cocoachina.com/ios/20160919/17593.html
其中iMessage App的用法大體上分兩種: Stickers與Interactive Messages。其中Stickers類似于表情包,非常的簡(jiǎn)單耙箍,也沒什么坑寒波。
Interactive Messages雖然內(nèi)容也不多,但目前為止能找到的資料還是太少头朱,所以我用這篇博客來記錄一下自己在開發(fā)過程中遇到的一些坑座享。
當(dāng)Container App未上架:在接收方?jīng)]有安裝Container App的情況下婉商,無論我把MSMessage對(duì)象的URL屬性設(shè)置成什么,對(duì)方都會(huì)在iMessage內(nèi)部打開App Store首頁(yè)渣叛。發(fā)送方在發(fā)送message后如果刪除Container App之后也會(huì)遇到相同情況丈秩。
當(dāng)Container App已上架:接收方?jīng)]有安裝Container App的情況下,接收方點(diǎn)擊message之后會(huì)在iMessage內(nèi)部打開App Store并跳轉(zhuǎn)到下載Container App的下載頁(yè)淳衙。
Interactive Messages是不支持Universal Links的蘑秽,所以上面謀篇博客中說可以使用Universal Links進(jìn)行跳轉(zhuǎn),應(yīng)該是想當(dāng)然了箫攀。
4.iMessage只能通過URL Scheme的方式打開自己的Containing App肠牲,無法打開其他App,且url的shceme沒有時(shí)靴跛,無法打開任何app缀雳。哪怕Containing App沒有配置URL Scheme,iMessage依舊可以打開梢睛,url的scheme只要不為空肥印,則只能打開Containing App。
這里是蘋果對(duì)
- (void)openURL:(NSURL *)URL completionHandler:(void (^)(BOOL success))completionHandler;
這個(gè)方法的解釋:
Each extension point determines whether to support this method, or under which conditions to support this method. In iOS 8, only the Today extension point (used for creating widgets) supports this method.
Important
Apple allows a widget to use the openURL:completionHandler: method to open the widget’s own containing app.
If you employ this method to open other apps from your widget, your App Store submission might entail additional review to ensure compliance with the intent of widgets.
To learn more, read App Store Review Guidelines and iOS Human Interface Guidelines, linked to from Apple’s App Review Support page
看樣子绝葡,在iOS8的時(shí)候深碱,today可以通過這個(gè)方法調(diào)起其他的App,但是蘋果覺得不妥藏畅,就通過審核的方式禁止敷硅,在后來的某個(gè)版本中,蘋果直接不管你的URL墓赴,只要scheme不為空竞膳,就只能打開Containing App
- 假設(shè)接收方裝了Container App航瞭,那么我們最希望的場(chǎng)景應(yīng)該是對(duì)方點(diǎn)擊message之后跳轉(zhuǎn)到自己的App诫硕。于是非常自然的會(huì)想到在這兩個(gè)方法中做一些處理:
-(void)willSelectMessage:(MSMessage *)message conversation:(MSConversation *)conversation;
-(void)didSelectMessage:(MSMessage *)message conversation:(MSConversation *)conversation;
然后就會(huì)驚喜的發(fā)現(xiàn):第一次點(diǎn)擊是OK的,順利的跳轉(zhuǎn)到了Container App刊侯,然后回來再點(diǎn)擊卻發(fā)現(xiàn)沒卵用了章办,不是打開Container App,而是打開iMessage App。而且通過打斷點(diǎn)發(fā)現(xiàn)毫無反應(yīng)藕届。這是因?yàn)楫?dāng)調(diào)轉(zhuǎn)到Container App之后iMessage App就已經(jīng)退出了挪蹭,可以試試在這兩個(gè)方法中打Log試試:
-(void)willResignActiveWithConversation:(MSConversation *)conversation;
-(void)didResignActiveWithConversation:(MSConversation *)conversation;
既然點(diǎn)擊message后會(huì)喚起iMessage App,那么不妨在這兩個(gè)方法中加入一些邏輯:
-(void)willBecomeActiveWithConversation:(MSConversation *)conversation;
-(void)didBecomeActiveWithConversation:(MSConversation *)conversation;
為了選擇能夠盡早進(jìn)入Container App休偶,我是這么寫的:
-(void)willBecomeActiveWithConversation:(MSConversation *)conversation {
if (conversation.selectedMessage) {
[self.extensionContext openURL:[NSURL URLWithString:@"containerAppDemo://"] completionHandler:nil];
}
}
- 對(duì)于試圖利用AppDelegate對(duì)象在Extension中試圖使用URL Schemers跳轉(zhuǎn)到自家Container App的同學(xué)梁厉,不妨這么寫:
if (conversation.selectedMessage) {
[self.extensionContext openURL:[NSURL URLWithString:@"containerAppDemo://"] completionHandler:nil];
絕大部分Extension的UIViewController都有這個(gè)屬性
@interface UIViewController(NSExtensionAdditions) <NSExtensionRequestHandling>
// Returns the extension context. Also acts as a convenience method for a view controller to check if it participating in an extension request.
@property (nullable, nonatomic,readonly,strong) NSExtensionContext *extensionContext NS_AVAILABLE_IOS(8_0);
@end
本人還是小菜雞一枚,還望各位大神不吝賜教踏兜。