iOS 相当于 Android 服务? [英] iOS equivalent to Android Service?

查看:28
本文介绍了iOS 相当于 Android 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么随着 iOS 7 支持更广泛的后台模式,是否有可能最终在 iOS 上拥有与 Android Service 相当的功能?

So with iOS 7 supporting a broader background mode, is it possible to finally have an equivalent to Android Service on iOS?

我所追求的实际上是在后台运行应用程序 A,并让一个或多个应用程序 B 和 C 与该应用程序对话(不显示应用程序 A 的 GUI).

What I am after is essentially running app A in the background and have one or more apps B and C talk to that app (without showing the GUI of app A).

请注意,使用连接和推送通知可能不是一种选择,尽管这是推荐的方式.有任何想法吗?

Please note that using connectivity and push notifications may not be an option although this is the recommended way of doing so. Any ideas?

推荐答案

没有按预期工作.请参阅此答案以获得最佳解决方案:推送通知

Not working as expected. See this answer for best solution: Push Notifications

下一个解决方案仅在用户在应用程序中保持同步时有用.

The next solution is only useful while the user is in the app to maintain it synced.

没有办法在后台永久执行任务,但是您可以使用有限长度的任务来执行此操作,当您创建有限长度时,这将始终在应用程序处于活动状态时运行,但当您点击主页按钮,ios 只给你 10 分钟来执行你的任务并使其无效,但它让你有机会制作一个无效处理程序块",在那里你可以在确定完成之前执行最后的操作.

There is no way to perform tasks in the background permanently, but you can use the finite-length tasks to do that, when you make a finite-length, this gonna run always while the app is active, but when you click home button, ios gives you only 10 min to perform your task and invalidate it, but it gives you a chance to make a 'invalidate handler block' where you can do last actions before finish definitely.

因此,如果您在其他时间使用该处理程序块来调用有限长度的任务,您可以通过运行一个任务 10 分钟来模拟服务,当它结束时,在其他 10 分钟内调用它,因此.

我在创建接口服务"的项目中使用它.我在这里给你代码:

I use that in a project creating a interface 'Service'. I let you here the code:

  • 服务.h
//
//  Service.h
//  Staff5Personal
//
//  Created by Mansour Boutarbouch Mhaimeur on 30/09/13.
//  Copyright (c) 2013 Smart & Artificial Technologies. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Service : NSObject

@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;
@property (nonatomic) NSInteger frequency;
@property (nonatomic, strong) NSTimer *updateTimer;

- (id) initWithFrequency: (NSInteger) seconds;
- (void) startService;
- (void) doInBackground;
- (void) stopService;
@end

<小时>

  • Service.m
  • //
    //  Service.m
    //  Staff5Personal
    //
    //  Created by Mansour Boutarbouch Mhaimeur on 30/09/13.
    //  Copyright (c) 2013 Smart & Artificial Technologies. All rights reserved.
    //
    
    #import "Service.h"
    
    @implementation Service
    @synthesize frequency;
    
    -(id)initWithFrequency: (NSInteger) seconds{
        if(self = [super init]){        
            self.frequency = seconds;
            return self;
        }
        return nil;
    }
    - (void)startService{
        [self startBackgroundTask];
    }
    
    - (void)doInBackground{
        //Español //Sobreescribir este metodo para hacer lo que quieras
        //English //Override this method to do whatever you want
    }
    
    - (void)stopService{
        [self.updateTimer invalidate];
        self.updateTimer = nil;
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }
    
    - (void) startBackgroundTask{
        self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:frequency
                                                            target:self
                                                          selector:@selector(doInBackground)
                                                          userInfo:nil
                                                           repeats:YES];
        self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            [self endBackgroundTask];
        }];
    }
    - (void) endBackgroundTask{
        [self.updateTimer invalidate];
        self.updateTimer = nil;
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
        [self startBackgroundTask];
    }
    
    @end
    

    <小时>

    通过这门课我执行我的服务,但我很长时间没有测试它.我在模拟器中做的最好的测试持续了 16 个小时,一切正常!

    这是在模拟器上测试的,但在应用程序终止后在手机中不起作用.

    That was tested on the simulator, but in phone doesnt work after the application has been terminated.

    我举个例子:

    // SomeService.h
    @interface SomeService : Service
    
    @end
    
    
    // SomeService.m
    #import "SomeService.h"
    
    @implementation SomeService
    
    // The method to override
    - (void)doInBackground{
        NSLog(@"Background time remaining = %.1f seconds", [UIApplication sharedApplication].backgroundTimeRemaining);
        NSLog(@"Service running at %.1f seconds", [self getCurrentNetworkTime]);
    }
    // Your methods
    - (long) getCurrentNetworkTime{
        return ([[NSDate date] timeIntervalSince1970]);
    }
    
    @end
    

    <小时>

    在您的应用委托或您需要提升服务的地方,您编写下一行:


    And in your app delegate or where you need to raise the service, you write the next line:

    Service myService = [[SomeService alloc] initWithFrequency: 60]; //execute doInBackground each 60 seconds
    [myService startService];
    

    如果你需要阻止它:

    [myService stopService];
    

    可能解释得比必要的多,但我想向任何人解释清楚!我希望它对我的英语有所帮助和抱歉.

    May have explained more than necessary, but i want to keep it clear for anyone! I hope its help and sorry for my english.

    这篇关于iOS 相当于 Android 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆