關于具體用法,這幾篇博客已經做了大致總結:
http://www.lxweimin.com/p/be79b8729bf8
http://www.cocoachina.com/ios/20160919/17593.html
其中iMessage App的用法大體上分兩種: Stickers與Interactive Messages。其中Stickers類似于表情包,非常的簡單,也沒什么坑。
Interactive Messages雖然內容也不多,但目前為止能找到的資料還是太少,所以我用這篇博客來記錄一下自己在開發過程中遇到的一些坑。
當Container App未上架:在接收方沒有安裝Container App的情況下,無論我把MSMessage對象的URL屬性設置成什么,對方都會在iMessage內部打開App Store首頁。發送方在發送message后如果刪除Container App之后也會遇到相同情況。
當Container App已上架:接收方沒有安裝Container App的情況下,接收方點擊message之后會在iMessage內部打開App Store并跳轉到下載Container App的下載頁。
Interactive Messages是不支持Universal Links的,所以上面謀篇博客中說可以使用Universal Links進行跳轉,應該是想當然了。
4.iMessage只能通過URL Scheme的方式打開自己的Containing App,無法打開其他App,且url的shceme沒有時,無法打開任何app。哪怕Containing App沒有配置URL Scheme,iMessage依舊可以打開,url的scheme只要不為空,則只能打開Containing App。
這里是蘋果對
- (void)openURL:(NSURL *)URL completionHandler:(void (^)(BOOL success))completionHandler;
這個方法的解釋:
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的時候,today可以通過這個方法調起其他的App,但是蘋果覺得不妥,就通過審核的方式禁止,在后來的某個版本中,蘋果直接不管你的URL,只要scheme不為空,就只能打開Containing App
- 假設接收方裝了Container App,那么我們最希望的場景應該是對方點擊message之后跳轉到自己的App。于是非常自然的會想到在這兩個方法中做一些處理:
-(void)willSelectMessage:(MSMessage *)message conversation:(MSConversation *)conversation;
-(void)didSelectMessage:(MSMessage *)message conversation:(MSConversation *)conversation;
然后就會驚喜的發現:第一次點擊是OK的,順利的跳轉到了Container App,然后回來再點擊卻發現沒卵用了,不是打開Container App,而是打開iMessage App。而且通過打斷點發現毫無反應。這是因為當調轉到Container App之后iMessage App就已經退出了,可以試試在這兩個方法中打Log試試:
-(void)willResignActiveWithConversation:(MSConversation *)conversation;
-(void)didResignActiveWithConversation:(MSConversation *)conversation;
既然點擊message后會喚起iMessage App,那么不妨在這兩個方法中加入一些邏輯:
-(void)willBecomeActiveWithConversation:(MSConversation *)conversation;
-(void)didBecomeActiveWithConversation:(MSConversation *)conversation;
為了選擇能夠盡早進入Container App,我是這么寫的:
-(void)willBecomeActiveWithConversation:(MSConversation *)conversation {
if (conversation.selectedMessage) {
[self.extensionContext openURL:[NSURL URLWithString:@"containerAppDemo://"] completionHandler:nil];
}
}
- 對于試圖利用AppDelegate對象在Extension中試圖使用URL Schemers跳轉到自家Container App的同學,不妨這么寫:
if (conversation.selectedMessage) {
[self.extensionContext openURL:[NSURL URLWithString:@"containerAppDemo://"] completionHandler:nil];
絕大部分Extension的UIViewController都有這個屬性
@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
本人還是小菜雞一枚,還望各位大神不吝賜教。