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. iOS SDK: Working with URL Schemes_第1张图片
  6. 然后在第二个app 的xxxAppDelegate.m中写
  7. - (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://[email protected]
  3. Tel tel://10086
  4. Msg sms://10086
Map http://maps.google.com/maps?q=Shanghai

Email mailto://[email protected] 

Tel tel://10086 

Msg sms://10086


你可能感兴趣的:(iOS SDK: Working with URL Schemes)