首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS在random[Titanium]停止发送httprequests

iOS在random[Titanium]停止发送httprequests
EN

Stack Overflow用户
提问于 2013-04-24 17:12:54
回答 1查看 187关注 0票数 2

我有一个应用程序,每5秒发送地理位置,如果没有找到新的位置,每分钟最后找到的位置。所以基本上,应用程序不可能停止将地理位置发送到PHP文件。

尽管如此,它还是做到了。这完全是随机的。这个应用程序只是停止向服务器发送一个错误(因为我的onerror中有一个警报),并在我打开应用程序查看发生了什么时再次开始重新发送。

顺便说一句,它在Android上似乎工作得很好!

我的info.plist中有一些东西可以让它活着(它确实让它活着,但我猜它只是随机地停止了活着):

代码语言:javascript
复制
<key>UIBackgroundModes</key>
<array>
    <string>external-accessory</string>
    <string>location</string>
</array>

我的httprequest看起来像这样:

代码语言:javascript
复制
    function sendCoordinates() {
        //Reset the visual text(errors/succesmessage etc)
        if (Titanium.Network.online) {

            //Concat the GPSholder array into the toSend and than empty the GPSholder.
            //To toSend accumulates GPSholder arrays in case it can't be sent for some reason but avoids getting duplicates in the GPSholder
            //the toSend is emptied out after a succesful save.
            toSend = toSend.concat(getGPSholder());
            GPSholder = [];
            if (toSend.length > 0) {
                GPSSaved.text = '';
                minuteInterval = 0;
                var xhr=Titanium.Network.createHTTPClient({enableKeepAlive: false});
                xhr.open("POST","http://xxx.nl/website/services/esrm_tracker/push_tt_positions.php");        
                xhr.onload = function(){

                    if(this.status == '200'){

                        if(this.readyState == 4){
                            var result = JSON.parse(this.responseText);

                            switch(result.result) {
                                case 1:
                                    secondsLastSent = 0;
                                    counterBlock.text = "De laatste locatie is " + secondsLastSent + " seconden geleden verstuurd";
                                    counterBlock.show();
                                    toSend = [];
                                break;

                                case -1:
                                    GPSSaved.text = 'Authorisatie code niet geldig. Er worden geen locaties meer verstuurd.';
                                GPSstop();
                            break;

                            case -2:
                                GPSSaved.text = 'Locaties niet geldig';
                            break;

                            case -3:
                                GPSSaved.text = 'Authorisatie code niet gevonden. Er worden geen locaties meer verstuurd.';
                                GPSstop();
                            break;

                            case -10:
                                GPSstop();
                            break;

                            default:
                                GPSSaved.text = 'Onbekende fout. Er worden geen locaties meer verstuurd.';
                                GPSstop();
                            break;
                        }
                    }       
                }
            }
            xhr.onerror = function(e){ 
                GPSSaved.text = e.status + ' <- error';
                alert(e);
            };

            xhr.timeout = 10000;

            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            var str = JSON.stringify(toSend);
            var params = {
                auth_key : auth_key,
                locations : str
            };

            xhr.send(params);
        }
   } else {
        GPSSaved.text = 'Geen internet. Het versturen van locaties wordt hervat als de verbinding is hervat.';
   }
}
EN

回答 1

Stack Overflow用户

发布于 2013-04-24 19:04:30

根据我从您的代码中可以看出,您没有为iOS注册后台任务。您只打算通过配置它来完成此操作。

我也不认为它是随机的,而是在应用程序关闭5分钟后。在这一点上,应用程序pauses,直到它被再次打开,因此它将继续传输位置。

您应该看一下这个文档:http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.App.iOS.BackgroundService

代码语言:javascript
复制
var service = Ti.App.iOS.registerBackgroundService({url:'bg-service1.js'});

请注意,它将运行一个不同的实例,并且您不应该将整个应用程序包含到此后台服务中,因为它也将没有UI。为它创建一个单独的文件,包含所需的http和数据库调用,仅此而已。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16188029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档