iOS SDK: Working with URL Schemes

 在自己的viewcontroller上面加 3个button :,然后添加3个对应的方法:

-(IBAction) openMaps:(id)sender {
    // Opens a map containing Envato's Headquarters
    UIApplication *ourApplication = [UIApplication sharedApplication];
    NSString *ourPath = @"http://maps.google.com/maps?ll=-37.812022,144.969277";
    NSURL *ourURL = [NSURL URLWithString:ourPath];
    [ourApplication openURL:ourURL];
}
 
-(IBAction) openYoutube:(id)sender {
    // Opens a video of an iPad 2 Commercial
    UIApplication *ourApplication = [UIApplication sharedApplication];
    NSString *ourPath = @"http://www.youtube.com/watch?v=TFFkK2SmPg4";
    NSURL *ourURL = [NSURL URLWithString:ourPath];
    [ourApplication openURL:ourURL];
}
 
-(IBAction) openReceiverApp:(id)sender {
    // Opens the Receiver app if installed, otherwise displays an error
    UIApplication *ourApplication = [UIApplication sharedApplication];
    NSString *URLEncodedText = [self.textBox.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *ourPath = [@"readtext://" stringByAppendingString:URLEncodedText];  //重点:readtext:// 就是你第二个app 的标示
    NSURL *ourURL = [NSURL URLWithString:ourPath];
    if ([ourApplication canOpenURL:ourURL]) {
        [ourApplication openURL:ourURL];
    }
    else {
        //Display error
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Receiver Not Found" message:@"The Receiver App is not installed. It must be installed to send text." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
 
}
然后在第二个app 中  打开XXX-Info.plist  文件
  1. 打开info.plist,添加一项URL types
  2. 展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme
  3. 展开URL Scheme,将Item1的内容修改为myapp
  4. 其他程序可通过readtext://访问此自定义URL

  5. 然后在第二个app 的xxxAppDelegate.m中写
  6. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
     
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
     
        //Display error is there is no URL
        if (![launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]) {
            UIAlertView *alertView;
            alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"This app was launched without any text. Open this app using the Sender app to send text." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
            [alertView release];
        }
     
        return YES;
    }

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
     
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
     
        //Display error is there is no URL
        if (![launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]) {
            UIAlertView *alertView;
            alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"This app was launched without any text. Open this app using the Sender app to send text." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
            [alertView release];
        }
     
        return YES;
    }
    这样这2个app 就 可以完成通信了 ! 哈哈!
    具体参考链接:http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-working-with-url-schemes/
系统的openurl有
  1. Map http://maps.google.com/maps?q=Shanghai
  2. Email mailto://myname@google.com
  3. Tel tel://10086
  4. Msg sms://10086
Map http://maps.google.com/maps?q=Shanghai

Email mailto://myname@google.com 

Tel tel://10086 

Msg sms://10086


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值