iCloud 与 iAd---iOS

作者: 平安喜乐698 | 来源:发表于2017-09-11 14:40 被阅读13764次

    iCloud云存储

    1. 在苹果开发者中心 AppID中勾选iCloud,重新生成描述文件profile
    2. 在项目中|Capabilities 中打开iCloud,勾选 Key-Value , iCloud Documents。 会自动生成项目.entitlements文件
    3. 手机中设置iCloud账号
    存储文档
    1. 自定义UIDocument对象类
    #import <UIKit/UIKit.h>
    
    @interface YTSDocument : UIDocument
    
    @property (nonatomic,strong) NSData *data;
    @end
    
    #import "YTSDocument.h"
    
    @implementation YTSDocument
    
    
    // 存储时调用
    -(id)contentsForType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError{
    
        if(_data){
        
            return [_data copy];
        }
        return [NSData data];
    }
    
    // 读取时调用
    -(BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError{
    
        _data=[contents copy];
        return true;
    }
    
    @end
    
    
    1. VC
    #import "YTViewController.h"
    #import "YTSDocument.h"
    
    @interface YTViewController ()
    @property (nonatomic,strong) NSMetadataQuery *dataQuery;
    @property (nonatomic,strong) NSMutableDictionary *fileDic;  // @{文件名:修改时间}
    @property (nonatomic,strong) YTSDocument *dcocument;
    @end
    
    
    @implementation YTPersonCCViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        
        // 查询icloud文档列表对象
        _dataQuery=[NSMetadataQuery new];
        _dataQuery.searchScopes=@[NSMetadataQueryUbiquitousDataScope];
        // noti
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(queryFinish:) name:NSMetadataQueryDidFinishGatheringNotification object:_dataQuery];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(queryFinish:) name:NSMetadataQueryDidUpdateNotification object:_dataQuery];
        // 开始查询
        [_dataQuery startQuery];
    }
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
        
        
    }
    
    // noti
    -(void)queryFinish:(NSNotification *)noti{
    
        // 获取查询列表,遍历
        NSArray *dataArr=_dataQuery.results;
        _fileDic=[NSMutableDictionary dictionary];
        [dataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSMetadataItem *item=obj;
            
            // 文件名,最后修改时间
            NSString *fileName=[item valueForAttribute:NSMetadataItemFSNameKey];
            NSDate *date=[item valueForAttribute:NSMetadataItemFSContentChangeDateKey];
            NSDateFormatter *dateFormat=[NSDateFormatter new];
            [dateFormat setDateFormat:@"yy-MM-dd hh:mm"];
            NSString *dateString=[dateFormat stringFromDate:date];
            [_fileDic setObject:dateString forKey:fileName];
        }];
    }
    
    // 获取icloud文件地址
    -(NSURL *)getICloudURL:(NSString *)fileName{
        return [[[[NSFileManager defaultManager]URLForUbiquityContainerIdentifier:@"entitie文件中ID"]URLByAppendingPathComponent:@"Documents"]URLByAppendingPathComponent:fileName];
    }
    // 新建文档
    -(void)addDocument:(NSString *)fileName{
    
        //
        NSURL *fileURL=[self getICloudURL:fileName];
        // 新建文档对象
        YTSDocument *document=[[YTSDocument alloc]initWithFileURL:fileURL];
        [document saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            //
            if(success){
            
                // 开始查询
                [_dataQuery startQuery];
                // reloadData
                
                
                //
                _dcocument=document;    //
            }else{
            
                //
                NSLog(@"save fail");
            }
        }];
    
    }
    
    //
    -(void)removeDocument:(NSString *)fileName{
        
        //
        NSError *error;
        [[NSFileManager defaultManager]removeItemAtURL:[self getICloudURL:fileName] error:&error];
        if(error){
            //
            NSLog(@"error:%@",error.localizedDescription);
        }
        [_fileDic removeObjectForKey:fileName];
        
        // reloadData
    }
    
    -(void)querryDocument:(NSString *)fileName{
        // 获取文档
        NSURL *fileURL=[self getICloudURL:fileName];
        YTSDocument *document=[[YTSDocument alloc]initWithFileURL:fileURL];
        
        // 打开文档
        [document openWithCompletionHandler:^(BOOL success) {
            if(success){
            
                //
                NSString *dataText=[[NSString alloc]initWithData:document.data encoding:NSUTF8StringEncoding];
            }else{
                NSLog(@"read fail");
            }
        }];
        
        // 保存文档
        [document saveToURL:document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
            NSLog(@"save success");
        }];
    }
    @end
    
    
    存储键值对
        // 用法同NSUserDefaults
        NSUbiquitousKeyValueStore *defaults=[NSUbiquitousKeyValueStore defaultStore];
        [defaults setBool:true forKey:@""];
        [defaults synchronize];
        
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(valueChange:) name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:defaults];
    

    iAd内置广告

    #import "YTViewController.h"
    #import <iAd/iAd.h>
    
    @interface YTViewController ()<ADBannerViewDelegate>
    
    @property (nonatomic,strong) ADBannerView *adView;
    @end
    
    
    @implementation YTPersonCCViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
    
        //
        _adView=[ADBannerView new];
        _adView.delegate=self;
        [_adView setFrame:self.view.frame];
        [self.view addSubview:_adView];
    }
    
    #pragma mark dele
    // 广告加载完后调用
    -(void)bannerViewDidLoadAd:(ADBannerView *)banner{
        NSLog(@"loaded ad");
    }
    // 点击广告后调用(false则不弹全屏)
    -(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave{
        return YES;
    }
    // 关闭广告后调用
    -(void)bannerViewActionDidFinish:(ADBannerView *)banner{
        NSLog(@"广告已关闭");
    }
    // 获取广告失败后调用
    -(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
        NSLog(@"广告加载失败:%@",error);
        _adView.hidden=true;
    }
    
    @end
    

    相关文章

      网友评论

        本文标题:iCloud 与 iAd---iOS

        本文链接:https://www.haomeiwen.com/subject/rncfsxtx.html