iOS相当于Android服务?

Elo*_*007 6 service android background ios ios7

因此,iOS 7支持更广泛的后台模式,是否有可能最终在iOS上拥有相当于Android服务?

我所追求的实际上是在后台运行应用程序A并且有一个或多个应用程序B和C与该应用程序通信(没有显示应用程序A的GUI).

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

Ign*_*ers 5

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


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

没有办法永久地在后台执行任务,但你可以使用有限长度的任务来做到这一点,当你做一个有限长度时,这将在应用程序处于活动状态时始终运行,但是当你单击主页按钮时, ios只给你10分钟的时间来执行你的任务并使它无效,但它让你有机会制作一个'invalidate handler block',你可以在完成之前完成最后的动作.

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

我在创建界面"服务"的项目中使用它.我告诉你这里的代码:


  • Service.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
Run Code Online (Sandbox Code Playgroud)
  • 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
Run Code Online (Sandbox Code Playgroud)

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

编辑:这是在模拟器上测试,但在应用程序终止后手机无法正常工作.

我举个例子:


// 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
Run Code Online (Sandbox Code Playgroud)

在您的应用程序委托或需要提升服务的位置,您可以编写下一行:

Service myService = [[SomeService alloc] initWithFrequency: 60]; //execute doInBackground each 60 seconds
[myService startService];
Run Code Online (Sandbox Code Playgroud)

如果你需要阻止它:

[myService stopService];
Run Code Online (Sandbox Code Playgroud)

可能已经解释了超过必要的,但我想为任何人保持清晰!我希望它的帮助和对不起我的英语.