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 메뉴에서 위의 그림처럼 아이클라우드를 사용하도록 체크하면 됩니다. 

댓글 없음:

댓글 쓰기

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

테슬라 차량이 무인 배송이 된 역사적인 사건입니다. ㅎㅎ

테슬라는 꿈을 그리는 기업입니다. 좀 황당하고 무섭기도 한데 시간이 지연되고 있지만 꾸준하게 발전하는 모습을 보여줍니다.  테슬라 차량을 주문한 고객이 무인 배송으로 차를 받는 영상입니다. ㅋㅋ  이러다가 정말 화성 탐사선에 옵티머스 로봇이 탑재되는 ...