【问题标题】:Beginner iOS app data upload to website初学者iOS应用数据上传到网站
【发布时间】:2012-11-23 20:57:27
【问题描述】:

首先,我是 iOS 新手,我正处于开发的早期阶段,但想从正确的方向开始,我做了一些基本的 iOS 教程,并完成了 html/c++ 开发。我们有两个成员制作这个应用程序,他将负责 UI 和本地数据存储,而我需要将数据从 iOS 上传到网站。将有一个本地数据库快速记录,我想做两种类型的上传。

  1. “实时流”数据,每秒左右更新网站上的文本。
  2. 一旦这个数据记录会话完成,它将有一个上传按钮来上传整个数据库。

我正在寻找我应该使用哪些方法、数据库和 API。我一直在环顾四周,人们使用不同的 API,我不确定什么最适合我的应用程序。如果您有教程的链接,也将不胜感激。环顾四周,我应该使用 sql(在电话和 Web 服务器上)、php 和 post。谢谢。

【问题讨论】:

  • “我是 iOS 新手”——在这种情况下,你可能还没有阅读/听过我关于 iOS 相关 SO 问题的第一条规则:你很可能误用了 Xcode 标签。 iOS 开发不需要 Xcode,标签不应该用于一般的 iOS 相关编程问题。请阅读标签的标签维基了解更多信息。

标签: ios web-services cocoa-touch


【解决方案1】:

我建议使用平台即服务提供商 (PAAS) 之一,他们提供服务器基础架构和 iOS 客户端 API。有很多可供选择。例如StackmobParseKinvey,但还有很多其他的。所有这些都提供免费服务,直到您获得大量流量,然后开始扩展成本。如果您决定自己开发,那么在客户端 RestKit 绝对是一个从客户端框架开始到基于 REST 的服务 API 的好地方。

【讨论】:

    【解决方案2】:

    我编写了一个自定义类,它使用 AFNetworking 来处理网络请求。我已经将它与基于 PHP 和 .NET 的 Web 服务一起使用。由于请求使用 POST 参数,因此非常灵活。

    这是课程:

    接口文件:

    /*
     NetworkClient.h
    
     Created by LJ Wilson on 2/3/12.
     Copyright (c) 2012 LJ Wilson. All rights reserved.
     License:
    
     Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
     and associated documentation files (the "Software"), to deal in the Software without restriction, 
     including without limitation the rights to use, copy, modify, merge, publish, distribute, 
     sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
     furnished to do so, subject to the following conditions:
    
     The above copyright notice and this permission notice shall be included in all copies or 
     substantial portions of the Software.
    
     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
     NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
     OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     */
    
    #import <Foundation/Foundation.h>
    
    extern NSString * const APIKey;
    
    @interface NetworkClient : NSObject
    
    +(void)processURLRequestWithURL:(NSString *)url 
                          andParams:(NSDictionary *)params 
                              block:(void (^)(id obj))block;
    
    +(void)processURLRequestWithURL:(NSString *)url 
                          andParams:(NSDictionary *)params 
                        syncRequest:(BOOL)syncRequest
                              block:(void (^)(id obj))block;
    
    +(void)processURLRequestWithURL:(NSString *)url 
                          andParams:(NSDictionary *)params 
                        syncRequest:(BOOL)syncRequest
                 alertUserOnFailure:(BOOL)alertUserOnFailure
                              block:(void (^)(id obj))block;
    
    +(void)handleNetworkErrorWithError:(NSError *)error;
    
    +(void)handleNoAccessWithReason:(NSString *)reason;
    
    @end
    

    实施文件:

    /*
     NetworkClient.m
    
     Created by LJ Wilson on 2/3/12.
     Copyright (c) 2012 LJ Wilson. All rights reserved.
     License:
    
     Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
     and associated documentation files (the "Software"), to deal in the Software without restriction, 
     including without limitation the rights to use, copy, modify, merge, publish, distribute, 
     sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
     furnished to do so, subject to the following conditions:
    
     The above copyright notice and this permission notice shall be included in all copies or 
     substantial portions of the Software.
    
     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
     NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
     OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     */
    
    #import "NetworkClient.h"
    #import "AFHTTPClient.h"
    #import "AFHTTPRequestOperation.h"
    #import "SBJson.h"
    
    NSString * const APIKey = @"APIKEY IF SO DESIRED (ADDS SECURITY)";
    
    @implementation NetworkClient
    
    +(void)processURLRequestWithURL:(NSString *)url 
                          andParams:(NSDictionary *)params 
                              block:(void (^)(id obj))block {
    
        [self processURLRequestWithURL:url andParams:params syncRequest:NO alertUserOnFailure:NO block:^(id obj) {
            block(obj);
        }];
    }
    
    +(void)processURLRequestWithURL:(NSString *)url 
                          andParams:(NSDictionary *)params 
                        syncRequest:(BOOL)syncRequest
                              block:(void (^)(id obj))block {
        [self processURLRequestWithURL:url andParams:params syncRequest:syncRequest alertUserOnFailure:NO block:^(id obj) {
            block(obj);
        }];
    }
    
    
    +(void)processURLRequestWithURL:(NSString *)url 
                          andParams:(NSDictionary *)params 
                        syncRequest:(BOOL)syncRequest
                 alertUserOnFailure:(BOOL)alertUserOnFailure
                              block:(void (^)(id obj))block {
    
        // Default url goes here, pass in a nil to use it
        if (url == nil) {
            url = @"DEFAULT URL HERE";
        }
    
        NSMutableDictionary *newParams = [[NSMutableDictionary alloc] initWithDictionary:params];
        [newParams setValue:APIKey forKey:@"APIKey"];
    
        NSURL *requestURL;
        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:requestURL];
    
        NSMutableURLRequest *theRequest = [httpClient requestWithMethod:@"POST" path:url parameters:newParams];
    
        __block NSString *responseString = @"";
    
        AFHTTPRequestOperation *_operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
        __weak AFHTTPRequestOperation *operation = _operation;
    
        [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            responseString = [operation responseString];
    
            id retObj = [responseString JSONValue];
    
            // Check for invalid response (No Access)
            if ([retObj isKindOfClass:[NSDictionary class]]) {
                if ([[(NSDictionary *)retObj valueForKey:@"Message"] isEqualToString:@"No Access"]) {
                    block(nil);
                    [self handleNoAccessWithReason:[(NSDictionary *)retObj valueForKey:@"Reason"]];
                }
            } else if ([retObj isKindOfClass:[NSArray class]]) {
                if ([(NSArray *)retObj count] > 0) {
                    NSDictionary *dict = [(NSArray *)retObj objectAtIndex:0];
                    if ([[dict valueForKey:@"Message"] isEqualToString:@"No Access"]) {
                        block(nil);
                        [self handleNoAccessWithReason:[(NSDictionary *)retObj valueForKey:@"Reason"]];
                    }
                }
            }
            block(retObj);
        } 
                                          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                              NSLog(@"Failed with error = %@", [NSString stringWithFormat:@"[Error]:%@",error]);
                                              block(nil);
                                              if (alertUserOnFailure) {
                                                  // Let the user know something went wrong
                                                  [self handleNetworkErrorWithError:operation.error];
                                              }
    
                                          }];
    
        [operation start];
    
        if (syncRequest) {
            // Process the request syncronously
            [operation waitUntilFinished];
        } 
    
    
    }
    
    
    +(void)handleNetworkErrorWithError:(NSError *)error {
        NSString *errorString = [NSString stringWithFormat:@"[Error]:%@",error];
    
        // Standard UIAlert Syntax
        UIAlertView *myAlert = [[UIAlertView alloc] 
                                initWithTitle:@"Connection Error" 
                                message:errorString 
                                delegate:nil 
                                cancelButtonTitle:@"OK" 
                                otherButtonTitles:nil, nil];
    
        [myAlert show];
    
    }
    
    +(void)handleNoAccessWithReason:(NSString *)reason {
        // Standard UIAlert Syntax
        UIAlertView *myAlert = [[UIAlertView alloc] 
                                initWithTitle:@"No Access" 
                                message:reason 
                                delegate:nil 
                                cancelButtonTitle:@"OK" 
                                otherButtonTitles:nil, nil];
    
        [myAlert show];
    
    }
    
    
    @end
    

    一个示例调用类似于:

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"GetMeetingRooms", @"Command",
                                nil];
    
        [NetworkClient processURLRequestWithURL:nil andParams:params block:^(id obj) {
            // Check to make sure we got back a response message (NSArray of NSDictionaries in JSON format
            if ([obj isKindOfClass:[NSArray class]]) {
                NSArray *response = (NSArray *)obj;
                // So dome work here with the response here
            }
        }];
    

    如果您有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多