開始之前,準備一個測試數據。分享出去的其實是一個URL,我們這邊呢簡單寫個小demo就好。
- (IBAction)toEat:(UIButton *)sender {
// 1.獲取應用程序App-B的URL Scheme
/*
biz 業務類型。1-購物
et 頁面。 1-單品頁
*/
NSURL *appBUrl = [NSURL URLWithString:[NSString stringWithFormat:@"ue://storeID=%zd&productID=1&biz=1&et=1",sender.tag]];
// 2.判斷手機中是否安裝了對應程序
if ([[UIApplication sharedApplication] canOpenURL:appBUrl]) {
// 3. 打開應用程序App-B
[[UIApplication sharedApplication] openURL:appBUrl];
} else {
NSLog(@"沒有安裝");
}
}
涉及到跳轉,我們這邊需要給一個白名單LSApplicationQueriesSchemes
添加白名單.jpeg
到此為止,我們的準備數據工作就完成了。下面開始進入我們自己的App處理代碼.......
part 1 約定scheme
scheme.jpeg
這里的scheme
跟測試數據里面的appBUrl
的前綴、LSApplicationQueriesSchemes
是一樣的~
part 2 代碼處理部分
當App運行在前臺時,點擊分享鏈接進來在此處拿到URL
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
NSString *string =[url absoluteString];
if ([string hasPrefix:@"ue"])
{
self.tab.selectedIndex = 0;
//點擊分享進來
[self postShareNotiWithUrlStr:string];
}
return YES;
}
當App被殺死時,點擊分享鏈接進來在此處拿到URL
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
self.tab = [JKTabBarController new];
self.window.rootViewController = self.tab;
[self.window makeKeyAndVisible];
//程序殺死后獲取分享url
NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
NSString *string =[url absoluteString];
if ([string hasPrefix:@"ue"])
{
[self postShareNotiWithUrlStr:string];
}
return YES;
}
拿到URL之后的處理
-(void)postShareNotiWithUrlStr:(NSString *)urlStr{
NSRange range = [urlStr rangeOfString:@"://"];
NSString *paramStr = [urlStr substringFromIndex:range.location + range.length];
NSArray *arrA = [paramStr componentsSeparatedByString:@"&"];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
for (int i = 0; i < arrA.count; i++) {
NSArray *arrB = [arrA[i] componentsSeparatedByString:@"="];
[dic setValue:arrB.lastObject forKey:arrB.firstObject];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//發送通知去首頁
[[NSNotificationCenter defaultCenter] postNotificationName:@"toHome" object:nil userInfo:dic];
});
}
首頁邏輯的處理
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toDetailVC:) name:@"toHome" object:nil];
}
-(void)toDetailVC:(NSNotification *)noti{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"toHome" object:nil];
JKCommodityDetailsController *commodityDetailsController = [[JKCommodityDetailsController alloc] init];
NSDictionary *dic = noti.userInfo;
if ([[dic valueForKey:@"biz"] isEqualToString:@"1"] && [[dic valueForKey:@"et"] isEqualToString:@"1"])
{
commodityDetailsController.storeID = [dic valueForKey:@"storeID"];
commodityDetailsController.productId = [[dic valueForKey:@"productID"] integerValue];
commodityDetailsController.SourceType = SourceTypeShare;
}
[self.navigationController pushViewController:commodityDetailsController animated:YES];
}
part 3 效果圖
效果圖.gif