码迷,mamicode.com
首页 > Web开发 > 详细

url scheme系列一----基本使用

时间:2015-07-07 11:03:03      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

url scheme是什么,能做什么?

  • url cheme,用于两个独立的App之间,让A应用能够打开B应用
  • A应用打开B应用时,告诉B应用当前这个传入的scheme要干什么,所需要的参数
  • URL Schemes 是一个数组,允许应用定义多个 URL schemes每一个scheme对应不同的业务操作

 

url scheme操作步骤一、根据要调用的App规定的scheme和参数格式进行拼接,最终打开的url

/**
 *  生成完整的 scheme url
 */
- (NSString*)_genShareUrl:(XZHMessage *)msg to:(XZHTencentPlatform)shareTo {
    
    NSString *url = [[NSString alloc] initWithString:ShareSchema];
    
    NSString *boundleName = [XZHShareManager base64Encode:[XZHShareManager CFBundleDisplayName]];
    NSString *callback_name = [[self optionDict] objectForKey:@"callback_name"];
    
    NSMutableDictionary *params = [@{
                                    @"thirdAppDisplayName" : boundleName,
                                    @"version" : @"1",
                                    @"cflag" : [NSString stringWithFormat:@"%ld", shareTo],
                                    @"callback_type" : @"scheme",
                                    @"generalpastboard" : @"1",
                                    @"callback_name" : callback_name,
                                    @"src_type" : @"app",
                                    @"shareType" : @"0",
                                    } mutableCopy];
    
    //如果未给当前message对象设置 分享消息类型,则默认为 news类型
    if (msg.link && !msg.messageType) {
        msg.messageType = XZHMessageNews;
    }
    
    NSDictionary *subParams = nil;
    
    if ([msg computeMessageType] == XZHMessageText) {
        
        NSString *fileData = [XZHShareManager base64AndUrlEncode:msg.title];
        subParams = @{
                      @"file_type" : @"text",
                      @"file_data" : fileData
                      };
        
    } else if ([msg computeMessageType] == XZHMessageImage) {
        
        //将message保存到剪贴板
        NSDictionary *data=@{
                             @"file_data":msg.imageData,
                             @"previewimagedata":msg.thumbImageData?:msg.imageData
                             };
        
        [[XZHShareManager manager] clipBoardSave:data
                                          ForKey:SaveObjectForQQPlatformKey
                                        Encoding:XZHClipBoardNSKeyedArchiver];
        
        NSString *title = [XZHShareManager base64AndUrlEncode:msg.title];
        NSString *desc = [XZHShareManager base64AndUrlEncode:msg.desc];
        subParams = @{
                      @"file_type" : @"img",
                      @"title" : title,
                      @"objectlocation" : @"pasteboard",
                      @"description" : desc,
                      };
        
    }else if ([msg computeMessageType] == XZHMessageNews) {
        
        NSDictionary *data=@{@"previewimagedata":msg.imageData};
        
        //图片保存到剪贴板
        [[XZHShareManager manager] clipBoardSave:data
                                          ForKey:SaveObjectForQQPlatformKey
                                        Encoding:XZHClipBoardNSKeyedArchiver];
        
        NSString *title = [XZHShareManager base64AndUrlEncode:msg.title];
        NSString *url = [XZHShareManager base64AndUrlEncode:msg.link];
        NSString *desc = [XZHShareManager base64AndUrlEncode:msg.desc];
        
        NSString *msgType=@"news";
        if (msg.messageType == XZHMessageNews) {
            msgType = @"news";
        } else if (msg.messageType == XZHMessageAudio) {
            msgType = @"audio";
        }
        
        subParams = @{
                      @"file_type" : msgType,
                      @"title" : title,
                      @"url" : url,
                      @"description" : desc,
                      @"objectlocation" : @"pasteboard",
                      };
    }
    
    //得到最后字典
    [params addEntriesFromDictionary:subParams];
    
    //构造最后的scheme url
    url = [XZHShareManager urlStringWithOriginUrlString:url appendParameters:params];
    
    return url;
}

 

url scheme操作步骤二、使用UIApplication打开这个url

+ (void)openURL:(NSString *)url {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

 

 

url scheme操作步骤三、被打开的App回传结果,掉起我们自己的App的回调函数,Appdelegate里面的方法

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    
    //其他应用掉起当前应用时,传回的参数,如果传入的url能够处理
    if ([[XZHShareManager manager] handleOpenURL:url]) {
        return YES;
    }
    
    //不能处理的App调起
    return YES;
}

 

url scheme系列一----基本使用

标签:

原文地址:http://www.cnblogs.com/xiongzenghui/p/4626252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!