????首先新建target選擇keyboard extension坎缭,xcode會自動新建一個UIViewController的子類UIInputViewController,遵循了一個協(xié)議和方法,剛開始只需知道與普通的UIViewController一樣用來展示彈出鍵盤的視圖,可以以任何形式添加子試圖布局。
? ? 當(dāng)然吟宦,鍵盤擴展肯定要提供鍵盤的屬性和對于輸入刪除字符的功能
????接著對于自定義的界面布局與一般工程一樣,需要注意的是bundle和application的使用涩维。
? ? 在bundle中有些文件找不到需要手動添加到Compile Sources中殃姓,對于主工程中的文件,例如分類瓦阐、自定義的類等蜗侈,可以在xcode右側(cè)的Target Membership中勾選我們的extension,這樣在擴展中就可以使用了睡蟋!
????在擴展中無法使用UIApplication,如要進行跳轉(zhuǎn)到主app需使用以下代碼(比如跳到自己app的設(shè)置界面提示打開full access的功能踏幻,需要先跳轉(zhuǎn)app再次進行跳轉(zhuǎn)),其中URL Schemes與跳轉(zhuǎn)其他app相同戳杀,在設(shè)置中設(shè)置或者plist中填寫
UIResponder* responder =self;
? ? while((responder = [respondernextResponder]) !=nil) {
? ? ? ? if([responderrespondsToSelector:@selector(openURL:)] ==YES) {
? ? ? ? ? ? [responderperformSelector:@selector(openURL:)
? ? ? ? ? ? ? ? ? ? ? ? ? ? withObject:[NSURLURLWithString:@"KeyboardExtension://"]];
? ? ? ? }
}
如果需要與主app共享文件則需要使用Capabilities中的App Group(看到這里你已經(jīng)發(fā)現(xiàn)與主app的交互其實和與其他app交互沒有區(qū)別)
使用下面的代碼進行存儲和讀取
//存儲
? ? UIImage *image = [UImage imageNamed:@"swift知識結(jié)構(gòu)圖.jpg"];? ?
? ? NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"你的GroupID"];
? ? groupURL = [groupURL URLByAppendingPathComponent:@"123.jpg"];
? ? [UIImageJPEGRepresentation(image,0) writeToURL:groupURL atomically:YES]?NSLog(@"success"):NSLog(@"failed");
//讀取
????NSURL *groupURL = [[NSFileManager defaultManager]? containerURLForSecurityApplicationGroupIdentifier:@"你的GroupID"];
? ? NSData *data = [NSData dataWithContentsOfURL:[groupURL URLByAppendingPathComponent:@"123.jpg"]];
? ? UIImage *image = [UIImage imageWithData:data];
? ? 注意:在進行鍵盤擴展開發(fā)時该面,盡量使用frame布局夭苗,懶加載,減少首次加載的時間隔缀,減少內(nèi)存使用题造,避免奔潰。有任何問題都可以留言討論猾瘸,互相交流晌梨。