zoukankan      html  css  js  c++  java
  • URL Scheme程序之间相互调用传值

    在ios程序间通信,可以通过URL Scheme,判断是否安装了另外的应用,打开特定 URL 的方式来传递参数给

    另一个程序。例如:在程序A(Receiver)的Info.plist中加入你需要注册的URL Scheme,然后在你的应用程序B中

    加入处理这类请求的代码,从而实现在B程序中调用A程序,判断A程序是否安装,跳到安装界面,传递参数给A程序。

    具体操作如下:

    在程序A中:

    Info.plist中,增加一个字段,名称为(URL Types),Xcode会自动为你创建一个必须的键:

    URL Identifier,这个键的值可以赋值为一个唯一的字符串。通常是逆向的域名结构,如:me.venj.myapp

    然后在URL Types这个键下增加一个子项:(URL Schemes),这里填入你想注册的URL Scheme的名称,

    如:cloud,你可以增加多个URL Scheme

     

    在程序B中:

    加入调用传参判断,我们可以在AppDelegate中加入一个方法,来处理这个请求

    /* For iOS 4.1 and earlier */

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

     // Handle url request.

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"URL Request"

     message:[url absoluteString] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

     [alert show];

     return YES;

    }

    /* For iOS 4.2 and later */

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url

       sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

      // Handle url request.

       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"URL Request" 

       message:[url absoluteString] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

       [alert show];

       return YES;

    }

    也可以在其它类中添加单独的方法处理

    -(void) openReceiverApp:(id)sender {

        // Opens the Receiver app if installed, otherwise displays an error

        UIApplication *ourApplication = [UIApplication sharedApplication];

        NSString *URLEncodedText = @"传递的内容";//传递参数

        /**  

         Url拼装形式

         Url Schemes :// +传递内容。

        */

        NSString *ourPath = [@"cloud://" stringByAppendingString:URLEncodedText];

        NSURL *ourURL = [NSURL URLWithString:ourPath];

        //判断程序A(Receiver)是否已经安装

        if ([ourApplication canOpenURL:ourURL]) {//检测已经安装

            //[ourApplication openURL:ourURL];//open应用A(Recevier)

          UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"Receiver Found"message:@""delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];

            alertView.tag = 101;

            alertView.delegate = self;

            [alertView show];

            [alertView release];

        }else {//检测未安装应用

            UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"" message:

             @"The Receiver App is not installed." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [alertView show];

            [alertView release];

            UIApplication *ourApplication = [UIApplication sharedApplication];

            NSURL *ourURL = [NSURL URLWithString:

            @"https://itunes.apple.com/us/app/da-yan-shi-pin/id586827887?ls=1&mt=8"];

            [ourApplication openURL:ourURL];//打开下载地址页面

        }

    }

    iOS 预定义了如下几种URL Scheme 。

    Mail:          mailto:frank@wwdcdemo.example.com

    Tel:            tel:1-408-555-5555

    SMS:        1-408-555-1212

    Map:                  http://maps.google.com/maps?q=cupertino

    YouTube: http://www.youtube.com/watch?v=VIDEO_IDENTIFIER

    iTunes: http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=156093464&id=156093462&s=143441

     

    利用一些方法获得当前正在运行的进程信息,从进程信息中获得安装的app信息。

    //  ViewController.h

    //  OpenProcess

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController

    @end

    @interface UIDevice (ProcessesAdditions)

    - (NSArray *)runningProcesses;

    @end

     

     

    //  ViewController.m

    //  OpenProcess

    #import <sys/sysctl.h>

    #import "ViewController.h"

     @interfaceViewController ()

    -(void)openProcess;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [superviewDidLoad];

        self.view.backgroundColor = [UIColorwhiteColor];

        UIButton *loginbt = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

        loginbt.frame = CGRectMake(40, 140, 141, 45);

        [loginbt setTitle:@"开启线程" forState:UIControlStateNormal];

        loginbt.tag = 900;

        [loginbt addTarget:selfaction:@selector(openProcess) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:loginbt];

    }

    -(void)openProcess {

    //    NSProcessInfo *proInfo = [NSProcessInfo processInfo];

    //    NSLog(@"----processName----%@",[proInfo processName]);  //进行名称

    //    NSLog(@"----processName----%d",[proInfo processIdentifier]);  //进程号,系统分配给该进程的号

    //    NSLog(@"----processName----%d",[proInfo processorCount]);  //获取cpu数目

    //    

    //    NSLog(@"------hostname-----%@",[proInfo hostName]);  //进程的主机名称

    //    

    //    NSLog(@"------systemName----%@",[proInfo operatingSystemName]);  //系统名称

    //    NSLog(@"------systemVersion-%@",[proInfo operatingSystemVersionString]); //系统版本

    //    NSLog(@"------system--------%d",[proInfo operatingSystem]);  //操作系统代表数字

        NSArray * processes = [[UIDevice currentDevice] runningProcesses];

        for (NSDictionary * dict in processes){

           //输出正在进程中的应用

            NSLog(@"%@ - %@", [dict objectForKey:@"ProcessID"], [dict objectForKey:@"ProcessName"]);

        }

    - (void)didReceiveMemoryWarning{

        [superdidReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

    @implementation UIDevice (ProcessesAdditions)

    - (NSArray *)runningProcesses {

        int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};

        size_t miblen = 4;

        size_t size;

        int st = sysctl(mib, miblen, NULL, &size, NULL, 0);

        struct kinfo_proc * process = NULL;

        struct kinfo_proc * newprocess = NULL;

        do {

            size += size / 10;

            newprocess = realloc(process, size);

            if (!newprocess){

                if (process){

                    free(process);

                }

                returnnil;

            }

            process = newprocess;

            st = sysctl(mib, miblen, process, &size, NULL, 0);

        } while (st == -1 && errno == ENOMEM);

        if (st == 0){

            if (size % sizeof(struct kinfo_proc) == 0){

                int nprocess = size / sizeof(struct kinfo_proc);

                if (nprocess){

                    NSMutableArray * array = [[NSMutableArrayalloc] init];

                    for (int i = nprocess - 1; i >= 0; i--){

                       NSString * processID = [[NSStringalloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];

                        NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];

                        NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]

                                                                            forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];

                        [processID release];

                        [processName release];

                        [array addObject:dict];

                        [dict release];

                    }

                    free(process);

                    return [array autorelease];

                }

            }

        }

          returnnil;

    }

    @end

     

    参考

    http://blog.csdn.net/kudy21/article/details/7803216

    http://forrst.com/posts/UIDevice_Category_For_Processes-h1H

    http://www.cocoachina.com/newbie/tutorial/2012/0529/4302.html

     

     

  • 相关阅读:
    java 读取src下的配置文件
    图书管理系统
    StaticDemo
    MatixDemo
    Magics
    GetPrime
    ConWithThis
    TestTHIS
    ConstructorOverLoad
    Sequence
  • 原文地址:https://www.cnblogs.com/jiangshiyong/p/3040599.html
Copyright © 2011-2022 走看看