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

댓글 없음:

댓글 쓰기

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

Xcode 26의 새로운 기능들

 애플의 AI준비에 대한 골든타임이 지나가고 있다는 말이 많이 들립니다. ㅎㅎ  애플의 그동안의 패쇄적인 환경을 생각하면 온디바이스AI나 LLM환경에서 다소 아쉬운 부분이 많이 있습니다. 다른 빅테크들과 전혀 다른 방향의 목소리를 내고 있어서 좀 우려...