Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

занятие8

1,025 views

Published on

  • Be the first to comment

  • Be the first to like this

занятие8

  1. 1. Разработка приложений для iPhone и iPad занятие #8
  2. 2. песочница
  3. 3. а что внутри?Home Directory Layout• Each app has its own set of directories я!• <Application Home> ельз ! MyApp.app т ьн е ня ! MyApp -м ! MainWindow.nib ! SomeImage.png ! Documents - добавления здесь! ! Library ! Caches ! Preferences• Applications only read and write within their home directory• Backed up by iTunes during sync (mostly)
  4. 4. File Paths in Your Application File Paths in Your Application // Basic directories путь к документам NSString *homePath = NSHomeDirectory(); // Basic directories NSTemporaryDirectory(); NSString *tmpPath = NSString *homePath = NSHomeDirectory(); // Documents directory NSString *tmpPath = NSTemporaryDirectory(); NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); // Documents directory NSArray *paths = NSSearchPathForDirectoriesInDomains NSString *documentsPath = [paths objectAtIndex:0]; (NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; // <Application Home>/Documents/foo.plist NSString *fooPath = // <Application stringByAppendingPathComponent:@“foo.plist”]; [documentsPath Home>/Documents/foo.plist NSString *fooPath = [documentsPath stringByAppendingPathComponent:@“foo.plist”];Tuesday, February 2, 2010 1Tuesday, February 2, 2010 16
  5. 5. SQLite!• база в файле• нет сервера• простая реализация• включена в iPhone
  6. 6. 6 шагов• находим файл базы• подключаемся - sqlite3_open• создаем запрос• готовим, проверяем - sqlite3_prepare_v2• выполняем - sqlite3_step• завершаем - sqlite3_finalize• закрываем - sqlite3_close
  7. 7. шаг 0 - создать БД$ sqlite3 ex1SQLite version 3.6.11Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> create table tbl1(one varchar(10), two smallint);sqlite> insert into tbl1 values(hello!,10);sqlite> insert into tbl1 values(goodbye, 20);sqlite> select * from tbl1;hello!|10goodbye|20sqlite>
  8. 8. подготовка #import <sqlite3.h> NSString *databaseName = @"Cocktails.sql"; NSArray *documentPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; NSString *databasePath = [documentsDirstringByAppendingPathComponent:databaseName]; BOOL success; NSFileManager *fileManager = [NSFileManager defaultManager]; success = [fileManager fileExistsAtPath:databasePath]; if(!success) { NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; [fileManager release]; }
  9. 9. чтениеsqlite3 *database; if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { const char *sqlStatement = "select * from cocktail;"; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) ==SQLITE_OK) { while(sqlite3_step(compiledStatement) == SQLITE_ROW) { int anId = sqlite3_column_int(compiledStatement, 0); NSString *aName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)]; //... } } sqlite3_finalize(compiledStatement); } sqlite3_close(database);
  10. 10. запись• просто sqlite3_step(compiledStatement), при удаче - SQLITE_DONE
  11. 11. GoogleMaps API Facebook API Яндекс Карты APITwitter API общение с вашим сервером
  12. 12. xml<?xml version="1.0" encoding="UTF-8" ?><painting> <img src="madonna.jpg" alt=Foligno Madonna, byRaphael/> <caption>This is Raphaels "Foligno" Madonna, painted in <date>1511</date>–<date>1512</date>. </caption></painting>
  13. 13. xml NSURL *xmlURL = [NSURL URLWithString:URL]; NSXMLParser * rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; [rssParser setDelegate:self]; [rssParser parse];- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementNamenamespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qNameattributes:(NSDictionary *)attributeDict- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementNamenamespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string- (void)parserDidEndDocument:(NSXMLParser *)parser {
  14. 14. What does a JSON object look like? json { “instructor” : “Josh Shaffer”, “students” : 60, “itunes-u” : true, “midterm-exam” : null, “assignments” : [ “WhatATool”, “HelloPoly”, “Presence” ] } • NSDictionary *result = [jsonString JSONValue];y, February 2, 2010 41
  15. 15. http://empatika.comoleg.parinov@empatika.com

×