doudou521125 2014-03-24 15:09
浏览 38
已采纳

通过UIWebView将设备令牌发送到PHP脚本

I am currently working on a transit web app to be used with San Diego's Metropolitan Transit System. For iOS, I was planning on using a TabBar with three items, [Home] [Favorites] [Notifications]. [Home] leads to the main page (and to any other pages), [Favorites] is where you can setup your favorite bus/trolley stops.

I was planning on having [Favorites] be where you can setup a notification to be sent to you before a bus arrives at the scheduled times provided by MTS. So for example, if a bus were to come at 10:30 and you set a notification to arrive 5 minutes before to alert you, you would get one at 10:25.

I was wanting to link the accounts with the device tokens, and I read some of the questions on here such as Linking user account to device token for push notifications.

I believe it is possible to somehow link the accounts by sending the device token to the UIWebView when I login.

Hope someone can help me.

EDIT: Below is the Obj-C code in Home.m

#import "Home.h"

@implementation HomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *URL1 = [NSURL URLWithString:@"url"];

    NSURLRequest *Request1 = [NSURLRequest requestWithURL:URL1];

    [webView1 loadRequest:Request1];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"1");
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    NSString *deviceToken = [defaults objectForKey:@"deviceToken"];
    bool tokenIsSent = [defaults boolForKey:@"tokenIsSent"];
    NSString *newToken = [defaults stringForKey:@"newToken"];
    NSLog(@"2");
    NSString *urlString = [NSString stringWithFormat:@"url"];
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSData *usernameData;
    NSURLResponse *response;
    usernameData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
    NSString *username = [[NSString alloc] initWithData:usernameData encoding:NSUTF8StringEncoding];
    NSLog(@"3");

    if (deviceToken != nil && tokenIsSent == NO) {
        NSLog(@"4");
        NSString *urlString = [NSString stringWithFormat:@"url/s=%@&u=%@",newToken, username];
        NSURL *url = [[NSURL alloc] initWithString:urlString];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        NSData *urlData;
        NSURLResponse *response;
        urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
        NSString *info = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
        if([info  isEqual: @"success"]){
            [defaults setBool:YES forKey:@"tokenIsSent"];
            NSLog(@"5");
        }else{
            [defaults setBool:NO forKey:@"tokenIsSent"];
            NSLog(@"6");
        }
        [defaults synchronize];
    } else {
        NSLog(@"7");
    }

}
  • 写回答

3条回答 默认 最新

  • drby30217 2014-03-24 15:50
    关注

    First save the token to the NSUserDefaults

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:deviceToken forKey:@"deviceToken"];
        [defaults setBool:NO forKey:@"tokenIsSent"];
        [defaults synchronize];
    }
    

    Because this is done in appDelegate, at this point the user is not logged in yet, that's why you need to track using something another value like in the example above tokenIsSent and set it to NO (false).

    Now once the user authenticates, you can then send it to your sever and associated with that user.

    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    bool deviceToken = [defaults boolForKey:@"deviceToken"];
    bool tokenIsSent = [defaults boolForKey:@"tokenIsSent"];
    
    if (deviceToken != nil && tokenIsSent) {
     // it is not null , and it was not sent to PHP so send it
            if(success){
                [defaults setBool:YES forKey:@"tokenIsSent"];
            }else{
                [defaults setBool:NO forKey:@"tokenIsSent"];
            }
            [defaults synchronize];
    }
    

    If the delgate is not being called you need to implement it:

    @interface HomeViewController : UIViewController<UIWebViewDelegate>
    

    And make sure you assign that delegate.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        //Assign the delegate to WebView1
        WebView1.delegate = self
    
        //Perform the request
        NSURL *URL1 = [NSURL URLWithString:@"http://clovercloud.net/app/f/"];
        NSURLRequest *Request1 = [NSURLRequest requestWithURL:URL1];
        [webView1 loadRequest:Request1];
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面