热门搜索 :
考研考公
您的当前位置:首页正文

iOS8中Today Extension(Widget)的使用

来源:东饰资讯网

扩展(Extension)是iOS 8中新引入的特性。iOS 8系统有6个支持扩展的系统区域,分别是

  • 今日插件(Today widget)
  • 分享(Share)
  • 操作(Action)
  • 图片编辑(Photo Editing)
  • 文档管理(Document Provider)
  • 自定义键盘(Custom keyboard)
E221C469511CDE6687B26BC7F6A49362.png
Widget的创建直接在targets里点击下边添加就可以了
3CE808F1-1E39-4892-886A-DD00386682AF.png
Xcode6新建的是自带SB的,直接在模拟器运行,注意图中箭头处 379887E7-8DF6-4C99-892D-E85138424B3A.png

运行效果,熟悉的Hello World

B8A41B2A-B787-45A7-BF35-CFA822F561C2.png

博主习惯了没有用SB,修改Info.plist文件,删除SB即可


094C49C8-5D98-49AF-BE78-043E645535E1.png

在TodayViewController的- (void)viewDidLoad方法种添加

self.preferredContentSize = CGSizeMake(0, 200);
self.view.backgroundColor = [UIColor redColor];

运行是如图效果


7EE8AABD-8624-4BBA-949F-1E99446510A9.png

值得注意:
1、尽量不要使用背景,默认的毛玻璃效果很好,也比较统一;
2、尽量保持默认的缩进,即左边会空几个像素。
如果想改变默认缩进,有一个方法:

- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets{
   return UIEdgeInsetsZero;
}

在TodayViewController里面实现以下,缩进就没有了,下面说几点关键的

1.共享数据

插件和主应用是独立的两个进程,以前是无法共享数据的,现在可以通过AppGroup来共享数据,同属于一个group的App可以共享数据

C2108126-AF89-412D-86F8-54F5C63369EE.png

target里选择主应用打开App Group,如果之前没有,则新加group,id格式可如图,创建之后,在你的Widget里同样地方勾选上即可,程序间共享数据使用的是

//存
NSUserDefaults *shared = [[NSUserDefaults alloc] 
[shared setObject:@"value" forKey:@"key"];
[shared synchronize];

获取数据

//取
NSUserDefaults *shared = [[NSUserDefaults alloc] 
[shared objectForKey:@"key"];

Name的名字跟上边添加的App Group需要一致

2.跳转到主应用

在widget里按钮添加点击事件,跳转到主app不同页面,widget里是没有UIApplication类的,所以相关方法都不能用,所以self.extensionContext代指当前widget

- (void)skip:(UIButton *)button
{
   if (button.tag == 1) {
       [self.extensionContext openURL:[NSURL URLWithString:@"iOSWidgetApp://action=GotoHomePage"] completionHandler:^(BOOL success) {
    }];
}
   else if(button.tag == 2) {
       [self.extensionContext openURL:[NSURL URLWithString:@"iOSWidgetApp://action=GotoOtherPage"] completionHandler:^(BOOL success) {
    }];
   }
}

主app里添加协议


EF825E95-5B33-4063-AF42-7E9008807147.png

然后主app里解析

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSString* prefix = @"iOSWidgetApp://action=";
    if ([[url absoluteString] rangeOfString:prefix].location != NSNotFound) {
        NSString* action = [[url absoluteString] substringFromIndex:prefix.length];
        if ([action isEqualToString:@"GotoHomePage"]) {
        
        }
        else if([action isEqualToString:@"GotoOtherPage"]) {
        
        }
    }
    return  YES;
}

3.国际化问题

4.网络请求

常用的AF和MK框架中都有用到UIApplication类,无法编译通过,可以自行修改相关地方,也可以直接使用AFHTTPSessionManager类,

164F9181-4802-470B-87C3-AB086896A9CF.png

相关类这些,都没有用到UIApplication(AFKit.h和AFHTTPShareRequest.h是我自己写的两个类),一个是头文件合集,一个单例类
AFKit.h
#ifndef AFKit_h
#define AFKit_h
#endif /* AFKit_h */

#ifdef __OBJC__
#import "AFHTTPSessionManager.h"
#import "AFNetworkReachabilityManager.h"
#import "AFSecurityPolicy.h"
#import "AFURLResponseSerialization.h"
#import "AFHTTPSessionManager.h"
#import "AFURLSessionManager.h"
#endif

AFHTTPShareRequest.h
#import "AFHTTPSessionManager.h"

@interface AFHTTPShareRequest : AFHTTPSessionManager

+ (instancetype)sharedClient;

@end

AFHTTPShareRequest.m
#import "AFHTTPShareRequest.h"

@implementation AFHTTPShareRequest

+ (instancetype)sharedClient
{
    static AFHTTPShareRequest *client = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        client = [[AFHTTPShareRequest alloc] init];
    });
    return client;
}
@end
Top