你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS-應用間通信之自定義URL Schemes

iOS-應用間通信之自定義URL Schemes

編輯:IOS開發綜合

應用間通信:

一、URL Schemes知識的了解

URL Scheme是類似http://, ftp://這樣的東西,同樣你也可以為自己的應用自定URL Scheme,其他應用通過此標識就可以訪問你的應用,如果自定的URL Scheme 和系統應用的相同,則會調用系統應用,而不會調用自定的應用程序。 例如:invoking://com.hello/yourpath/?username=WT&password=123456&callback=myapp 其中invoking是URL Scheme 即[url scheme],com.hello是host,即[url host], yourpath是path,即[url path],username=WT&password=123456&callback=myapp是query,即[url query]。

二、調用自己開發的應用

1)在plist文件中,注冊自定對外接口

CFBundleURLName(URL Identifier) A string containing the abstract name of the URL scheme. To ensure uniqueness, it is recommended that you specify a reverse-DNS style of identifier, for example, com.acme.myscheme.The string you specify is also used as a key in your app’s InfoPlist.strings file. The value of the key is the human-readable scheme name.

CFBundleURLSchemes(URL Schemes) An array of strings containing the URL scheme names—for example, http, mailto, tel, and sms.

可以設置多個(URL Schemes),設置成功後如下:

\

調用方的部分代碼如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    CGRect rectTextView= CGRectMake(10.0f, 30.0f, 300.0f, 100.0f);
    self.textView = [[UITextView alloc] initWithFrame:rectTextView];
    [self.textView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [self.textView.layer setBorderWidth:0.5f];
    [self.textView setText:@"username=WT&password=123456&callback=invoking"];
    [self.view addSubview:self.textView];
    
    CGRect rect = CGRectMake(10.0f, 150.0f, 300.0f, 40.0f);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = rect;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitle:@"Login" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor blueColor]];
    [button addTarget:self action:@selector(handle:) forControlEvents:UIControlEventTouchUpInside];
    [button.layer setMasksToBounds:YES];
    [button.layer setCornerRadius:5.0f];
    [self.view addSubview:button];
}

- (void)handle:(id)sender
{
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"invoked://com.hello/path?%@", self.textView.text]];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"message" message:[NSString stringWithFormat:@"%@", url] delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

被調用方的接收代碼:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"%@", url);
    if ([[url scheme] isEqualToString:@"invoked"]) {
        if ([[url host] isEqualToString:@"com.hello"]) {
            NSString *query = [url query];
            NSArray *array = [query componentsSeparatedByString:@"&"];
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:10];
            for (NSString *item in array) {
                NSArray *valueArray = [item componentsSeparatedByString:@"="];
                [dic setValue:[valueArray objectAtIndex:1] forKey:[valueArray objectAtIndex:0]];
            }
            [self application:application didFinishLaunchingWithOptions:dic];
        }
        return YES;
    }
    return NO;
}

同樣,可以在傳入參數中設置一些預留字段,以便以後擴展,要實現再能夠回調回去,就可以加一個callback字段,如:callback=myapp 後面跟上自己應用的URL Scheme,再將執行完成的結果返回回去。代碼處理和被調用方大致相同。
點擊下載完整代碼

參考文獻:

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW20

http://blog.csdn.net/likendsl/article/details/7553605

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

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved