iOS 今日插件
//宿主app设置跳转 参数是自定义 必须和extension设置成统一
在宿主app设置url type:
url schemes:today
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//app的标题
NSUserDefaults *userDefault = [[NSUserDefaults alloc]initWithSuiteName:@"group.smallWuGui"];
[userDefault setObject:@"小乌龟" forKey:@"group.smallWuGui.nickname"];
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSString* prefix = @"today://action=";
if ([[url absoluteString] rangeOfString:prefix].location != NSNotFound) {
NSString* action = [[url absoluteString] substringFromIndex:prefix.length];
if([action isEqualToString:@"rootVC"]) {
RootViewController *rootvc = [[RootViewController alloc] init];
[vc.navigationController pushViewController:rootvc animated:YES];
}
}
return YES;
}
创建Group(上面的key)
选中主应用的Target,选择Capabilities,创建一个group,名字叫group.smallWuGui,然后到插件的target勾选刚才创建的group,这样就ok了。
创建todayExtension:
1、project--》target--》点击左下角的“+”--》选择application extension--》Today Extension
2、具体类事例
1)系统默认是storyboard
NSExtension:NSExtensionPointIdentifier:com.apple.widget-extension
NSExtensionMainStoryboard : MainInterface
2)可以是纯代码:
关于删除Storyboard的配置方法,首先删除Storyboard文件,并修改TodayExtension的Plist文件中关于NSExtension的值
然后在info.plist(今日插件的)配置相关类:
NSExtension:NSExtensionPointIdentifier:com.apple.widget-extension
NSExtensionPrincipalClass : TodayViewController
#import "TodayViewController.h"
#import <NotificationCenter/NotificationCenter.h>
@interface TodayViewController () <NCWidgetProviding>
@property (weak, nonatomic) IBOutlet UILabel *contentLab;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
- (IBAction)addBtnClick:(id)sender;
@end
@implementation TodayViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self setUp];
}
- (void)setUp
{
self.addBtn.layer.cornerRadius = 3.0;
NSUserDefaults* userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.smallWuGui"];
NSString* nickName = [userDefault objectForKey:@"group.smallWuGui.nickname"];
if (nickName) {
NSString* message = @"今天smallWuGui又给你准备了很多惊喜哦,快去看看吧!";
self.contentLab.text = [NSString stringWithFormat:@"%@,%@",nickName,message];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResultFailed
// If there's no update required, use NCUpdateResultNoData
// If there's an update, use NCUpdateResultNewData
completionHandler(NCUpdateResultNewData);
[self setUp];
}
- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets
{
return UIEdgeInsetsZero;
}
- (IBAction)addBtnClick:(id)sender {
NSURL *url = [NSURL URLWithString:@"today://action=rootVC"];
[self.extensionContext openURL:url completionHandler:nil];
}
@end
注:证书配置
只需要配置宿主的证书即可,
today插件的bundle id (com.smallwugui.fennu.Today)不要使用系统默认的(com.smallwugui.fennu.TodayExternsion),会坑人最好修改下。
project--》capabilities--》app group 勾选和宿主app一样的app group。
today插件也需要一个app id(开发者中心创建,必须和宿主app是同一个账号)。