1.添加Widget讓APP數(shù)據(jù)互通是報錯處理
xcode->targets中的主程序中的bundle ID和today extension中的bundle id都要在開發(fā)者賬號下創(chuàng)建,且開發(fā)者賬號還要創(chuàng)建一個App Groups,否則會報錯,如圖:


參考網(wǎng)址https://onevcat.com/2014/08/notification-today-widget/
2.widget中添加空間的方式(storyboard或者故事板)
添加widget系統(tǒng)生成的info.plist文件,默認是使用Storyboard 實現(xiàn)的界面;如果你想使用代碼實現(xiàn)是界面的搭建,需更改這個配置文件,如圖3:

在TodayViewController中的ViewDidLoad中添加:preferredContentSize = CGSizeMake(0, 80)//在設(shè)定 preferredContentSize 時,指定的寬度都是無效的,系統(tǒng)會自動將其處理為整屏的寬度,所以扔個 0 進去就好了,
在TodayViewController中添加:
func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 0, 0)
}
參考網(wǎng)址https://www.ishuo.cn/doc/ermjmiqf.html
3.widget從Assets中添加圖片
widget系統(tǒng)生成的info.plist文件中,改成storyboard添加控件的方式,將圖3中的info.plist文件修改一下
在xcode中選擇Assets.xcassets文件,在xcode最右側(cè)選擇show the file inspector,選擇target Membership,勾選項目中對應的today extension,結(jié)果如圖:

然后按鈕在storyboard中就可以添加Assets.xcassets中的image
參考網(wǎng)址http://stackoverflow.com/questions/33150654/image-not-appearing-in-today-extension-widget
4.添加Extension的PP證書
開發(fā)者賬號下不但要創(chuàng)建一個Extension的App ID,還要創(chuàng)建一個Extension的PP證書安裝到Xcode上
PP證書的作用就是將前面所有的證書集成于PP證書中安裝到真機上