您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 應用間通訊:自定義 URL Schemes 完整指南

編輯:iOS 開發綜合

一、認識 URL Schemes

URL Scheme 是一種類似 https://、ftp:// 的機制,你也可以為自己的 App 定義專屬的 URL Scheme。這樣一來,其他應用便能透過這個標識來唤起並進入你的 App。需要注意的是,如果你自訂的 Scheme 剛好與系統內建應用相同,系統會優先呼叫內建程式,而不會啟動你自訂的應用。

舉個例子:invoking://com.hello/yourpath/?username=WT&password=123456&callback=myapp。其中 invoking 是 URL Scheme,com.hello 是 host,yourpath 是 path,而 username=WT&password=123456&callback=myapp 則是 query 查詢參數。

二、呼叫自己開發的應用

首先,在 plist 檔中註冊對外開放的介面:

CFBundleURLName(URL Identifier):一個描述 URL scheme 抽象名稱的字串。為確保唯一性,建議採用反轉網域(reverse-DNS)風格的識別碼,例如 com.acme.myscheme。這個字串同時也是 InfoPlist.strings 檔中的鍵值,其對應值即為人類可讀的 scheme 名稱。

CFBundleURLSchemes(URL Schemes):一個字串陣列,用來存放 scheme 名稱,例如 http、mailto、tel、sms 等。

你可以設定多組 URL Schemes,設定完成後如下圖所示:


呼叫方的關鍵程式碼如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    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

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

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

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