iOS 插件开发之 today widget

Dmo链接

一、什么是today widget

苹果官方关于 today widget 的介绍(点击链接查看详情)

 App extensions in the Today view are called widgets. Widgets give users quick access to information that’s important right now. For example, users open the Today view to check current stock prices or weather conditions, see today’s schedule, or perform a quick task such as marking an item as done. Users tend to open the Today view frequently, and they expect the information they’re interested in to be instantly available.

A Today widget can appear on the lock screen of an iOS device if the user has enabled this. They do so in the “Allow Access When Locked” area by going to Settings > Touch ID & Passcode > Notifications View.

      今日视图中的 app扩展叫做widget ,widget 可以为用户提供当前所需简明扼要的信息 ,例如用户打开今日查看股票价格或者天气状况,今天的日程表,待办事等等。用户趋向于频繁的打开widget 以便获取最新的重要的信息。同时用户能够设置中是否允许  today widget  在锁屏上出现,。

二、创建一个带today widget 的工程

打开一个已经创建好的程序,选择File>New>Target> Today Extension    操作如下图      图2-1  图2-2

图2-1
图2-2

创建完成之后,我们可以在工程里面看到这几个文件。

创建完成之后

      today widget 插件是依靠于宿主程序存在的,宿主程序就是普通创建的程序。

三、today widget插件和宿主程序之间的数据共享

苹果官方关于插件和宿主程序之常见场景的处理文档

Sharing Data with Your Containing App

Even though an app extension bundle is nested within its containing app’s bundle, the running app extension and containing app have no direct access to each other’s containers.

You can, however, enable data sharing. For example, you might want to allow your app extension and its containing app to share a single large set of data, such as prerendered assets.

To enable data sharing, use Xcode or the Developer portal to enable app groups for the containing app and its contained app extensions. Next, register the app group in the portal and specify the app group to use in the containing app. To learn about working with app groups, seeAdding an App to an App Group.

After you enable app groups, an app extension and its containing app can both use theNSUserDefaultsAPI to share access to user preferences. To enable this sharing, use theinitWithSuiteName:method to instantiate a new NSUserDefaultsobject, passing in the identifier of the shared group. For example, a Share extension might update the user’s most recently used sharing account, using code like this:

// Create and share access to an NSUserDefaults object

NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"com.example.domain.MyShareExtension"];

// Use the shared user defaults object to update the user's account

[mySharedDefaults setObject:theAccountName forKey:@"lastAccountName"];

    虽然扩展程序的包是依赖于宿主程序的包存在的,但是扩展程序是不能直接访问宿主程序的沙盒。

    但是你仍然可以,通过开启 宿主程序的 appGroup 来使宿主程序和扩展程序之间完成数据共享。当你开启app group 之后扩展程序和宿主程序可以使用NSUserDefaults  的initWithSuiteName方法来存储数据。

例如 :NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"com.example.domain.MyShareExtension"];

[mySharedDefaults setObject:theAccountName forKey:@"lastAccountName"];

给宿主app设置appGroup 的方法如下图 3-1 所示

图 3-1

然后点击加加号在弹出框里面输入appGroup 的名字。如下图3-2

图3-2

添加完成之后如下图3-3

图3-3

然后打开插件的appGroup 如下图 图3-4

图3-4

创建完成之后,我们就可以将数据存入appGroup中,方便插件和宿主程序共享。在本次示例中,我将采用对象归档,解档的方式将数据缓存到本地,数据模型在实现<NSCoding>协议之后可以通过如下的方法将数据缓存到本地

NSURL*mURL = [[NSFileManagerdefaultManager] containerURLForSecurityApplicationGroupIdentifier:@"appGroup 的名称 (如上面的 "group.STTodyaWidget.data")"];

mURL = [mURL URLByAppendingPathComponent:@"保存的名称"];

Bool  isSaveSuccess = ([NSKeyedArchiver archiveRootObject:xxxmodel toFile:mURL.path]&& result);

从本地取出数据

XXXModel   *xxxmodel =[NSKeyedUnarchiver unarchiveObjectWithFile:mURL.path];

由于插件和宿主app 是两个相互独立的程序,所以,在插件中是不能直接访问宿主程序的类,如果要创建一个插件和宿主程序都要使用的类可以勾选下面的勾选框如图3-5

图3-5

如果插件需要用到宿主程序里面之前建好的类的话,只需要,选中类将下图中的勾选框勾选上就可以了如下图 图3-6

图3-6

四、today widget插件的布局

Because user interaction with Today widgets is quick and limited, you should design a simple, streamlined UI that highlights the information users are interested in. In general, it’s a good idea to limit the number of interactive items in a widget. In particular, note that iOS widgets don’t support keyboard entry.

NOTE:

Avoid putting a scroll view inside a Today widget. It’s difficult for users to scroll within a widget without inadvertently scrolling the Today view.

iOS.Because Today widgets don’t allow keyboard entry, users need to be able to use the containing app to configure a widget’s content and behavior. In the Stocks widget, for example, users can switch between different representations of a symbol’s value, but they must open the Stocks app to manage the list of symbols.

today widget  的设计应该遵从能够以简洁的界面为用户提供高质量的信息的原则,today widget 不允许键盘的弹出。尽量不要将scrollview 放到widget 上因为用户很难在这个页面进行滑动。

 4.1 使用sb

我们在创建完today widget 之后,Xcode 会为我们创建好MainInterface.storyboard。我们可以使storyboard 来进行布局。如下图 图4-1

图4-1

运行效果如下 图4-2

图4-2

 在viewDidLoad中添加这段代码可以使用折叠效果

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {

self.extensionContext.widgetLargestAvailableDisplayMode = NCWidgetDisplayModeExpanded;

}

然后实现代理方法

- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize

{

if (activeDisplayMode == NCWidgetDisplayModeCompact) {

self.preferredContentSize = maxSize;

} else if (activeDisplayMode == NCWidgetDisplayModeExpanded) {

CGFloat height = 你要设置的展开时候的高度;

self.preferredContentSize = CGSizeMake(0,height );

}

}

展开折叠效果

如果在10以下的系统我们需要在-(void)viewWillAppear:(BOOL)animated;里面设置today widget 的高度。

if (stsystemVersion < 10.0) {

CGFloat height = self.eventListModel.eventList.count * cellH ;

height        = height > cellH ? height : cellH;

[self setPreferredContentSize:CGSizeMake(0, height+ 30)];

}

如果你想设置today widget 的内边距可以在这个代理方法中设置

- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets

{

return  UIEdgeInsetsMake(0, 40, 0, 0);

//    return UIEdgeInsetsZero;

}

 4.2使用纯代码

五、today widget跳转到宿主程序

打开宿主程序的 plist 文件 添加URL types 如下图

然后在today widget 中添加这段代码,然后就可以从widget跳转到宿主程序

[self.extensionContext openURL:[NSURL URLWithString:@"STTodayWidget://GOTOEventListVC"] completionHandler:^(BOOL success) {

NSLog(@"open url result:%d", success);

}];


在appdelegate 中添加下面的方法

// 9.0 以后

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options {

}

// 9.0 以前

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

}

六、today widget的网络请求

七、today widget的国际化

八、today widget的打包上线



Demo链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,585评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,923评论 1 301
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,314评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,346评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,718评论 3 291
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,828评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,020评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,758评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,486评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,722评论 2 251
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,196评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,546评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,211评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,132评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,916评论 0 200
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,904评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,758评论 2 274

推荐阅读更多精彩内容