zoukankan      html  css  js  c++  java
  • 关于iCloud的注册,到代码的实现

    关于iCloud的注册,到代码的实现   



    iCloud需要xcode4.x IOS5 sdk 请先做好准备工作


    1.需要传件一个新的app id,要是有了一个的话,保证着个app id 不是一个通配符的那种。

    2.创建完成之后,你要做的是开启这项功能,就跟开发推送一样,然后在创建一个新的Provisional Profile


    3.选择工程的summary,滚动到entitlement点击entitlements,xcode会自动的创建一个*.entitlements


    4.点击创建的*.entitlements,分别把pist列表里的三个字段都添上内容,格式为 (Team_ID.com.yourcompany.icloudtest),不要把team_id 跟 app_id弄混了啊,team_id是你创建完Provisional的时候,在最前面显示的那10个字符。


    5.然后就可以在delegate里面写下面的代码


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      // Override point for customization after application launch.
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

      NSFileManager *fileManager = [NSFileManager defaultManager];
      // Team-ID + Bundle Identifier
      NSURL *iCloudURL = [fileManager URLForUbiquityContainerIdentifier:@"ZZZZ826ZZ2.com.yourcompany.icloudtest"];
      NSLog(@"%@", [iCloudURL absoluteString]);

      NSUbiquitousKeyValueStore *cloudStore = [NSUbiquitousKeyValueStore defaultStore];
      [cloudStore setString:[iCloudURL absoluteString] forKey:@"iCloudURL"];
      [cloudStore synchronize]; // Important as it stores the values you set before on iCloud

      UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,round(self.window.bounds.size.height/4.0),self.window.bounds.size.width,round(self.window.bounds.size.height/8.0))];
      myLabel.font = [UIFont fontWithName:@"Marker Felt" size:round(self.window.bounds.size.width/20.0)];
      myLabel.numberOfLines = 4;
      myLabel.text =[ @"iCloudURL=" stringByAppendingFormat:@"%@", [cloudStore stringForKey:@"iCloudURL"]];
      myLabel.backgroundColor = [UIColor clearColor];
      myLabel.textColor = [UIColor whiteColor];
      myLabel.textAlignment = UITextAlignmentCenter;
      [self.window addSubview:myLabel];

      [self.window makeKeyAndVisible];
      return YES;
    }




    下面的代码,是参考的一个demo,没有试验过,不过应该是没问题的,仅供参考




    - (void)viewDidLoad
    {
        // 文件
        NSString *fileName = @"data.txt";

        // 得到文件管理器
        NSFileManager *manager = [NSFileManager defaultManager];

        // 验证iCloud是否激活
        NSURL *url = [manager URLForUbiquityContainerIdentifier:nil];
        if (url == nil) 
        {
            NSLog(@"iCloud未激活");
            return;
        }

        // 指定iCloud完整路径
        self.icloudURL = [NSURL URLWithString:fileName relativeToURL:url];

        // 得到本程序沙盒路径
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        self.filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];

        [super viewDidLoad];
    }




    - (void)viewDidUnload
    {
        self.filePath = nil;
        self.icloudURL = nil;
        self.textView = nil;
        self.label = nil;
        [super viewDidUnload];
    }


    // 按下上传按钮
    - (IBAction)uploadPressed:(id)sender
    {
        // 另起一个线程上传
        [NSThread detachNewThreadSelector:@selector(uploadICloud) toTarget:self withObject:nil];
    }


    - (void)uploadICloud
    {
        // 调用主进程的方法更新界面,在主进程外更新界面常会引起错误
        [self performSelectorOnMainThread:@selector(setString:)
                               withObject:@"开始上传"
                            waitUntilDone:NO];

        NSFileManager *manager = [NSFileManager defaultManager];

        NSArray *data = [[NSArray alloc] initWithObjects:self.textView.text, nil];

        // 判断本地文件是否存在
        if (![manager fileExistsAtPath:self.filePath]) {
            // 不存在则创建
            if (![data writeToFile:self.filePath atomically:YES]) 
            {
                [self performSelectorOnMainThread:@selector(setString:)
                                       withObject:@"写本地文件失败"
                                    waitUntilDone:NO];
            }
        }

        // 判断iCloud里该文件是否存在
        if ([manager isUbiquitousItemAtURL:self.icloudURL]) {
            // 存在则修改
            if (![data writeToURL:self.icloudURL atomically:YES]) 
            {
                [self performSelectorOnMainThread:@selector(setString:)
                                       withObject:@"写iCloud文件失败"
                                    waitUntilDone:NO];
            }
            [self performSelectorOnMainThread:@selector(setString:)
                                   withObject:@"上传成功"
                                waitUntilDone:NO];
            return;
        }

        // 上传至iCloud
        // 指定本地文件完整路径
        NSURL *url = [NSURL fileURLWithPath:self.filePath];
        NSError *error;
        // 官方文档建议本方法不要在主进程里执行
        if (![manager setUbiquitous:YES itemAtURL:url destinationURL:self.icloudURL error:&error]) 
        {
            NSLog(@"setUbiquitous error %@,\n%@", error, [error userInfo]);
            self.label.text = @"上传失败";
            return;
        }
        self.label.text = @"上传成功";
    }


    // 按下下载按钮
    - (IBAction)downloadPressed:(id)sender
    {
        self.label.text = @"开始下载";

        // 下载icloud文件
        if (![self downloadFileIfNotAvailable:self.icloudURL]) 
        {
            self.label.text = @"下载失败";
            return;
        }
        // 更新界面
        NSArray *array = [[NSArray alloc] initWithContentsOfURL:self.icloudURL];
        self.textView.text = [array objectAtIndex:0];

        self.label.text = @"下载成功";
    }


    // 此方法是官方文档提供,用来检查文件状态并下载
    - (BOOL)downloadFileIfNotAvailable:(NSURL*)file {
        NSNumber*  isIniCloud = nil;

        if ([file getResourceValue:&isIniCloud forKey:NSURLIsUbiquitousItemKey error:nil]) {
            // If the item is in iCloud, see if it is downloaded.
            if ([isIniCloud boolValue]) {
                NSNumber*  isDownloaded = nil;
                if ([file getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil]) {
                    if ([isDownloaded boolValue])
                        return YES;

                    // Download the file.
                    NSFileManager*  fm = [NSFileManager defaultManager];
                    if (![fm startDownloadingUbiquitousItemAtURL:file error:nil]) {
                        return NO;
                    }
                    return YES;
                }
            }
        }

        // Return YES as long as an explicit download was not started.
        return YES;
    }

  • 相关阅读:
    5Hibernate入门----青软S2SH(笔记)
    5Hibernate配置及使用方法----青软S2SH(笔记)
    4Struts2标签库----青软S2SH(笔记)
    3Struts2进阶----青软S2SH(笔记)
    Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) M
    Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) A
    Codeforces Round #418 (Div. 2) D
    Codeforces Round #418 (Div. 2) C
    Codeforces Round #418 (Div. 2) B
    Codeforces Round #418 (Div. 2) A
  • 原文地址:https://www.cnblogs.com/ligun123/p/2398759.html
Copyright © 2011-2022 走看看