2014년 11월 9일 일요일

아이폰 - 아이클라우드(iCloud) 간단한 데모 코드 입니다.

아이클라우드(iCloud) 간단한 데모 코드 입니다. 조회 : 79
나의 폴더 > 아이폰 | 2012-12-13 (Thu) 12:44http://blog.dreamwiz.com/papasmf1/13911329
아이클라우드(iCloud) 간단한 데모 코드 입니다.
복잡하게 많은 기능들을 제공하지만 아래의 코드에서는 키-밸류 기반으로 간단한 동기화 데모를
보실 수 있습니다.

//
//  ViewController.h
//  DemoiCloudKey
//
//  Created by kim jong duk on 12. 9. 7..
//  Copyright (c) 2012 kim jong duk. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    UITextField *textField;
    NSUbiquitousKeyValueStore *keyStore;
}

@property (strongnonatomicIBOutlet UITextField *textField;
-(IBAction)saveKey;

@end

//
//  ViewController.m
//  DemoiCloudKey
//
//  Created by kim jong duk on 12. 9. 7..
//  Copyright (c) 2012 kim jong duk. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize textField;

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    keyStore = [[NSUbiquitousKeyValueStore allocinit];
    
    NSString *storedString = [keyStore stringForKey:@"MyString"];
    if(storedString != nil) {
        textField.text = storedString;
    }
    
    [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(ubiquitousKeyValueStoreDidChange:)name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:keyStore];
    
}

-(void)ubiquitousKeyValueStoreDidChange:(NSNotification *)notification
{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Change detected" message:@"iCloud key-value store change detected" delegate:nilcancelButtonTitle:@"OK" otherButtonTitles:nilnil];
    [alert show];
    textField.text = [keyStore stringForKey:@"MyString"];
}

-(void)saveKey
{
    [keyStore setString:textField.text forKey:@"MyString"];
    [keyStore synchronize]; 
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end



Xcode 4.5에서 Single-View Application으로 생성해서 프로젝트 => 타겟에서 Entitlements 메뉴에서 위의 그림처럼 아이클라우드를 사용하도록 체크하면 됩니다. 

댓글 없음:

댓글 쓰기

참고: 블로그의 회원만 댓글을 작성할 수 있습니다.

송길영 작가의 신간 - "경량문명의 탄생" 독서 후기입니다.

  문명이 바뀌고 있다는 것을 누구나 감지하고 있습니다. ㅎㅎ 중량문명이 아닌 경량문명의 시대가 도래하고 있습니다. 수업을 하면서, 또는 친구들과 대화하면서 문명이 바뀌고 있다는 말을 자주했는데 정말 설명하기 좋은 단어가 바로 "경량문명...