結(jié)果圖
IMG_0090.PNG
1.添加Today Extension

Paste_Image.png
添加后會有以下這部分內(nèi)容(我的只是把MainInterface.storyboard刪了)

Paste_Image.png
2.直接去plist文件填寫相應(yīng)的東西(這里NSExtensionPrincipalClass對應(yīng)的TodayViewController是要和你的控制器名稱一致)

Paste_Image.png
3.繪制相應(yīng)的UI(注意:@"appextension://xxx",appextension是要和你項目中的URLSchemes Role添加一樣的字段,后面xxx代表的是參數(shù))
- (void)viewDidLoad {
[super viewDidLoad];
self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 100);
NSArray *dataArray = @[@"逛市場",@"消息", @"進貨車", @"我的"];
NSArray *nImageArray = @[@"ico_toolbar_market",@"ico_toolbar_msg",@"ico_toolbar_cart", @"ico_toolbar_my"];
for (int i = 0; i < 4; i++) {
[self setupButtonWithTitle:dataArray[i] normalImage:nImageArray[i] leadI:i];
}
}
- (void)setupButtonWithTitle:(NSString *)title normalImage:(NSString *)normalInage leadI:(int)i{
CGFloat leadx = i * (([UIScreen mainScreen].bounds.size.width-30)/4);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(leadx+10, 45, ([UIScreen mainScreen].bounds.size.width-30)/4, 50);
button.titleLabel.font = [UIFont systemFontOfSize:13];
button.tag = i;
[button setTitle:title forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(leadx+10, 25, 30, 30)];
image.image = [UIImage imageNamed:normalInage];
image.center = CGPointMake(button.center.x, image.center.y);
[image setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:image];
}
- (void)buttonClick:(UIButton *)button {
NSLog(@"%@",button);
//通過extensionContext借助host app調(diào)起app
[self.extensionContext openURL:[NSURL URLWithString:@"appextension://xxx"] completionHandler:^(BOOL success) {
NSLog(@"open url result:%d",success);
}];
}
4.回到項目的delegate里面執(zhí)行相應(yīng)的操作
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
debugMethod();
debugLog(@"%@, %@", sourceApplication, url.host);
if ([[url absoluteString] rangeOfString:@"appextension"].location != NSNotFound) {
//執(zhí)行你要的操作
}
return YES;
}