【IOS】APP使用Safari做分享推廣功能

  • 文章已在 2017年04月26日14:00 更新

例如:

分享一個頁面,然后用戶通過這個頁面去下載app,完成安裝后,打開這個app,就可以獲取分享的一些數(shù)據(jù),比如:掃碼注冊、分享、推廣邀請... 當通過appstore下載完app之后,打開該app,可以完成自動注冊,自動登錄,自動綁定等等操作

一、獲取Cookies思路

iOS9之前是不可以的,因為iOS應用是在沙盒運行,app中創(chuàng)建的webView的cookies都是存儲在單獨的沙盒中的,但是iOS9之后,增加了一個全新的類SFSafariViewController,這個相當于在app內(nèi)部創(chuàng)建了一個瀏覽器,用的和safari瀏覽器共同的cookies,所以可以用SFSafariViewController來獲取cookies

二、使用SFSafariViewController

//首先需要導入頭文件 
#import <SafariServices/SafariServices.h>
self.safariView = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"三步驟中所說的web頁地址"]];
self.safariView.delegate = self;
[self presentViewController:self.safariView animated:false completion:nil]

一般使用這兩個代理函數(shù)

//點擊Done按鈕
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller{}

//加載完成
- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully{}

之后就可以打開指定的URL,并且使用的是系統(tǒng)safari的cookies,打開的頁面像下面這樣

SFView

三、系統(tǒng)Safari和App共用cookies

首先,必須讓用戶在safari中打開指定的鏈接(可以帶有cookie),比如:http://www.jdxyz.com:10000/common/share?uid=12386&shareid=588
然后這樣cookie就存在safari瀏覽器中了

然后,我們再打開我們的app,如果我們代碼設(shè)置為打開就調(diào)用SFSafariViewController,那么SFSafariViewController就會訪問一個web頁面,這個web頁面是前端寫給我的,可以獲取到safari的cookies,然后這個頁面中通過js(調(diào)用的代碼為:location.href = "myscheme://?uid=12386&shareid=588")這樣就可以在app的openurl代理方法中獲取傳過來的值

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options NS_AVAILABLE_IOS(9_0)
{
    [self getUrlDataAPI:url];
    return true;
}

// 獲取參數(shù)
-(void)getUrlDataAPI:(NSURL *)url
{
    NSLog(@"獲取到url:%@",[url relativeString]);
    if ([[url scheme] isEqualToString:@"myscheme"] && [url query])
    {
        //例子: myscheme://?uid=12386&shareid=588
        NSString * uidString = [self getParamsWithScheme:@"uid" webaddress:[url relativeString]];
        NSString * shareidString = [self getParamsWithScheme:@"shareid" webaddress:[url relativeString]];
    }
}

//根據(jù)正則表達式獲取 url scheme 的參數(shù)
-(NSString *)getParamsWithScheme:(NSString *)param webaddress:(NSString *)webAddress
{
    NSError *error;
    NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",param];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags options:NSRegularExpressionCaseInsensitive error:&error];
    NSArray *matches = [regex matchesInString:webAddress
                                      options:0
                                        range:NSMakeRange(0, [webAddress length])];
    for (NSTextCheckingResult *match in matches) {
        NSString *tagValue = [webAddress substringWithRange:[match rangeAtIndex:2]];
        return tagValue;
    }
    return @"";
}


四、使用SFSafariViewController時的體驗優(yōu)化

下面這個方法是目前比較不錯的,目的就是不讓SFSafariViewController在app運行期間展示出來,這樣就“仿佛”在后臺自己獲取

self.safariView.modalPresentationStyle = UIModalPresentationOverCurrentContext;
self.safariView.view.alpha = 0.05f;(不能設(shè)置為0)
self.safariView.view.backgroundColor = [UIColor clearColor];
self.safariView.view.userInteractionEnabled = NO;
[self presentViewController:self.safariView animated:false completion:nil];

//初始化完成時隱藏
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully{
    if (didLoadSuccessfully){
        [controller dismissViewControllerAnimated:true completion:nil]; //隱藏
    }
}

五、使用webView獲取cookies

這個cookies是沙盒中的cookies,就是App自己的cookies,不是safari的cookies,所以無法共用

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSHTTPCookieStorage * sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    
    NSArray * cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:@"http://adapp.jidonggame.com/"]];
    
    NSEnumerator * enumerator = [cookies objectEnumerator];
   
    NSHTTPCookie * cookie;
   
    while (cookie = [enumerator nextObject]) 
    {
        NSLog(@"COOKIE{name: %@, value: %@}", [cookie name], [cookie value]);
        
        [sharedHTTPCookieStorage deleteCookie:cookie];
    }
}

六、URL的說明

NSString * linkStr = @"damon://sdf?mmmm?ss";
NSURL * s = [NSURL URLWithString:linkStr];
NSLog(@"uu:%@\n",s);
NSLog(@"scheme:%@\n",[s scheme]);
NSLog(@"host:%@\n",[s host]);
NSLog(@"query:%@\n",[s query]);
NSLog(@"%@\n",[s relativeString]);

這是輸出的內(nèi)容:

輸出

七、參考文章

胡東東的博客 - IOS未安裝APP獲取Safari瀏覽器數(shù)據(jù)

胡東東的博客 - IOS的軟件之間的調(diào)用


歡迎關(guān)注我的微信公共號:iapp666666

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容