2012-11-23 27 views
1

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

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

我在寻找哪种方法,数据库和API,我应该使用。我一直在环顾四周,人们使用不同的API,我不知道什么最适用于我的应用程序。如果你有链接到教程,也将不胜感激。从四处看,它看起来像我应该使用SQL(在手机和Web服务器),PHP和帖子。谢谢。

+0

“我是iOS的新手” - 在这种情况下,您可能还没有阅读/听说过我的iOS相关SO问题的第一条规则:您很可能滥用了Xcode标记。 iOS开发不需要Xcode,并且该标签不应用于一般的iOS相关编程问题。请阅读标签的标签维基以获取更多信息。 – 2012-11-23 22:45:55

回答

0

我会推荐与平台作为服务提供商之一(PAAS),他们提供的服务器基础设施和iOS客户端API。有很多可供选择。例如Stackmob,Parse,Kinvey,但还有一些其他的。所有提供免费服务,直到你获得大量的流量,然后开始缩减成本。如果您决定推出自己的产品,那么在客户端RestKit绝对是一个开始作为基于REST的服务API的客户端框架的好地方。

0

我写了一个自定义类,它使用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 
     } 
    }]; 

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