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

댓글 없음:

댓글 쓰기

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

요즘 새로운 과정을 기획하면서 react.js + next.js를 OpenAI API와 같이 사용하는 과정을 만들고 있습니다. ㅎㅎ

 오랜만에 웹 기술들을 공부하니 재미있네요. ㅎㅎ  쭉 파이썬 과정들을 운영하고 있었는데 좀 더 범위를 넓혀서 아이폰 앱 개발과정 with ChatGPT,  웹 프로그래밍 with ChatGPT, AI시대의 AI어시스턴트 활용하기와 같은 글을 쓰고, ...