更改服务器而不重新编译iPhone应用程序,如何?

发布于 2024-08-09 22:24:00 字数 406 浏览 4 评论 0原文

我正在开发一个 iPhone 应用程序,在其中我从当前位于 LAN 本地服务器上的 Web 服务获取数据。

现在我正在获取位于本地服务器上的数据,其网址如下:

http:\192.168.0.150\service\service.asmx\dataFetchingMethod

但是将来如果我的网址发生更改,即服务器如下所示:

http:\www.anotherDomain .com\service\service.asmx\dataFetchingMethod

http:\www.anotherURL.com\service\service.asmx\dataFetchingMethod

那么我如何动态更改它?

目前我已经在应用程序中硬编码了 url。

I am developing an iPhone application in which I am fetching data from a web service currently located on a local server in LAN.

Now I am fetching my data located on a local server with url something like this:

http:\192.168.0.150\service\service.asmx\dataFetchingMethod

But In future if my url changes i.e server something like this:

http:\www.anotherDomain.com\service\service.asmx\dataFetchingMethod

and

http:\www.anotherURL.com\service\service.asmx\dataFetchingMethod

then how I can change it dynamically?

Currently I have hard coded the url in the application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〆凄凉。 2024-08-16 22:24:00

为此进行了应用程序首选项:

- (BOOL) getDataFromWebserver
{
    NSString *url = [[NSUserDefaults standardUserDefaults] stringForKey:@"dataUrl"];

    if(url == nil)
    {
        [self setDefaultPrefs];
        url = [[NSUserDefaults standardUserDefaults] stringForKey:@"dataUrl"];
    }
    [self fetchData:url];//to implement
}

- (void)setDefaultPrefs
{
    NSDictionary *appPrefs = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"hardCodedUrl", @"dataUrl", nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:appPrefs];
    [[NSUserDefaults standardUserDefaults] synchronize];        
}

了解更多

Application Preferences are made for this:

- (BOOL) getDataFromWebserver
{
    NSString *url = [[NSUserDefaults standardUserDefaults] stringForKey:@"dataUrl"];

    if(url == nil)
    {
        [self setDefaultPrefs];
        url = [[NSUserDefaults standardUserDefaults] stringForKey:@"dataUrl"];
    }
    [self fetchData:url];//to implement
}

- (void)setDefaultPrefs
{
    NSDictionary *appPrefs = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"hardCodedUrl", @"dataUrl", nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:appPrefs];
    [[NSUserDefaults standardUserDefaults] synchronize];        
}

Read more

悲凉≈ 2024-08-16 22:24:00

您应该编写一个从文件(例如 plist)读取属性的类。

这样,每当应用程序运行时,它都会从 plist 中读取其配置,并调用指定的服务 URL。无需重新编译应用程序即可更改此设置。

You should write a class that reads properties from a file (such as a plist).

That way whenever the app runs, it'll read its configuration from the plist, and make calls to the service URL specified. This can be changed without recompiling the app.

画骨成沙 2024-08-16 22:24:00

您将需要连接到某种从不更改或很少更改的资源。我看到了几种不同的方法。


代理方法

您可以设置一个主服务器(http://masterurl.com/ )将您的请求代理到另一台服务器。这里的想法是主服务器永远不会改变,但它代理的机器可以随时改变。例如:

假设您的 iPhone 应用程序连接到 http://masterurl.com/

  1. 所有请求
    http://masterurl.com/ 被代理到http://someotherurl.com/

  2. 在未来的某个时刻,你
    需要更换
    http://someotherurl.com/http://snazzynewurl.com/

  3. 现在你可以改变
    http://masterurl.com/ 代理到 http://snazzynewurl.com/

对于您的应用程序的用户来说,没有任何变化,您不必更新您的应用程序并重新提交以供审核。


位置文件方法

您可以从文件(例如 txt 或 xml 文件)获取位置。

假设您的文件位于 http://someurl.com/location.txt 并且内容很简单:

inside location.txt:

http://someotherbaseurl.com/

然后,您的应用程序将读取此文本文件并使用 http://someotherbaseurl.com/ 作为您的应用程序的其余部分。现在,如果您需要更改 URL,只需更新文本文件即可。

如果您采用位置文件方法,我建议使用 Amazon S3 或 Rackspace Cloud Files 托管该文件,以便可以通过其内容交付网络上的 URL 访问该文本文件。

我确信还有其他方法可以解决这个问题。无论哪种方式,您都需要一个从不或很少更改的主 URL。否则,您将遇到必须重新编译并重新提交应用程序的情况。

You're going to need to connect to some kind of resource that either never changes or changes very infrequently. I see a couple of different approaches.


Proxy Approach

You could set up a master server (http://masterurl.com/) that proxies your request to another server. The idea here is that the master server never changes, but the machine that it proxies to could change at any time. For example:

Assuming your iPhone app connects to http://masterurl.com/:

  1. All requests to
    http://masterurl.com/ are proxied to http://someotherurl.com/.

  2. At some point in the future, you
    need to replace
    http://someotherurl.com/ with http://snazzynewurl.com/.

  3. Now you can just change
    http://masterurl.com/ to proxy to http://snazzynewurl.com/.

To your application's user nothing has changed and you don't have to update your app and re-submit it for review.


Location File Approach

You could get the location from a file, like a txt or xml file.

Assuming your file lives at http://someurl.com/location.txt and the contents are simple:

inside location.txt:

http://someotherbaseurl.com/

Your app would then read in this text file and use http://someotherbaseurl.com/ as a base url for the rest of your app. Now if you need to make a change to the URL, you can just update the text file.

If you're taking the location file approach, I'd recommend hosting the file using Amazon S3 or Rackspace Cloud Files so that the text file is available over a URL on their content delivery network.

I'm sure there are other ways to tackle this. Either way you'll need a master URL that changes never or seldom. Otherwise you'll run into a situation where you'll have to re-compile and re-submit your app.

水水月牙 2024-08-16 22:24:00

这不正是 DNS 的用途吗?您指定一个域名,将其重定向到您喜欢的位置...在设置域之前,这对调试没有太大帮助,但现在进行一些设置并重定向到您喜欢的位置。

Isn't this exactly what DNS is for? You specify a single domain name, that re-directs where you like... it's not much help in debugging before you have the domain set up, but get something set up now and re-direct where you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文