OGeek|极客世界-中国程序员成长平台

标题: ios - 如何从 iOS 的后台任务中打开应用程序? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:53
标题: ios - 如何从 iOS 的后台任务中打开应用程序?

是否可以在 iOS 中通过后台任务打开应用程序?

我希望我的应用在后台运行,并且无论出于何种原因,我希望它在没有用户输入的情况下重新出现。这可能吗?

在 Android 中,您可以拥有在后台运行的服务,并且您可以随时启动 Activity,该 Activity 将为该 Activity 启动应用程序。

我知道这不会带来良好的用户体验。我还是想知道能不能搞定。



Best Answer-推荐答案


您可以创建时间偏移为 0 的本地通知计划(例如立即显示警报)。如果用户点击通知,应用程序将被启动。

但否则,您将无法在没有通知的情况下从后台启动到前台。

    NSDate * theDate = [[NSDate date] dateByAddingTimeInterval:0]; //

    UIApplication* app = [UIApplication sharedApplication];
    NSArray  *    oldNotifications = [app scheduledLocalNotifications];


    // Clear out the old notification before scheduling a new one.
    if ([oldNotifications count] > 0)
        [app cancelAllLocalNotifications];

    // Create a new notification.
    UILocalNotification* alarm = [[UILocalNotification alloc] init];
    if (alarm)
    {
        alarm.fireDate = theDate;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = @"sonar";
        alarm.alertBody = @"Background task complete, tap to show app" ;

        [app scheduleLocalNotification:alarm];
    }

来自苹果文档: “通知是应用程序暂停、在后台或未运行以引起用户注意的一种方式。应用程序可以使用本地通知来显示警报、播放声音、标记应用程序的图标,或三者的组合. 例如,一个闹钟应用程序可能使用本地通知来播放闹钟声音并显示警报以禁用警报。当通知传递给用户时,用户必须决定该信息是否需要将应用程序带回前台.(如果应用程序已经在前台运行,本地通知会悄悄地传递给应用程序而不是用户。)"

关于ios - 如何从 iOS 的后台任务中打开应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213907/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4