ios - IOS后台运行代码

标签 ios objective-c ios4 firebase

Firebase * ref = nil;


NSInteger iid = [[API sharedInstance] userid];
NSString * path = [NSString stringWithFormat:  @"http://example.firebaseIO.com/user/%d/conversations", iid];

ref = [[Firebase alloc] initWithUrl:path];


if(ref) {

    NSString * path = [NSString stringWithFormat: @"http://example.firebaseIO.com/conversations"];
    Firebase * conv = [[Firebase alloc] initWithUrl: path];

    [ref observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

        // name of conversation
        NSString * name = snapshot.name;
        Firebase * ref1 = [conv childByAppendingPath: name];

        [ref1 observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

            if(snapshot.value != [NSNull null] && ![snapshot.value isKindOfClass: [NSString class]])
            {
                FDataSnapshot * chatsnapshot = [snapshot childSnapshotForPath: @"chats"];

                NSInteger numChatMessages = chatsnapshot.childrenCount;
                numberOfTotalChatMessages += numChatMessages;

                NSMutableDictionary *m = [snapshot.value mutableCopy];
                [m setValue: snapshot.name forKey: @"ref_name"];

                NSInteger current_user = [[API sharedInstance] userid];
                NSString * userpath = [NSString stringWithFormat: @"users/%d", current_user];
                FDataSnapshot * usersnapshot = [snapshot childSnapshotForPath: userpath];

                if(usersnapshot.value != [NSNull null] && ![usersnapshot.value isKindOfClass: [NSString class]])
                {
                    NSDictionary * userdict = usersnapshot.value;
                    NSInteger numUserMessagesRead = [userdict[@"numOfMessages"] intValue];

                    numberOfMessagesRead += numUserMessagesRead;

                    if(numberOfTotalChatMessages > numberOfMessagesRead) {
                        [m setValue: @"true" forKey: @"bubble"];
                    }
                }

                [self.chats addObject: m];

                NSNumber * index = [NSNumber numberWithInt: self.chats.count - 1];
                [read setValue: index forKey: snapshot.name];

                PLRightMenuViewController * rightPanel = (PLRightMenuViewController *) self.viewController.rightPanel;
                [rightPanel.tableView reloadData];

                self.numChats = numberOfTotalChatMessages - numberOfMessagesRead;
                [[UIApplication sharedApplication] setApplicationIconBadgeNumber: self.numChats];

            }
        }];

    }];


    [ref observeEventType:FEventTypeChildChanged withBlock:^(FDataSnapshot *snapshot) {

        NSString * name = snapshot.name;
        Firebase * ref1 = [conv childByAppendingPath: name];

        [ref1 observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot)
        {
            if(snapshot.value != [NSNull null] && ![snapshot.value isKindOfClass: [NSString class]])
            {
                numberOfTotalChatMessages += 1;

                NSMutableDictionary *m = [snapshot.value mutableCopy];
                [m setValue: snapshot.name forKey: @"ref_name"];
                [m setValue: @"true" forKey: @"bubble"];
                [self.chats addObject: m];


                if([read objectForKey: snapshot.name])
                {
                    NSInteger index = [[read objectForKey: snapshot.name] intValue];
                    [self.chats removeObjectAtIndex: index];

                     NSNumber * index1 = [NSNumber numberWithInt: self.chats.count - 1];
                    [read setValue: index1 forKey: snapshot.name];
                }

                self.numChats = numberOfTotalChatMessages - numberOfMessagesRead;
                [[UIApplication sharedApplication] setApplicationIconBadgeNumber: self.numChats];


                PLRightMenuViewController * rightPanel = (PLRightMenuViewController *) self.viewController.rightPanel;
                [rightPanel.tableView reloadData];
            }
        }];

    }];
}

我有上面的代码,基本上使用 firebase 检查任何新的聊天对话并更改应用程序角标(Badge)编号。如何在应用程序后台运行代码,以便无论是否有人正在使用该应用程序,应用程序角标(Badge)编号都会更改?

基本上,我如何在后台运行上面的代码?我应该在 Appdelegate 中更改什么?

最佳答案

你不能,除非你作弊。目前 iOS 或 Apple 分别不允许应用程序进入后台,只有极少数异常(exception)。例如位置服务或播放音频。

有些人通过假装播放某种声音来作弊。

到目前为止,您必须使用推送通知来通知应用有关收到的消息并更新角标(Badge)。

或者...等待 iOS 7 发布。假设您有一个开发者帐户,您已经可以访问文档和预览/测试版资源并做好准备,直到 iOS 7 和 SDK 等正式发布。

关于ios - IOS后台运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17680026/

相关文章:

c# - 我可以在 Xamarin 上使用 WPF 的全屏 UI 吗?

c++ - 从 Objective-C++ 调用 C++ 时出现 BAD_ACCESS 错误。所有文件都是 .mm 但仍然是这个问题

ios - UITableViewCell 中的 NSLayoutConstraint 使应用程序崩溃

ios - 如何检测iOS中的wifi网络变化

iphone - 在 UITableView 之上覆盖 View

iphone - StopUpdatingLocation 方法不适用于 iOS5

ios - 使用 TestFairy.begin() 时在 Xcode 9 中使用未解析的标识符 'TestFairy' 错误

ios - 如何设置 UIBarButtonItem 的字体大小?

ios - 我需要一个 api key 来访问新的 freebase 图像吗?

objective-c - 在 View Controller 中使用导航 Controller