如何解决静态快速操作在目标c iOS中不起作用
我对iOS应用程序开发非常陌生,我一直在遵循教程来建立自己的职业生涯。因此,从我开始尝试按照客户项目在Objective C中实现静态快速操作。我能够在按下图标的同时添加键并获得选项。但是,当我按“快速操作”时,什么也没有发生,它只是启动了应用程序。我做了很多研究,但尝试却徒劳。以下是我编写的Appdelegate代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
BOOL shouldPerformAdditionalDelegateHandling = YES;
// Check API availiability
// UIApplicationShortcutItem is available in iOS 9 or later.
if([[UIApplicationShortcutItem class] respondsToSelector:@selector(new)]){
NSLog(@"HERE");
// [self configDynamicShortcutItems];
// If a shortcut was launched,display its information and take the appropriate action
UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];
if(shortcutItem)
{
NSLog(@"shortcut launch");
// When the app launch at first time,this block can not called.
[self handleShortCutItem:shortcutItem];
// This will block "performActionForShortcutItem:completionHandler" from being called.
shouldPerformAdditionalDelegateHandling = NO;
}else{
NSLog(@"normal launch");
// normal app launch process without quick action
// [self launchWithoutquickaction];
}
}else{
// Less than iOS9 or later
// [self launchWithoutquickaction];
}
return shouldPerformAdditionalDelegateHandling;
}
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
NSLog(@"performActionForShortcutItem");
BOOL handledShortCutItem = [self handleShortCutItem:shortcutItem];
completionHandler(handledShortCutItem);
}
/**
* @brief handle shortcut item depend on its type
*
* @param shortcutItem shortcutItem selected shortcut item with quick action.
*
* @return return BOOL description
*/
- (BOOL)handleShortCutItem : (UIApplicationShortcutItem *)shortcutItem{
BOOL handled = NO;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
if ([shortcutItem.type isEqualToString:@"PlayMusic"]) {
handled = YES;
NSLog(@"Play");
secondview *vc = [storyboard instantiateViewControllerWithIdentifier:@"second"];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
else if ([shortcutItem.type isEqualToString:@"StopMusic"]) {
handled = YES;
NSLog(@"Stop");
// ThirdViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"thirdVC"];
//
// self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
return handled;
}
但是,当我深深按下主页图标但单击时没有任何反应,我可以看到“快速行动”项。我检查info.plist中的键及其相同的。 Quick Actions
下面是用于添加静态操作的info.plist
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypePlay</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Play</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>Start playback</string>
<key>UIApplicationShortcutItemType</key>
<string>PlayMusic</string>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypePlay</string>
<key>UIApplicationShortcutItemTitle</key>
<string>STOP</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>Stop playback</string>
<key>UIApplicationShortcutItemType</key>
<string>StopMusic</string>
</dict>
有人可以帮我做错什么吗?
解决方法
由于iOS(13.0,*)
AppDelegate通过UIWindow
处理了自己的SceneDelegate
,因此该方法
@implementation AppDelegate
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
// here shortcut evaluation below ios(13.0)
}
@end
仅在iOS较低版本13.0的iPhone或iPad上调用
要使其在iOS(13.0,*)及更高版本中运行,您需要在SceneDelegate中以稍有不同的名称实现它,请参见下文。。(matt是正确的!)
@implementation SceneDelegate
-(void)windowScene:(UIWindowScene *)windowScene performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
// here shortcut evaluation starting with ios(13.0)
NSLog(@"shortcutItem=%@ type=%@",shortcutItem.localizedTitle,shortcutItem.type);
}
@end
PS:假设您可以像在iOS(13.0, *)中那样使用它,则swift的Apple快速操作示例代码是虚假的,因为它是为iOS早期版本编写的
编辑:,根据要求,此处为iOS> = 13.0的完整版本
@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>
@property (strong,nonatomic) UIWindow * window;
@end
和
-(void)windowScene:(UIWindowScene *)windowScene performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
/* //go to Info.plist open as Source Code,paste in your items with folling scheme
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypePlay</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Play</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>Start playback</string>
<key>UIApplicationShortcutItemType</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).Start</string>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypePlay</string>
<key>UIApplicationShortcutItemTitle</key>
<string>STOP</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>Stop playback</string>
<key>UIApplicationShortcutItemType</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).Stop</string>
</dict>
</array>
*/
// may look complex but this is 100% unique
// which i would place $(PRODUCT_BUNDLE_IDENTIFIER). in front of types in Info.plist
NSString *devIdent = [NSString stringWithFormat:@"%@.",NSBundle.mainBundle.bundleIdentifier];
if ([shortcutItem.type isEqualToString:[devIdent stringByAppendingString:@"Start"]]) {
completionHandler([self windowScene:windowScene byStoryBoardIdentifier:@"startview"]);
} else if ([shortcutItem.type isEqualToString:[devIdent stringByAppendingString:@"Stop"]]) {
completionHandler([self windowScene:windowScene byStoryBoardIdentifier:@"stopview"]);
} else {
NSLog(@"Arrgh! unrecognized shortcut '%@'",shortcutItem.type);
}
//completionHandler(YES);
}
-(BOOL)windowScene:(UIWindowScene *)windowScene byStoryBoardIdentifier:(NSString *)identifier {
// idear is to return NO if build up fails,so we can startup normal if needed.
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
if (story!=nil) {
UIViewController *vc = [story instantiateViewControllerWithIdentifier:identifier];
if (vc!=nil) {
if (_window==nil) _window = [[UIWindow alloc] initWithWindowScene:windowScene];
_window.rootViewController = vc;
// .. OR following code..
//[_window.rootViewController presentViewController:vc animated:YES completion:^{ }];
//[_window makeKeyAndVisible];
// should be success here..
return YES;
}
}
// no success here..
return NO;
}
除了从Storyboard实例化ViewController之外,您还可以执行其他操作。例如,您可以切换属性或类似内容。
,抱歉,为时已晚。与主要开发商讨论后,对家庭快速行动的进一步研究向我显示了以下结论。集成HomeQuickActions时需要注意以下几点:
- 对于每个开发人员来说最重要的一点: 家庭快速操作(单击应用程序图标上的快捷方式)绝对基于操作系统和硬件(仅iOS支持) 13)。 Apple documents。这意味着在iOS 13之前,即使设备具有Haptic Touch / 3D Touch,也不会显示任何应用程序快捷项目。 iOS 13及更高版本是强制性要求。(已在所有iPhone的Simulator中进行了检查,需要在使用旧手机的真实设备中进行测试)
启用3D触摸和未启用3D触摸的设备都可以通过强制触摸或长按来支持此功能,具体取决于它们是否能够运行iOS 13或更高版本。
-
因此,要使App快捷方式正常工作,iOS设备必须肯定具有iOS 13,并且还需要硬件兼容性,这意味着设备必须具有iOS 13及更高版本才能使App快捷方式正常工作。对于iPhone 5S,iPhone 6所有者:iOS 13将不再兼容。
-
由于Apple已用Haptic Touch代替了3D Touch硬件,因此iPhone XR的新款iPhone将仅具有Haptic Touch。 SE(第1代,iPhone 6系列直至iPhone X)之后的旧设备具有3D Touch硬件。 Haptic Touch是依赖于软件OS的操作系统,它使用了手指按下的区域,而3D Touch是依赖于硬件的硬件,它使用了屏幕之间的特定传感器。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。